Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added root_path to jsonLike #340

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5",
"version": "3.6.6",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/adapters/json.like.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ class LikeJson {
}

function validate(actual, expected, opts) {
return new LikeJson(opts).compare(actual, expected);
let actual_path = '$';
let expected_path = '$';
if (opts && opts.root_path) {
expected_path = opts.root_path;
}
return new LikeJson(opts).compare(actual, expected, actual_path, expected_path);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Expect {
for (let i = 0; i < this.jsonQueryLike.length; i++) {
const jQ = this.jsonQueryLike[i];
const value = jqy(jQ.path, { data: response.json }).value;
const msg = jlv.validate(value, jQ.value);
const msg = jlv.validate(value, jQ.value, { root_path: jQ.path });
if (msg) this.fail(msg);
}
}
Expand Down
26 changes: 15 additions & 11 deletions test/unit/json.like.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ describe('JSON Like Array of Objects', () => {
}
];
expect(jsl.validate(actual, expected)).equals(`Json doesn't have value '42' at '$[1].scores.languages[0].english' but found '43'`);
expect(jsl.validate(actual, expected, { root_path: 'data' })).equals(`Json doesn't have value '42' at 'data[1].scores.languages[0].english' but found '43'`);
});

it('equals - different order', () => {
Expand Down Expand Up @@ -1043,14 +1044,15 @@ describe('JSON Like - Assert Expressions', () => {

it('object fulfil simple expression', () => {
const actual = { id: 1 };
const expected = { id: '$V === 1'};
const expected = { id: '$V === 1' };
expect(jsl.validate(actual, expected)).equals('');
});

it('object does not fulfil simple expression', () => {
const actual = { id: 1 };
const expected = { id: '$V > 1'};
const expected = { id: '$V > 1' };
expect(jsl.validate(actual, expected)).equals(`Json doesn't fulfil expression '$.id > 1'`);
expect(jsl.validate(actual, expected, { root_path: 'data' })).equals(`Json doesn't fulfil expression 'data.id > 1'`);
});

it('array fulfil simple expression', () => {
Expand All @@ -1063,6 +1065,7 @@ describe('JSON Like - Assert Expressions', () => {
const actual = [{ id: 1 }];
const expected = '$V.length > 1';
expect(jsl.validate(actual, expected)).equals(`Json doesn't fulfil expression '$.length > 1'`);
expect(jsl.validate(actual, expected, { root_path: 'data.users' })).equals(`Json doesn't fulfil expression 'data.users.length > 1'`);
});

it('object fulfil complex expression', () => {
Expand All @@ -1080,7 +1083,7 @@ describe('JSON Like - Assert Expressions', () => {
it('object fulfil simple custom includes expression', () => {
settings.setAssertExpressionStrategy({ includes: '$' });
const actual = { id: 1 };
const expected = { id: '$ === 1'};
const expected = { id: '$ === 1' };
expect(jsl.validate(actual, expected)).equals('');
});

Expand All @@ -1103,53 +1106,54 @@ describe('JSON Like - Assert Handlers', () => {

it('object fulfil simple assert', () => {
const actual = { id: 1 };
const expected = { id: '#number'};
const expected = { id: '#number' };
expect(jsl.validate(actual, expected)).equals('');
});

it('object does not fulfil simple assert', () => {
const actual = { id: '1' };
const expected = { id: '#number'};
const expected = { id: '#number' };
expect(jsl.validate(actual, expected)).equals(`Json doesn't fulfil assertion '#number' at '$.id'`);
expect(jsl.validate(actual, expected, { root_path: 'data' })).equals(`Json doesn't fulfil assertion '#number' at 'data.id'`);
});

it('object fulfil simple assert with args', () => {
const actual = { id: 1 };
const expected = { id: '#type:number'};
const expected = { id: '#type:number' };
expect(jsl.validate(actual, expected)).equals('');
});

it('simple assert does not exist', () => {
const actual = { id: '1' };
const expected = { id: '#number py'};
const expected = { id: '#number py' };
expect(jsl.validate(actual, expected)).equals(`Json doesn't have value '#number py' at '$.id' but found '1'`);
});

it('object fulfil simple custom starts with assert', () => {
settings.setAssertHandlerStrategy({ starts: '#$' });
const actual = { id: 1 };
const expected = { id: '#$number'};
const expected = { id: '#$number' };
expect(jsl.validate(actual, expected)).equals('');
});

it('object fulfil simple custom ends with assert', () => {
settings.setAssertHandlerStrategy({ ends: '#$' });
const actual = { id: 1 };
const expected = { id: 'number#$'};
const expected = { id: 'number#$' };
expect(jsl.validate(actual, expected)).equals('');
});

it('object fulfil simple custom starts & ends with assert', () => {
settings.setAssertHandlerStrategy({ starts: '#$', ends: '$#' });
const actual = { id: 1 };
const expected = { id: '#$number$#'};
const expected = { id: '#$number$#' };
expect(jsl.validate(actual, expected)).equals('');
});

it('simple assert satisfies only one strategy', () => {
settings.setAssertHandlerStrategy({ starts: '#', ends: '$#' });
const actual = { id: '1' };
const expected = { id: '#number'};
const expected = { id: '#number' };
expect(jsl.validate(actual, expected)).equals(`Json doesn't have value '#number' at '$.id' but found '1'`);
});

Expand Down
Loading