Skip to content

Commit

Permalink
test: e2e tests for v2 dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Apr 17, 2024
1 parent 0b68c0d commit 1a44811
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrismaService } from '@/database/prisma.service';
export class PairLiquidityInfoHistoryV2ErrorDbService {
constructor(private prisma: PrismaService) {}

getErrorByPairIdAndMicroBlockHashWithinHours(
getErrorWithinHours(
pairId: number,
microBlockHash: string,
transactionHash: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PairLiquidityInfoHistoryV2DbService } from '@/database/pair-liquidity-i
import { PairLiquidityInfoHistoryV2ErrorDbService } from '@/database/pair-liquidity-info-history-error/pair-liquidity-info-history-v2-error-db.service';
import { bigIntToDecimal, decimalToBigInt } from '@/lib/utils';

enum EventType {
export enum EventType {
Sync = 'Sync',
SwapTokens = 'SwapTokens',
PairMint = 'PairMint',
Expand Down Expand Up @@ -69,7 +69,7 @@ export class PairLiquidityInfoHistoryImporterV2Service {
try {
// If an error occurred for this pair recently, skip pair
const error =
await this.pairLiquidityInfoHistoryErrorDb.getErrorByPairIdAndMicroBlockHashWithinHours(
await this.pairLiquidityInfoHistoryErrorDb.getErrorWithinHours(
pairWithTokens.id,
'',
'',
Expand Down Expand Up @@ -219,7 +219,7 @@ export class PairLiquidityInfoHistoryImporterV2Service {

// If an error occurred for this log recently, skip block
const error =
await this.pairLiquidityInfoHistoryErrorDb.getErrorByPairIdAndMicroBlockHashWithinHours(
await this.pairLiquidityInfoHistoryErrorDb.getErrorWithinHours(
pairWithTokens.id,
current.log.block_hash,
current.log.call_tx_hash,
Expand Down
220 changes: 220 additions & 0 deletions test/e2e/pair-liquidity-info-history-v2-db.service.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Pair, PairLiquidityInfoHistoryV2, Token } from '@prisma/client';
import { Decimal } from '@prisma/client/runtime/library';

import { PairLiquidityInfoHistoryV2DbService } from '@/database/pair-liquidity-info-history/pair-liquidity-info-history-v2-db.service';
import { PrismaService } from '@/database/prisma.service';
import { EventType } from '@/tasks/pair-liquidity-info-history-importer/pair-liquidity-info-history-importer-v2.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 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: PairLiquidityInfoHistoryV2 = {
id: 1,
pairId: 1,
eventType: EventType.PairMint,
reserve0: new Decimal(1000),
reserve1: new Decimal(1000),
deltaReserve0: new Decimal(1000),
deltaReserve1: new Decimal(1000),
fiatPrice: new Decimal(0),
height: 100001,
microBlockHash: 'mh_entry1',
microBlockTime: 1000000000001n,
transactionHash: 'th_entry1',
transactionIndex: 100001n,
logIndex: 1,
createdAt: new Date(),
updatedAt: new Date(),
};
const historyEntry2: PairLiquidityInfoHistoryV2 = {
id: 2,
pairId: 1,
eventType: EventType.SwapTokens,
reserve0: new Decimal(1050),
reserve1: new Decimal(950),
deltaReserve0: new Decimal(50),
deltaReserve1: new Decimal(-50),
fiatPrice: new Decimal(0),
height: 200002,
microBlockHash: 'mh_entry2',
microBlockTime: 2000000000002n,
transactionHash: 'th_entry2',
transactionIndex: 200002n,
logIndex: 1,
createdAt: new Date(),
updatedAt: new Date(),
};
const historyEntry3: PairLiquidityInfoHistoryV2 = {
id: 3,
pairId: 2,
eventType: EventType.PairMint,
reserve0: new Decimal(1000),
reserve1: new Decimal(1000),
deltaReserve0: new Decimal(1000),
deltaReserve1: new Decimal(1000),
fiatPrice: new Decimal(0),
height: 300003,
microBlockHash: 'mh_entry3',
microBlockTime: 3000000000003n,
transactionHash: 'th_entry3',
transactionIndex: 300003n,
logIndex: 1,
createdAt: new Date(),
updatedAt: new Date(),
};
const historyEntry4: PairLiquidityInfoHistoryV2 = {
id: 4,
pairId: 2,
eventType: EventType.SwapTokens,
reserve0: new Decimal(1050),
reserve1: new Decimal(950),
deltaReserve0: new Decimal(50),
deltaReserve1: new Decimal(-50),
fiatPrice: new Decimal(0),
height: 300003,
microBlockHash: 'mh_entry4',
microBlockTime: 3000000000003n,
transactionHash: 'th_entry4',
transactionIndex: 300003n,
logIndex: 2,
createdAt: new Date(),
updatedAt: new Date(),
};

describe('PairLiquidityInfoHistoryV2DbService', () => {
let service: PairLiquidityInfoHistoryV2DbService;
let prismaService: PrismaService;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PairLiquidityInfoHistoryV2DbService, PrismaService],
}).compile();

service = module.get<PairLiquidityInfoHistoryV2DbService>(
PairLiquidityInfoHistoryV2DbService,
);
prismaService = module.get<PrismaService>(PrismaService);
});

beforeEach(async () => {
await prismaService.token.createMany({ data: [token1, token2, token3] });
await prismaService.pair.createMany({ data: [pair1, pair2, pair3] });
await prismaService.pairLiquidityInfoHistoryV2.createMany({
data: [historyEntry1, historyEntry2, historyEntry3, historyEntry4],
});
});

afterEach(async () => {
await prismaService.pairLiquidityInfoHistoryV2.deleteMany();
await prismaService.pair.deleteMany();
await prismaService.token.deleteMany();
});

afterAll(async () => {
await prismaService.$disconnect();
});

describe('upsert', () => {
it('should correctly upsert an existing entry', async () => {
const updatedEntry = {
pairId: 1,
eventType: EventType.PairBurn,
reserve0: new Decimal(500),
reserve1: new Decimal(500),
deltaReserve0: new Decimal(-500),
deltaReserve1: new Decimal(-500),
fiatPrice: new Decimal(0),
height: 200002,
microBlockHash: 'mh_entry2',
microBlockTime: 2000000000002n,
transactionHash: 'th_entry2',
transactionIndex: 200002n,
logIndex: 1,
};
await service.upsert(updatedEntry);
const entry = await prismaService.pairLiquidityInfoHistoryV2.findFirst({
where: { id: 2 },
});
expect(entry?.reserve0).toEqual(new Decimal(500));
});

it('should correctly insert an new entry', async () => {
const newEntry = {
pairId: 1,
eventType: EventType.PairBurn,
reserve0: new Decimal(500),
reserve1: new Decimal(500),
deltaReserve0: new Decimal(-500),
deltaReserve1: new Decimal(-500),
fiatPrice: new Decimal(0),
height: 500005,
microBlockHash: 'mh_entry5',
microBlockTime: 5000000000005n,
transactionHash: 'th_entry5',
transactionIndex: 500005n,
logIndex: 1,
};
await service.upsert(newEntry);
const entry = await service.getLastlySyncedLogByPairId(1);
expect(entry?.microBlockHash).toEqual('mh_entry5');
});
});

describe('getLastlySyncedLogByPairId', () => {
it('should correctly return the last synced log for a given pairId', async () => {
const result1 = await service.getLastlySyncedLogByPairId(1);
const result2 = await service.getLastlySyncedLogByPairId(2);
expect(result1?.id).toEqual(2);
expect(result2?.id).toEqual(4);
});
});
});
116 changes: 116 additions & 0 deletions test/e2e/pair-liquidity-info-history-v2-error-db.service.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Test, TestingModule } from '@nestjs/testing';

Check warning on line 1 in test/e2e/pair-liquidity-info-history-v2-error-db.service.e2e-spec.ts

View workflow job for this annotation

GitHub Actions / Lint

Run autofix to sort these imports!
import { Pair, PairLiquidityInfoHistoryV2Error, Token } from '@prisma/client';

import { PrismaService } from '@/database/prisma.service';
import { PairLiquidityInfoHistoryV2ErrorDbService } from '@/database/pair-liquidity-info-history-error/pair-liquidity-info-history-v2-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: PairLiquidityInfoHistoryV2Error = {
id: 1,
pairId: 1,
microBlockHash: '',
transactionHash: '',
logIndex: -1,
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: PairLiquidityInfoHistoryV2Error = {
id: 2,
pairId: 1,
microBlockHash: 'mh_1',
transactionHash: 'th_1',
logIndex: 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('PairLiquidityInfoHistoryV2ErrorDbService', () => {
let service: PairLiquidityInfoHistoryV2ErrorDbService;
let prismaService: PrismaService;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PairLiquidityInfoHistoryV2ErrorDbService, PrismaService],
}).compile();

service = module.get<PairLiquidityInfoHistoryV2ErrorDbService>(
PairLiquidityInfoHistoryV2ErrorDbService,
);
prismaService = module.get<PrismaService>(PrismaService);
});

beforeEach(async () => {
await prismaService.token.createMany({ data: [token1, token2] });
await prismaService.pair.createMany({ data: [pair1, pair2] });
await prismaService.pairLiquidityInfoHistoryV2Error.createMany({
data: [errorEntry1, errorEntry2],
});
jest.useFakeTimers().setSystemTime(new Date('2024-01-01 17:59:00.000'));
});

afterEach(async () => {
await prismaService.pairLiquidityInfoHistoryV2Error.deleteMany();
await prismaService.pair.deleteMany();
await prismaService.token.deleteMany();
jest.useRealTimers();
});

afterAll(async () => {
await prismaService.$disconnect();
});

describe('getErrorWithinHours', () => {
it('should correctly return an error within a recent given time window in hours on pair basis', async () => {
jest.useFakeTimers().setSystemTime(new Date('2024-01-01 17:59:00.000'));
const result = await service.getErrorWithinHours(1, '', '', -1, 6);
expect(result?.id).toEqual(1);
});

it('should correctly return an error within a recent given time window in hours on log basis', async () => {
const result = await service.getErrorWithinHours(1, 'mh_1', 'th_1', 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.getErrorWithinHours(1, '', '', -1, 5);
expect(result).toBe(null);
});
});
});

0 comments on commit 1a44811

Please sign in to comment.