Skip to content

Commit

Permalink
fix(tests): make float column test deterministic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Samuel Roy authored and benjamin658 committed Mar 20, 2022
1 parent 1f4bcb4 commit a021235
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 2 additions & 0 deletions test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
8 changes: 2 additions & 6 deletions test/utils/prepareData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const data = [...Array(10).keys()].map((i) => ({
name: `user${i}`,
balance: getRandomFloat(1, 2),
balance: balances[i],
camelCaseColumn: setTimestamp(i),
photos: [
{
Expand Down

0 comments on commit a021235

Please sign in to comment.