Skip to content

Commit

Permalink
Merge pull request #170 from pactumjs/169-data-template-removes
Browse files Browse the repository at this point in the history
feat: add removes property in data templates
  • Loading branch information
ASaiAnudeep authored Jun 13, 2022
2 parents a61d428 + 83e66d0 commit 36f52eb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.1.9",
"version": "3.1.10",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/dataProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const dataProcessor = {
if (!data) return data;
const templateName = data['@DATA:TEMPLATE@'];
const overrides = data['@OVERRIDES@'];
const removes = data['@REMOVES@'] || [];
if (templateName) {
const templateValue = this.template[templateName];
if (templateValue) {
Expand All @@ -63,6 +64,7 @@ const dataProcessor = {
override(data, overrides);
data = this.processDataTemplates(data);
}
removes.forEach(_key => delete data[_key]);
} else {
log.warn(`Template Not Found - ${templateName}`);
}
Expand Down
89 changes: 89 additions & 0 deletions test/unit/dataProcessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,95 @@ describe('Data Processing - Templates', () => {
expect(config.data.template.processed).equals(true);
});

it('processTemplates - removes single property', () => {
stash.addDataTemplate({
'User': {
'Name': 'Snow',
'Address': {
'@DATA:TEMPLATE@': 'Address',
'@REMOVES@': ['Zip']
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'Name': 'Snow',
'Address': {
'Street': 'Main'
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
expect(config.data.template.enabled).equals(true);
expect(config.data.template.processed).equals(true);
});

it('processTemplates - removes multiple properties', () => {
stash.addDataTemplate({
'User': {
'Name': 'Snow',
'Address': {
'@DATA:TEMPLATE@': 'Address',
'@REMOVES@': ['Zip', 'Street']
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'Name': 'Snow',
'Address': {}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
expect(config.data.template.enabled).equals(true);
expect(config.data.template.processed).equals(true);
});

it('processTemplates - removes non existing properties', () => {
stash.addDataTemplate({
'User': {
'Name': 'Snow',
'Address': {
'@DATA:TEMPLATE@': 'Address',
'@REMOVES@': ['Zip', 'Street', 'Pin']
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'Name': 'Snow',
'Address': {}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
expect(config.data.template.enabled).equals(true);
expect(config.data.template.processed).equals(true);
});

afterEach(() => {
config.data.template.enabled = false;
config.data.template.processed = false;
Expand Down

0 comments on commit 36f52eb

Please sign in to comment.