From 9bc90729c0101a6608b280ed69fd06ca0c4957e2 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Mon, 1 Apr 2024 22:14:59 +0530 Subject: [PATCH] feat: getData management with jq --- package-lock.json | 4 +- package.json | 2 +- src/exports/stash.d.ts | 86 ++++++++++++++++++++++++++++++--- src/exports/stash.js | 49 ++++++++++++++----- test/unit/dataProcessor.spec.js | 7 ++- 5 files changed, 124 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index f2581c7..72ebf74 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pactum", - "version": "3.6.4", + "version": "3.6.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pactum", - "version": "3.6.4", + "version": "3.6.5", "license": "MIT", "dependencies": { "@exodus/schemasafe": "^1.3.0", diff --git a/package.json b/package.json index 9fc840a..9b37401 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pactum", - "version": "3.6.4", + "version": "3.6.5", "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/exports/stash.d.ts b/src/exports/stash.d.ts index d940e4e..b454707 100644 --- a/src/exports/stash.d.ts +++ b/src/exports/stash.d.ts @@ -6,23 +6,57 @@ export function loadData(path?: string): void; /** - * loads data maps + * adds data maps * @example * stash.addDataMap({ * 'User': { - * 'Name': 'Snow', - * 'Age': 26 + * name: 'Snow', + * age: 26 * } * }); */ export function addDataMap(maps: object): void; + +/** + * adds data maps + * @example + * stash.addDataMap([ + * { + * 'UserOne': { name: 'Snow' } + * } + * { + * 'UserTwo': { name: 'John' } + * } + * ]); + */ export function addDataMap(maps: object[]): void; /** - * loads data templates + * adds data maps + * @example + * stash.addDataMap('PASSWORD', 'password@123'); + * stash.addDataMap('CREDENTIALS', { username: 'john', password: 'password@123' }); + */ +export function addDataMap(key: string, value: any): void; + +/** + * returns data map + */ +export function getDataMap(): object; + +/** + * @example + * const username = stash.getDataMap('CREDENTIALS.username'); + */ +export function getDataMap(path: string): any; + +export function clearDataMaps(): void; + +/** + * adds data templates * @example * stash.addDataTemplate({ - * 'User.NewUser': { + * 'User:NewUser': { * 'Name': 'Snow', * 'Age': 26, * 'Address': [] @@ -32,9 +66,49 @@ export function addDataMap(maps: object[]): void; export function addDataTemplate(templates: object): void; export function addDataTemplate(templates: object[]): void; -export function getDataMap(): object; +/** + * + * @example + * stash.addDataTemplate('USER:NEW', { name: 'john', age: 28 }); + */ +export function addDataTemplate(key: string, value: object): void; + + export function getDataTemplate(): object; + +/** + * @example + * const credentials = stash.getDataTemplate('CREDENTIALS'); + */ +export function getDataTemplate(path: string): object; + +export function clearDataTemplates(): void; + + +/** + * adds data store + * @example + * stash.addDataStore({ + * 'User': { + * name: 'Snow', + * age: 26 + * } + * }); + */ +export function addDataStore(store: object): void; + +/** + * adds data stores + * @example + * stash.addDataStore('PASSWORD', 'password@123'); + * stash.addDataStore('CREDENTIALS', { username: 'john', password: 'password@123' }); + */ +export function addDataStore(key: string, value: any): void; + export function getDataStore(): object; +export function getDataStore(path: string): any; +export function clearDataStores(): void; + export function getStoreKey(key: string): string; export function getMapKey(key: string): string; export function getFunctionKey(key: string): string; diff --git a/src/exports/stash.js b/src/exports/stash.js index 77f9594..cd68a92 100644 --- a/src/exports/stash.js +++ b/src/exports/stash.js @@ -3,6 +3,7 @@ const pt = require('path'); const log = require('../plugins/logger'); const { PactumConfigurationError } = require('../helpers/errors'); const config = require('../config'); +const jq = require('json-query'); let dataMap = {}; let dataTemplate = {}; @@ -40,15 +41,22 @@ const stash = { }); }, - addDataMap(maps) { - if (!Array.isArray(maps)) { - maps = [maps]; + addDataMap(key, value) { + if (value) { + Object.assign(dataMap, { [key]: value }); + } else { + if (!Array.isArray(key)) { + key = [key]; + } + key.forEach(map => Object.assign(dataMap, map)); } - maps.forEach(map => Object.assign(dataMap, map)); config.data.ref.map.processed = false; }, - getDataMap() { + getDataMap(path) { + if (path) { + return jq(path, { data: dataMap }).value; + } return dataMap; }, @@ -58,15 +66,23 @@ const stash = { config.data.ref.map.enabled = false; }, - addDataTemplate(templates) { - if (!Array.isArray(templates)) { - templates = [templates]; + addDataTemplate(key, value) { + if (value) { + Object.assign(dataTemplate, { [key]: value }); + } else { + if (!Array.isArray(key)) { + key = [key]; + } + key.forEach(template => Object.assign(dataTemplate, template)); } - templates.forEach(template => Object.assign(dataTemplate, template)); config.data.template.processed = false; }, - getDataTemplate() { + getDataTemplate(path) { + if (path) { + console.log(dataTemplate) + return jq(path, { data: dataTemplate }).value; + } return dataTemplate; }, @@ -76,12 +92,19 @@ const stash = { config.data.template.enabled = false; }, - addDataStore(store) { - Object.assign(dataStore, store); + addDataStore(key, value) { + if (value) { + Object.assign(dataStore, { [key]: value }); + } else { + Object.assign(dataStore, key); + } config.data.ref.spec.enabled = true; }, - getDataStore() { + getDataStore(path) { + if (path) { + return jq(path, { data: dataStore }).value; + } return dataStore; }, diff --git a/test/unit/dataProcessor.spec.js b/test/unit/dataProcessor.spec.js index 8b6c1d3..862a93a 100644 --- a/test/unit/dataProcessor.spec.js +++ b/test/unit/dataProcessor.spec.js @@ -45,7 +45,7 @@ describe('Data Processing - Templates', () => { it('processTemplates - simple with Overrides', () => { stash.addDataTemplate({ - 'User': { + 'User_New': { 'Name': 'Snow', 'Address': { '@DATA:TEMPLATE@': 'Address', @@ -61,7 +61,7 @@ describe('Data Processing - Templates', () => { }); dp.processTemplates(); expect(dp.template).deep.equals({ - 'User': { + 'User_New': { 'Name': 'Snow', 'Address': { 'Street': 'Main', @@ -75,6 +75,7 @@ describe('Data Processing - Templates', () => { }); expect(config.data.template.enabled).equals(true); expect(config.data.template.processed).equals(true); + expect(stash.getDataTemplate('User_New.Name')).deep.equals('Snow'); }); it('processTemplates - template not found', () => { @@ -443,6 +444,8 @@ describe('Data Processing - Maps', () => { } ] }); + expect(stash.getDataMap('User.Name')).equals('$M{Users[0].Name}'); + expect(stash.getDataMap('Users[0].Name')).equals('Snow'); dp.processMaps(); expect(dp.map).deep.equals({ User: {