How to check if the response is received in a sort order

@Akshatha_B_A Then the sorted responses are not matching I believe. Your Test is supposed to fail here. Even though the length are same, the values and index are not matching.

var resp = JSON.parse(responseBody);
var _ = require('lodash')
pm.test('order by Test', function() {
var expectedSortedOrder = _.orderBy(resp.objects, ['name'],['asc']);
console.log(expectedSortedOrder);
console.log(resp.objects);
pm.expect(resp.objects).to.eql(expectedSortedOrder);
});

Try checking the printed array’s in the console log.

Both expectedSortedOrder and resp.objects results are same.

But it is throwing the same error. AssertionError: expected [ Array(10) ] to deeply equal [ Array(10) ]

1 Like

@Akshatha_B_A: Can you please cross check the index of the variables again and possible share the snapshot here?

Hi,
This post on lodash really helped me.Thanks again. I have a small query…
image
In the above image I have given ‘branchName’ which is present in my response and the sorting order is ‘asc’ in the sortExpected.
I am validating response with sortExpected as

pm.expect(bodyData.data).to.eql(sortExpected) //test fails with lowercase(b) branchName but console.log prints the data rightly
My response
image

When I change branchName to uppercase (B) BranchName ,test is getting passed.

var sortExpected = _.orderBy(bodyData.data, [‘BranchName’],[‘asc’]); // test getting passed

var sortExpected = _.orderBy(bodyData.data, [‘BranchName’],[‘desc’]); //test should fail here when I changed b to Uppercase and desc because ASC is expected ,but test still getting passed.

All my other requests are working fine using lodash except for this one. Is it the problem with the request/response data or my script.

What is the reason for the above query? Does lodash have any case-sensitive things to consider? Or Did I understand something wrong? Please help me on this…

Hi @bpricilla @singhsivcan ,

