From a021235cd95e415e2732efac8f89adf5f4258448 Mon Sep 17 00:00:00 2001 From: Samuel Roy Date: Sun, 20 Mar 2022 09:09:45 +0100 Subject: [PATCH] fix(tests): make float column test deterministic Sometimes the test case would fail due to the random balances chosen for each user in prepareData. The test is now deterministic by using an ordered fixed list of balances. --- test/pagination.ts | 2 ++ test/utils/prepareData.ts | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/pagination.ts b/test/pagination.ts index 9ec6146..448ecc2 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -91,6 +91,8 @@ describe('TypeORM cursor-based pagination test', () => { expect(firstPageResult.data[1].id).to.not.eq(nextPageResult.data[0].id); expect(firstPageResult.data[1].balance).to.be.above(nextPageResult.data[0].balance); + expect(firstPageResult.data[0].id).to.eq(10); + expect(nextPageResult.data[0].id).to.eq(8); }); it('should return entities with given order', async () => { diff --git a/test/utils/prepareData.ts b/test/utils/prepareData.ts index 4c3081c..2b81dc1 100644 --- a/test/utils/prepareData.ts +++ b/test/utils/prepareData.ts @@ -9,16 +9,12 @@ function setTimestamp(i: number): Date { return now; } -function getRandomFloat(min: number, max: number): number { - const str = (Math.random() * (max - min) + min).toFixed(2); - - return parseFloat(str); -} +const balances = [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2]; export async function prepareData(): Promise { const data = [...Array(10).keys()].map((i) => ({ name: `user${i}`, - balance: getRandomFloat(1, 2), + balance: balances[i], camelCaseColumn: setTimestamp(i), photos: [ {