# Testing if a date sorting is correct in Postman

**URL:** <https://community.postman.com/t/testing-if-a-date-sorting-is-correct-in-postman/5293>\
**Category:** Help Hub\
**Tags:** newman-project, tests\
**Created:** [May 6, 2019, 7:38am UTC](https://community.postman.com/t/testing-if-a-date-sorting-is-correct-in-postman/5293 "2019-05-06T07:38:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkolomanski](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mkolomanski](https://community.postman.com/u/mkolomanski)\
**Post date:** [May 6, 2019, 7:38am UTC](https://community.postman.com/t/testing-if-a-date-sorting-is-correct-in-postman/5293/1 "2019-05-06T07:38:33Z")

</div>

Hello,  
I send a request which has a random date.

In pre-request Script:

pm.globals.set(‘hours’, \_.random(10,23));  
pm.globals.set(‘minutes’, \_.random(10,59));  
pm.globals.set(‘seconds’, \_.random(10,59));  
pm.globals.set(‘day’, \_.random(10,30));  
pm.globals.set(‘month’, \_.random(10,12));  
pm.globals.set(‘year’, \_.random(2010,2019));

in body:

{  
“eventtype”: “{{eventtype}}”,  
“severity” : “{{severity}}”,  
“description” : “{{event\_description}}”,  
“settingtime”: “{{year}}-{{month}}-{{day}}T{{hours}}:{{minutes}}:{{seconds}}”  
}

Example output:

{  
“eventtype”: “manual”,  
“severity”: “warning”,  
“description”: “fwquovfwwu”,  
“settingtime”: “2018-12-27T11:30:53”  
}

That request generate an event for me. After generating plenty of that kind of events I send another request with a TimeBegin parameter which should get every event for me which has a parameter “settingtime” greater than my parameter TimeBegin.

For example:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/5/256ec492eb9e3cd171fb3cbf08515d9d8a38ad3c.png)  
TimeBegin = 2016-11-18T11:41:51 and request should return every event which has a “newer” setting\_time parameter than this.

I want to write a test which automatically checks if every generated event which had parameter setting\_time greater than TimeBegin was returned by the response of the server. Moreover, checks if response is sorted correctly (in rising mode).

I would be very grateful for any kind of help. I use native Postman and Newman.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [May 6, 2019, 11:00am UTC](https://community.postman.com/t/testing-if-a-date-sorting-is-correct-in-postman/5293/2 "2019-05-06T11:00:11Z")

</div>

We can make use of `moment.js` and `loadsh` which are built into postman (available in the scripts).  
Docs: [https://learning.getpostman.com/docs/postman/scripts/postman\_sandbox\_api\_reference/](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/)

MomentJS docs: [https://momentjs.com/docs/#/query/is-before/](https://momentjs.com/docs/#/query/is-before/)

**Step 1:**  
So we need `timeBegin`, so in the request where you generate the time begin, you should store that in an environment variable.  
Instead of breaking the time into multiple variables you can directly store it as one single variable and also use it in the request body or I am sure you know how to get around that problem.

**Step 2:**  
I have hardcoded the responseBody but you have to replace it with what the comment says.  
Here’s the test script that’s going to help you out:

Test Script:

```javascript
_ = require("lodash");

let moment = require('moment');
// Hardcoding the response body, replace this with
// responseBody = pm.response.json();
let responseBody = {
    rows: [
        {
            "setting_time": "2018-12-27T11:30:53"
        },
        {
            "setting_time": "2018-12-27T11:30:53"
        },
        {
            "setting_time": "2018-12-27T11:32:51"
        },
        {
            "setting_time": "2018-12-27T11:32:53"
        },
    ] 
}

pm.test('Are all dates after time begin and in correct sorted order', function () {
    let previousDate = null,
        timeBegin = pm.environment.get('timeBegin') || "2018-12-27T11:30:52",
        isDateAfterTimeBegin, // Will be used to check if date if time begin is before the given setting_time
        isBreakingSortOrder; // Will be used to check if the time is breaking the sort order

    areDatesInvalid = _.some(responseBody.rows, (row) => {
        isDateAfterTimeBegin = moment(timeBegin).isBefore(row.setting_time);

        isBreakingSortOrder = previousDate ? moment(previousDate).isBefore(row.setting_time) : false;

        previousDate = row.setting_time; // Storing this which will be used in next iteration to check if the sort order is not being broken

        return !isDateAfterTimeBegin && !isBreakingSortOrder;
    });

    pm.expect(areDatesInvalid).to.be.false;
});

```

_ **Explanation:** _  
I am checking if any of the dates are invalid - each date is always after the `timeBegin`.  
Also, I am checking if any of the dates is breaking the sort order i.e each `setting_time` should be more than the `previousDate`.  
For the first iteration, previousDate is null so that’s why there’s a conditional check in there.

Finally, if any of the date is invalid then `_.some` will return true that yeah some date is actually invalid and your test will fail.  
Otherwise, the test will pass.

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [May 23, 2019, 11:43am UTC](https://community.postman.com/t/testing-if-a-date-sorting-is-correct-in-postman/5293/3 "2019-05-23T11:43:30Z")

</div>

I was having a bit of a geek moment and thought I might be able to do a solution in straight javascript… this is what I came up with (borrowing @singhsivcan’s general approach)

```javascript
let response_Body = {
    rows: [
        {"setting_time": "2018-12-27T11:30:53"},
        {"setting_time": "2018-12-27T11:30:53"},
        {"setting_time": "2018-12-27T11:32:51"},
        {"setting_time": "2018-12-27T11:32:53"}
    ] 
};
// Check if there are any rows with time less than timeBegin
const timeBegin = new Date("2018-12-27T11:30:51");
const anyRowsBeforeTimeBegin = response_Body.rows.find(
   row => new Date(row.setting_time) < timeBegin
);
pm.test("no RowsBeforeTimeBegin", () => {
    pm.expect(anyRowsBeforeTimeBegin).to.be.undefined;
});

// Check the order is correct
const rowsAsReceived = response_Body.rows; 
// Create a copy of the array, and sort it by date
const sortedRows = Array.from(rowsAsReceived).sort(
    (a, b) => new Date(a.setting_time) - new Date(b.setting_time)
);
// check if the arrays are both still in the same order
const orderHasNotChanged = sortedRows.every(
   (a,i) => a.setting_time === rowsAsReceived[i].setting_time
);
pm.test("orderHasNotChanged", () => {
    pm.expect(orderHasNotChanged).to.be.true;
});

```

Running that gives  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/8/381a61bd8ff98e61750a212d4915609af6c272e4.png)

If I change the input slightly

```javascript
let response_Body = {
    rows: [
        {"setting_time": "2018-12-27T11:30:53"},
        {"setting_time": "2018-12-27T11:30:50"},//before timeBegin & out of order
        {"setting_time": "2018-12-27T11:32:51"},
        {"setting_time": "2018-12-27T11:32:53"}
    ] 
};

```

It does this

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/f/0f9324d64a9f23c9ac4b0c99da5c75b1eaa25c95.png)  
(the error messages could be more helpful)

**Links for more details**

- [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Find_an_object_in_an_array_by_one_of_its_properties)
- [Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description)
- [Array.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
- [Arrow Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Basic_syntax)
