Check sort order and unique IDs in API responses

Sorting bugs are quiet. The list still has items, every item still has the right fields, and the test that checks items[0] still passes. Meanwhile a user pages through orders and sees the same order twice, or misses one entirely.

Three checks catch most of these bugs: the order is right, nothing repeats, and the order is the same every time you ask.

Sorted by a field

For GET /orders?sort=-createdAt (newest first):

JavaScript
const items = pm.response.json().data;

pm.test('sorted by createdAt, newest first', () => {
  const outOfOrder = items
    .slice(1)
    .filter((item, i) => items[i].createdAt < item.createdAt)
    .map((item) => `${item.id} is newer than the item before it`);
  pm.expect(outOfOrder.slice(0, 3)).to.be.empty;
});

ISO 8601 timestamps in the same time zone compare correctly as strings. If yours mix offsets (+02:00 and Z), compare Date.parse(...) values instead.

Sorted by text

Text sorting depends on the database collation: is apple before Banana? Is é after e? Decide what your API promises, then test that:

JavaScript
const names = pm.response.json().data.map((x) => x.name);
const expected = [...names].sort((a, b) => a.localeCompare(b, 'en', { sensitivity: 'base' }));

pm.test('sorted by name, case-insensitive', () => {
  pm.expect(names).to.eql(expected);
});

No duplicates

JavaScript
const ids = pm.response.json().data.map((x) => x.id);

pm.test('no duplicate ids on the page', () => {
  const seen = new Set();
  const dupes = ids.filter((id) => (seen.has(id) ? true : (seen.add(id), false)));
  pm.expect(dupes).to.be.empty;
});

If it fails, the message lists the repeated ids.

No duplicates across pages

Duplicates often appear between pages, when the sort key isn't unique. Collect ids page by page in a variable:

JavaScript
// Each "List orders, page N" request
const seen = JSON.parse(pm.environment.get('seenOrderIds') ?? '[]');
const ids = pm.response.json().data.map((x) => x.id);

pm.test('no order repeats from an earlier page', () => {
  pm.expect(ids.filter((id) => seen.includes(id))).to.be.empty;
});

pm.environment.set('seenOrderIds', [...seen, ...ids]);
pm.environment.set('nextCursor', pm.response.json().nextCursor ?? '');

More on paging in how to test API pagination.

Same request, same order

If two records share a createdAt, the database may return them in any order. That's harmless on one page and a bug across pages: a record can slip from the end of page 1 to the start of page 2, or vanish.

Send the list twice and compare:

JavaScript
// First "List orders" request
pm.environment.set('firstOrder', pm.response.json().data.map((x) => x.id));
JavaScript
// Second, identical "List orders (again)" request
const first = JSON.parse(pm.environment.get('firstOrder'));
const second = pm.response.json().data.map((x) => x.id);

pm.test('same order on a repeated request', () => {
  pm.expect(second).to.eql(first);
});

The fix on the server side is a tie-breaker: ORDER BY created_at DESC, id DESC.

Make it a rule for a whole folder

If every list endpoint in a folder must have unique ids, put the uniqueness check in the folder's post-response script. It runs after every request in the folder, so new list endpoints get the check for free. See set auth, variables and scripts once.