Can any one help me on the below scenario. I need to check the Json Response received in the Title Ascending order or not (My title data has the Small and capital letters . and I need to compare the data once sorted with the original data. I have used the _.orderby but it is considering only Initial capital letter, if my title initial letter has the small letter it keeping at the end.

When I tried to compare I am getting the Deep equal error.

“list1”: [
{
“Test1”:1,
“Test2”:2,
“Test3”: 1221,
“ID”: “156456789”,
“title”: "A Test ",
“Test5”: “Test3”
},
{
“Test1”:1,
“Test2”:2,
“Test3”: 1221,
“ID”: “156456789”,
“title”: "B Test ",
“Test5”: “Test3”
},
{
“Test1”:1,
“Test2”:2,
“Test3”: 1221,
“ID”: “156456789”,
“title”: "b Test ",
“Test5”: “Test3”
},
{
“Test1”:1,
“Test2”:2,
“Test3”: 1221,
“ID”: “156456789”,
“title”: "AB Test ",
“Test5”: “Test3”
},

 {
         "Test1":1,
        "Test2":2,
        "Test3": 1221,
        "ID": "156456789",
        "title": "B S Test ",
        "Test5": "Test3"
    }

Hi bpricilla , I tried to print response objects in the console “console.log(resp.objects)” .
But it is showing as “undefined” in the console.

@kasunishankayapa1996 Welcome to the Community :partying_face:

The details you have provided is in-sufficient. Without the response it’s tough to predict/answer your questions :slight_smile:

It’s better to paste your response https://jsonpathfinder.com and understand it better. Please read my blog here to understand better and how to fix this undefined issue :blush:

@bpricilla @singhsivcan Hi! I’m trying to apply your suggestions that you gave, but I can’t. Could you guide me on the right path?

my response body:

{
    "data": [
        {
            "id": 1117,
            "acceptance": {
                "externalId": "33232"
            },
            "statusIdFrom": {
                "id": 1,
                "name": "Create"
            },
            "statusIdTo": {
                "id": 1,
                "name": "Create"
            },
            "createdAt": "2021-11-26T06:20:08.717Z",
            "updatedBy": {
                "id": 15,
                "name": "production",
                "fullname": "production",
                "departmentId": 1
            }
        },
        {
            "id": 1118,
            "acceptance": {
                "externalId": "33234"
            },
            "statusIdFrom": {
                "id": 1,
                "name": "Create"
            },
            "statusIdTo": {
                "id": 1,
                "name": "Create"
            },
            "createdAt": "2021-11-26T07:57:34.108Z",
            "updatedBy": {
                "id": 15,
                "name": "warehouse",
                "fullname": "warehouse",
                "departmentId": 1
            }
        },
        {
            "id": 1119,
            "acceptance": {
                "externalId": "33236"
            },
            "statusIdFrom": {
                "id": 1,
                "name": "Create"
            },
            "statusIdTo": {
                "id": 11,
                "name": "Delete"
            },
            "createdAt": "2021-11-26T07:57:35.238Z",
            "updatedBy": {
                "id": 15,
                "name": "fabric",
                "fullname": "fabric",
                "departmentId": 1
            }
        }
    ],
    "quantity": 5449
}

I’m trying to check the sorting by “updatedBy.name”

I tried to do this:

var resp = JSON.parse(responseBody);
console.log(resp.data[0].updatedBy)
var _ = require('lodash')

pm.test('Employee names are in sorted order', () => {

    var expectedSortedOrder = _.orderBy(resp.data[0].updatedBy, ['name'],['asc']);

    pm.expect(resp.data[0].updatedBy).to.eql(expectedSortedOrder);
});

I get an answer:

FAIL
Employee names are in sorted order | AssertionError: expected { id: 15, name: ‘production’, …(2) } to deeply equal [ 15, ‘production’, …(2) ]

@aerospace-administr7

Try the following… (which will fail as they are not in the correct order).

let resp = 
    {
        "data": [
            {
                "id": 1117,
                "acceptance": {
                    "externalId": "33232"
                },
                "statusIdFrom": {
                    "id": 1,
                    "name": "Create"
                },
                "statusIdTo": {
                    "id": 1,
                    "name": "Create"
                },
                "createdAt": "2021-11-26T06:20:08.717Z",
                "updatedBy": {
                    "id": 15,
                    "name": "production",
                    "fullname": "production",
                    "departmentId": 1
                }
            },
            {
                "id": 1118,
                "acceptance": {
                    "externalId": "33234"
                },
                "statusIdFrom": {
                    "id": 1,
                    "name": "Create"
                },
                "statusIdTo": {
                    "id": 1,
                    "name": "Create"
                },
                "createdAt": "2021-11-26T07:57:34.108Z",
                "updatedBy": {
                    "id": 15,
                    "name": "warehouse",
                    "fullname": "warehouse",
                    "departmentId": 1
                }
            },
            {
                "id": 1119,
                "acceptance": {
                    "externalId": "33236"
                },
                "statusIdFrom": {
                    "id": 1,
                    "name": "Create"
                },
                "statusIdTo": {
                    "id": 11,
                    "name": "Delete"
                },
                "createdAt": "2021-11-26T07:57:35.238Z",
                "updatedBy": {
                    "id": 15,
                    "name": "fabric",
                    "fullname": "fabric",
                    "departmentId": 1
                }
            }
        ],
        "quantity": 5449
    }

console.log(resp.data);

var _ = require('lodash');
let expectedSortedOrder = _.orderBy(resp.data, ['updatedBy.name'],['asc']);
console.log(expectedSortedOrder);
    

pm.test('Employee names are in sorted order', () => {
    pm.expect(resp.data).to.eql(expectedSortedOrder);
});
1 Like

@michaelderekjones Hi, thank you, you helped me a lot! It worked:) Only I changed a little bit to the whole answer “let resp = pm.response.json();”

let resp = pm.response.json();
console.log(resp.data);

var _ = require('lodash');
let expectedSortedOrder = _.orderBy(resp.data, ['acceptance.updatedBy'],['asc']);
console.log(expectedSortedOrder);
    

pm.test('Employee names are in sorted order', () => {
    pm.expect(resp.data).to.eql(expectedSortedOrder);
});

I still face a small problem when checking the sorting. Maybe you know how to do this case-insensitive check? I catch an error in the test when I get a capital letter. I tried to change it in the database, and yes, it’s really a capital letter.

@michaelderekjones upd: I tried using to.match

let resp = pm.response.json();
console.log(resp.data);

var _ = require('lodash');
let expectedSortedOrder = _.orderBy(resp.data, ['acceptance.updatedBy'],['asc']);
console.log(expectedSortedOrder);
    

pm.test('Employee names are in sorted order', () => {
    pm.expect(resp.data).to.match(expectedSortedOrder);
});

But I get an error
image

@aerospace-administr7

Expect / Should - Chai (chaijs.com)

Match uses REGEX (which is a function). Not an object which is what expectedSortedOrder will be.

lt’s the wrong assertion type for your use case. Please use eql.

@michaelderekjones I understood about the “match”, ok. The next thing I try is “toLowerCase”. But I also get an error and don’t understand how exactly this can be applied here. I need to exclude an error when the test falls due to the letter case. That’s what I do:

const response = pm.response.json();
console.log(response.data);
let result = response.data.toLowerCase();
console.log(result);

var _ = require('lodash');
let expectedSortedOrder = _.orderBy(result.data, ['acceptance.updatedBy'],['asc']);
console.log(expectedSortedOrder);
    

pm.test('Employee names are in sorted order', () => {

    pm.expect(result.data).to.eql(expectedSortedOrder)
    console.log(result.data);

});

@aerospace-administr7

This is a JavaScript query really.

toLowerCase works against strings.

JavaScript String toLowerCase() Method (w3schools.com)

response.data will be an object. Specifically an array object (an array is a special type of object).

You can only really use toLowerCase on the string elements within the array.

This can be done with a regex match or straight up JavaScript.

Have a look at the following conversation.

How can I test if a letter in a string is uppercase or lowercase using JavaScript? - Stack Overflow

The following appears to work and deal with numbers and special characters which are an extra consideration.

function isLowerCase(str) {
    return str == str.toLowerCase() && str != str.toUpperCase();
}

let strings = ["abc", "ABC", "Abc", "aBC", "123", "$AB"];

strings.forEach(string => 
    pm.test(`Verifying ${string} is all lowercase `, () => {
        pm.expect(isLowerCase(string)).to.eql(true);
    })
);

image

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.