Skip to content

Commit

Permalink
Merge pull request #280 from pactumjs/279-direct-use-of-overrides-in-…
Browse files Browse the repository at this point in the history
…data-templates

feat: added direct override
  • Loading branch information
ASaiAnudeep authored Apr 23, 2023
2 parents ea678a2 + 9b6199f commit d522943
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 5 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.3.3",
"version": "3.4.0",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const config = {
},
template: {
enabled: false,
processed: false
processed: false,
direct_override: false,
}
},
strategy: {
Expand Down
3 changes: 2 additions & 1 deletion src/exports/stash.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export function getDataTemplate(): object;
export function getDataStore(): object;
export function getStoreKey(key: string): string;
export function getMapKey(key: string): string;
export function getFunctionKey(key: string): string;
export function getFunctionKey(key: string): string;
export function setDirectOverride(value: boolean): void;
4 changes: 4 additions & 0 deletions src/exports/stash.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ const stash = {

getFunctionKey(key) {
return `$F{${key}}`;
},

setDirectOverride(value) {
config.data.template.direct_override = value;
}

};
Expand Down
13 changes: 12 additions & 1 deletion src/helpers/dataProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const dataProcessor = {
if (typeof data !== 'object') return data;
if (!data) return data;
const templateName = data['@DATA:TEMPLATE@'];
const overrides = data['@OVERRIDES@'];
const overrides = this.getOverrides(data);
const removes = data['@REMOVES@'] || [];
if (templateName) {
const templateValue = this.template[templateName];
Expand Down Expand Up @@ -123,6 +123,17 @@ const dataProcessor = {
}
}
return raw;
},

getOverrides(data) {
if (config.data.template.direct_override) {
const cloned_data = klona(data);
delete cloned_data['@DATA:TEMPLATE@'];
delete cloned_data['@REMOVES@'];
return cloned_data;
} else {
return data['@OVERRIDES@'];
}
}

};
Expand Down
143 changes: 143 additions & 0 deletions test/unit/dataProcessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,149 @@ describe('Data Processing - Templates', () => {

});

describe('Data Processing - Templates - Direct Overrides', () => {

before(() => {
stash.setDirectOverride(true);
});

after(() => {
stash.setDirectOverride(false);
});

afterEach(() => {
config.data.template.enabled = false;
config.data.template.processed = false;
stash.clearDataTemplates();
});

it('processTemplates - simple with Overrides', () => {
stash.addDataTemplate({
'User': {
'Name': 'Snow',
'Address': {
'@DATA:TEMPLATE@': 'Address',
'Zip': '524003'
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'Name': 'Snow',
'Address': {
'Street': 'Main',
'Zip': '524003'
}
},
'Address': {
'Street': 'Main',
'Zip': '524004'
}
});
expect(config.data.template.enabled).equals(true);
expect(config.data.template.processed).equals(true);
});

it('processTemplates - complex array of objects with Overrides', () => {
stash.addDataTemplate({
'User': {
'Name': 'Snow',
'Age': 12,
'Nation': 'The North',
'Address': []
},
'Address': {
'Castle': 'WinterFell',
'Region': 'North'
},
'User:Address': {
'@DATA:TEMPLATE@': 'User',
'Hostage': null,
'Address': [
{
'@DATA:TEMPLATE@': 'Address',
'Castle': 'The Wall'
},
{
'@DATA:TEMPLATE@': 'Address'
}
]
}
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'Name': 'Snow',
'Age': 12,
'Nation': 'The North',
'Address': []
},
'Address': {
'Castle': 'WinterFell',
'Region': 'North'
},
'User:Address': {
'Address': [
{
'Castle': 'The Wall',
'Region': 'North'
},
{
'Castle': 'WinterFell',
'Region': 'North'
}
]
,
'Age': 12,
'Name': 'Snow',
'Nation': 'The North',
'Hostage': null
}
});
expect(config.data.template.processed).equals(true);
});

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

});

describe('Data Processing - Maps', () => {

it('processMaps - empty map', () => {
Expand Down

0 comments on commit d522943

Please sign in to comment.