Skip to content

Commit

Permalink
Merge pull request #334 from pactumjs/feature/data-management-more-fu…
Browse files Browse the repository at this point in the history
…nctions

feat: getData management with jq
  • Loading branch information
ASaiAnudeep authored Apr 1, 2024
2 parents 0c921ba + 9bc9072 commit 8d91842
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 24 deletions.
4 changes: 2 additions & 2 deletions 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.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",
Expand Down
86 changes: 80 additions & 6 deletions src/exports/stash.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': []
Expand All @@ -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;
Expand Down
49 changes: 36 additions & 13 deletions src/exports/stash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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;
},

Expand All @@ -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;
},

Expand All @@ -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;
},

Expand Down
7 changes: 5 additions & 2 deletions test/unit/dataProcessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Data Processing - Templates', () => {

it('processTemplates - simple with Overrides', () => {
stash.addDataTemplate({
'User': {
'User_New': {
'Name': 'Snow',
'Address': {
'@DATA:TEMPLATE@': 'Address',
Expand All @@ -61,7 +61,7 @@ describe('Data Processing - Templates', () => {
});
dp.processTemplates();
expect(dp.template).deep.equals({
'User': {
'User_New': {
'Name': 'Snow',
'Address': {
'Street': 'Main',
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 8d91842

Please sign in to comment.