0

My response body looks like this (I'm only showing part of it, in fact it consists of more than 5k id values). I need to check if the ascending sort by key is correct updatedBy.name . But when I encounter a capital letter my test fails.

{
    "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": "fabric",
                "fullname": "fabric",
                "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": "production",
                "fullname": "production",
                "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": "warehouse",
                "fullname": "warehouse",
                "departmentId": 1
            }
        }
    ],
    "quantity": 5449
}

This check works until it encounters a capital letter in the name

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

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

pm.test('sort column', () => {
    pm.expect(resp.data).to.eql(expectedSortedOrder);
});

I tried using toLowerCase, but with it I manage to convert only a specific key number, and not the entire body of the response. And it won't help when checking sorting.

I tried this option, but I came across the fact that my test crashes when it sees empty "null" values

pm.test("sort column", () => {
    const response = pm.response.json();

    // Extract all ids in an array
    const art = response.data.map(item => item.updatedBy.name);
    
    // Check that all are ascending
    function isAscending(a) {
        return a.every((x, i) => {
            return i === 0 || x >= a[i-1];
        });
    }
    pm.expect(isAscending(art)).to.be.true;
});

I will be grateful for any help. Thanks

sachin
  • 1,075
  • 1
  • 7
  • 11
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – nullptr Apr 28 '23 at 06:14
  • So you mean `let expectedSortedOrder = _.orderBy(resp.data, [item => item.updatedBy.name.toLowerCase()],['asc']);` didnt work? Also for your `null` issue, have you tried using `Array.filter` to filter out null values? – callback Apr 28 '23 at 06:15
  • let expectedSortedOrder = _.orderBy(resp.data, [item => item.updatedBy.name.toLowerCase()],['asc']); It helped, thank you very much! I probably tried to apply it incorrectly. – Dmitriy Petrov Apr 28 '23 at 06:35
  • I didn't know about Array.filter, I'll try to use it to get the result, thanks for the tip – Dmitriy Petrov Apr 28 '23 at 06:39

0 Answers0