Repository: pactumjs/pactum
Pull request #280 (2023-04-23 09:59:32)

feat: added direct override


Patch:
diff --git a/package-lock.json b/package-lock.json
index 1097724..9787ab4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.3.3",
+  "version": "3.4.0",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 2d28d92..ae795e9 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/config.js b/src/config.js
index f89607b..0893c54 100644
--- a/src/config.js
+++ b/src/config.js
@@ -39,7 +39,8 @@ const config = {
     },
     template: {
       enabled: false,
-      processed: false
+      processed: false,
+      direct_override: false,
     }
   },
   strategy: {
diff --git a/src/exports/stash.d.ts b/src/exports/stash.d.ts
index 4f39e68..d940e4e 100644
--- a/src/exports/stash.d.ts
+++ b/src/exports/stash.d.ts
@@ -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;
\ No newline at end of file
+export function getFunctionKey(key: string): string;
+export function setDirectOverride(value: boolean): void;
\ No newline at end of file
diff --git a/src/exports/stash.js b/src/exports/stash.js
index a48da8a..77f9594 100644
--- a/src/exports/stash.js
+++ b/src/exports/stash.js
@@ -100,6 +100,10 @@ const stash = {
 
   getFunctionKey(key) {
     return `$F{${key}}`;
+  },
+
+  setDirectOverride(value) {
+    config.data.template.direct_override = value;
   }
 
 };
diff --git a/src/helpers/dataProcessor.js b/src/helpers/dataProcessor.js
index c6c3eec..67db491 100644
--- a/src/helpers/dataProcessor.js
+++ b/src/helpers/dataProcessor.js
@@ -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];
@@ -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@'];
+    }
   }
 
 };


Test patch:
diff --git a/test/unit/dataProcessor.spec.js b/test/unit/dataProcessor.spec.js
index bab4909..7e6a12e 100644
--- a/test/unit/dataProcessor.spec.js
+++ b/test/unit/dataProcessor.spec.js
@@ -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', () => {