From 081bbb056ea2369a901a24bec756cee931999d2f Mon Sep 17 00:00:00 2001 From: Timo Erdelt Date: Thu, 21 Mar 2024 15:40:36 +0100 Subject: [PATCH] test: add e2e tests for PairLiquidityInfoHistoryDbService and PairLiquidityInfoHistoryErrorDbService --- ...uidity-info-history-db.service.e2e-spec.ts | 215 ++++++++++++++++++ ...-info-history-error-db.service.e2e-spec.ts | 116 ++++++++++ test/jest-e2e.json | 2 +- 3 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 src/database/pair-liquidity-info-history-db.service.e2e-spec.ts create mode 100644 src/database/pair-liquidity-info-history-error-db.service.e2e-spec.ts diff --git a/src/database/pair-liquidity-info-history-db.service.e2e-spec.ts b/src/database/pair-liquidity-info-history-db.service.e2e-spec.ts new file mode 100644 index 0000000..acec1c7 --- /dev/null +++ b/src/database/pair-liquidity-info-history-db.service.e2e-spec.ts @@ -0,0 +1,215 @@ +import { PairLiquidityInfoHistoryDbService } from './pair-liquidity-info-history-db.service'; +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from './prisma.service'; +import { Pair, PairLiquidityInfoHistory, Token } from '@prisma/client'; +import { OrderQueryEnum } from '../dto'; +import { ContractAddress } from '../lib/utils'; + +const token1: Token = { + id: 1, + address: 'ct_token1', + symbol: '1', + name: '1', + decimals: 18, + malformed: false, + noContract: false, + listed: false, +}; +const token2: Token = { + id: 2, + address: 'ct_token2', + symbol: '2', + name: '2', + decimals: 18, + malformed: false, + noContract: false, + listed: false, +}; +const token3: Token = { + id: 3, + address: 'ct_token3', + symbol: '3', + name: '3', + decimals: 18, + malformed: false, + noContract: false, + listed: false, +}; +const pair1: Pair = { + id: 1, + address: 'ct_pair1', + t0: 1, + t1: 2, + synchronized: true, +}; +const pair2: Pair = { + id: 2, + address: 'ct_pair2', + t0: 2, + t1: 3, + synchronized: true, +}; +const pair3: Pair = { + id: 3, + address: 'ct_pair4', + t0: 2, + t1: 3, + synchronized: true, +}; +const historyEntry1: PairLiquidityInfoHistory = { + id: 1, + pairId: 1, + totalSupply: '2000148656239820912122563', + reserve0: '950875688379385634428666', + reserve1: '4208476309359648851631167', + height: 100001, + microBlockHash: 'mh_entry1', + microBlockTime: 1000000000001n, + updatedAt: new Date(), +}; +const historyEntry2: PairLiquidityInfoHistory = { + id: 2, + pairId: 1, + totalSupply: '9954575303087659158151', + reserve0: '20210309618736130321327', + reserve1: '4903471477408475598460', + height: 200002, + microBlockHash: 'mh_entry2', + microBlockTime: 2000000000002n, + updatedAt: new Date(), +}; +const historyEntry3: PairLiquidityInfoHistory = { + id: 3, + pairId: 2, + totalSupply: '56931443813890767374824', + reserve0: '20556919390913460010617', + reserve1: '157691178959228289022449', + height: 300003, + microBlockHash: 'mh_entry3', + microBlockTime: 3000000000003n, + updatedAt: new Date(), +}; +const historyEntry4: PairLiquidityInfoHistory = { + id: 4, + pairId: 2, + totalSupply: '56931443813890767374824', + reserve0: '20556919390913460010617', + reserve1: '157691178959228289022449', + height: 300003, + microBlockHash: 'mh_entry4', + microBlockTime: 4000000000004n, + updatedAt: new Date(), +}; + +describe('PairLiquidityInfoHistoryDbService', () => { + let service: PairLiquidityInfoHistoryDbService; + let prismaService: PrismaService; + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PairLiquidityInfoHistoryDbService, PrismaService], + }).compile(); + + service = module.get( + PairLiquidityInfoHistoryDbService, + ); + prismaService = module.get(PrismaService); + + await prismaService.token.createMany({ data: [token1, token2, token3] }); + await prismaService.pair.createMany({ data: [pair1, pair2, pair3] }); + await prismaService.pairLiquidityInfoHistory.createMany({ + data: [historyEntry1, historyEntry2, historyEntry3, historyEntry4], + }); + }); + + afterEach(async () => { + await prismaService.pairLiquidityInfoHistory.deleteMany(); + await prismaService.pair.deleteMany(); + await prismaService.token.deleteMany(); + }); + + describe('getAll', () => { + it('should return all entries', async () => { + const result = await service.getAll(100, 0, OrderQueryEnum.asc); + expect(result.map((e) => e.id)).toEqual([1, 2, 3, 4]); + }); + + it('should return return entries with limit, offset and order', async () => { + const result = await service.getAll(2, 1, OrderQueryEnum.desc); + expect(result.map((e) => e.id)).toEqual([3, 2]); + }); + + it('should correctly filter by pair address', async () => { + const result = await service.getAll( + 100, + 0, + OrderQueryEnum.asc, + pair1.address as ContractAddress, + undefined, + undefined, + undefined, + ); + expect(result.map((e) => e.id)).toEqual([1, 2]); + }); + + it('should correctly filter by height', async () => { + const result = await service.getAll( + 100, + 0, + OrderQueryEnum.asc, + undefined, + 300003, + undefined, + undefined, + ); + expect(result.map((e) => e.id)).toEqual([3, 4]); + }); + + it('should correctly return entries newer or equal fromBlockTime', async () => { + const result = await service.getAll( + 100, + 0, + OrderQueryEnum.asc, + undefined, + undefined, + 2000000000002n, + undefined, + ); + expect(result.map((e) => e.id)).toEqual([2, 3, 4]); + }); + + it('should correctly return entries older or equal toBlockTime', async () => { + const result = await service.getAll( + 100, + 0, + OrderQueryEnum.desc, + undefined, + undefined, + undefined, + 3000000000003n, + ); + expect(result.map((e) => e.id)).toEqual([3, 2, 1]); + }); + }); + + describe('getLastlySyncedBlockByPairId', () => { + it('should correctly return the last synced block for a given pairId', async () => { + const result = await service.getLastlySyncedBlockByPairId(1); + expect(result?.microBlockTime).toEqual(2000000000002n); + }); + }); + + describe('getWithinHeightSorted', () => { + it('should correctly return all entries greater or equal a given height limit sorted by microBlockTime ascending', async () => { + const result = await service.getWithinHeightSorted(200002); + expect(result.map((e) => e.id)).toEqual([2, 3, 4]); + }); + }); + + describe('deleteFromMicroBlockTime', () => { + it('should correctly delete all entries newer or equal a given block time', async () => { + await service.deleteFromMicroBlockTime(3000000000003n); + const result = await prismaService.pairLiquidityInfoHistory.findMany(); + expect(result.map((e) => e.id)).toEqual([1, 2]); + }); + }); +}); diff --git a/src/database/pair-liquidity-info-history-error-db.service.e2e-spec.ts b/src/database/pair-liquidity-info-history-error-db.service.e2e-spec.ts new file mode 100644 index 0000000..fe6269a --- /dev/null +++ b/src/database/pair-liquidity-info-history-error-db.service.e2e-spec.ts @@ -0,0 +1,116 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from './prisma.service'; +import { Pair, PairLiquidityInfoHistoryError, Token } from '@prisma/client'; +import { PairLiquidityInfoHistoryErrorDbService } from './pair-liquidity-info-history-error-db.service'; + +const token1: Token = { + id: 1, + address: 'ct_token1', + symbol: '1', + name: '1', + decimals: 18, + malformed: false, + noContract: false, + listed: false, +}; +const token2: Token = { + id: 2, + address: 'ct_token2', + symbol: '2', + name: '2', + decimals: 18, + malformed: false, + noContract: false, + listed: false, +}; +const pair1: Pair = { + id: 1, + address: 'ct_pair1', + t0: 1, + t1: 2, + synchronized: true, +}; +const pair2: Pair = { + id: 2, + address: 'ct_pair2', + t0: 1, + t1: 2, + synchronized: true, +}; +const errorEntry1: PairLiquidityInfoHistoryError = { + id: 1, + pairId: 1, + microBlockHash: '', + error: 'error_1', + timesOccurred: 1, + createdAt: new Date('2024-01-01 12:00:00.000'), + updatedAt: new Date('2024-01-01 12:00:00.000'), +}; +const errorEntry2: PairLiquidityInfoHistoryError = { + id: 2, + pairId: 1, + microBlockHash: 'mh_1', + error: 'error_2', + timesOccurred: 1, + createdAt: new Date('2024-01-01 12:00:00.000'), + updatedAt: new Date('2024-01-01 12:00:00.000'), +}; + +describe('PairLiquidityInfoHistoryErrorDbService', () => { + let service: PairLiquidityInfoHistoryErrorDbService; + let prismaService: PrismaService; + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PairLiquidityInfoHistoryErrorDbService, PrismaService], + }).compile(); + + service = module.get( + PairLiquidityInfoHistoryErrorDbService, + ); + prismaService = module.get(PrismaService); + + await prismaService.token.createMany({ data: [token1, token2] }); + await prismaService.pair.createMany({ data: [pair1, pair2] }); + await prismaService.pairLiquidityInfoHistoryError.createMany({ + data: [errorEntry1, errorEntry2], + }); + jest.useFakeTimers().setSystemTime(new Date('2024-01-01 17:59:00.000')); + }); + + afterEach(async () => { + await prismaService.pairLiquidityInfoHistoryError.deleteMany(); + await prismaService.pair.deleteMany(); + await prismaService.token.deleteMany(); + jest.useRealTimers(); + }); + + describe('getErrorByPairIdAndMicroBlockHashWithinHours', () => { + it('should correctly return an error within a recent given time window in hours by pairId', async () => { + jest.useFakeTimers().setSystemTime(new Date('2024-01-01 17:59:00.000')); + const result = await service.getErrorByPairIdAndMicroBlockHashWithinHours( + 1, + '', + 6, + ); + expect(result?.id).toEqual(1); + }); + + it('should correctly return an error within a recent given time window in hours by pairId and microBlockHash', async () => { + const result = await service.getErrorByPairIdAndMicroBlockHashWithinHours( + 1, + 'mh_1', + 6, + ); + expect(result?.id).toEqual(2); + }); + + it('should not return errors older than the given time window in hours', async () => { + const result = await service.getErrorByPairIdAndMicroBlockHashWithinHours( + 1, + '', + 5, + ); + expect(result).toBe(null); + }); + }); +}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json index e9d912f..93400cd 100644 --- a/test/jest-e2e.json +++ b/test/jest-e2e.json @@ -1,6 +1,6 @@ { "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", + "rootDir": "../", "testEnvironment": "node", "testRegex": ".e2e-spec.ts$", "transform": {