Skip to content

Commit

Permalink
test: add unit tests for PairLiquidityInfoHistoryController
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Mar 21, 2024
1 parent 16f4dd6 commit 1ff80e6
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 1 deletion.
228 changes: 228 additions & 0 deletions src/api/pair-liquidity-info-history/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import { PairLiquidityInfoHistoryController } from './controller';
import { Test, TestingModule } from '@nestjs/testing';
import { PairLiquidityInfoHistoryService } from './service';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { Pair, PairLiquidityInfoHistory } from '@prisma/client';
import { OrderQueryEnum } from '../../dto';

const mockPairLiquidityInfoHistoryService = {
getAllHistoryEntries: jest.fn(),
};
describe('PairLiquidityInfoHistoryController', () => {
let app: INestApplication;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PairLiquidityInfoHistoryController],
providers: [
{
provide: PairLiquidityInfoHistoryService,
useValue: mockPairLiquidityInfoHistoryService,
},
],
}).compile();

app = module.createNestApplication();
await app.init();
});

describe('GET /history/liquidity', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should return history entries and use default values for empty params', async () => {
// Mocks
const historyEntry1: { pair: Pair } & PairLiquidityInfoHistory = {
id: 1,
pairId: 1,
totalSupply: '2000148656239820912122563',
reserve0: '950875688379385634428666',
reserve1: '4208476309359648851631167',
height: 912485,
microBlockHash: 'mh_Tx43Gh3acudUNSUWihPcV1Se4XcoFK3aUFAtFZk2Z4Zv7igZs',
microBlockTime: 1709027642807n,
updatedAt: new Date('2024-03-20 17:04:51.625'),
pair: {
id: 1,
address: 'ct_efYtiwDg4YZxDWE3iLPzvrjb92CJPvzGwriv4ZRuvuTDMNMb9',
t0: 15,
t1: 5,
synchronized: true,
},
};

const historyEntry2: { pair: Pair } & PairLiquidityInfoHistory = {
id: 2,
pairId: 3,
totalSupply: '9954575303087659158151',
reserve0: '20210309618736130321327',
reserve1: '4903471477408475598460',
height: 707395,
microBlockHash: 'mh_2dUTfmwFc2ymeroB534giVwEvsa8d44Vf8SXtvy6GeHjdgQoHj',
microBlockTime: 1671708830503n,
updatedAt: new Date('2024-03-20 12:16:49.065'),
pair: {
id: 3,
address: 'ct_22iY9F7hng23gN8awi4aGnLy54YSR41wztbqgQCquuLYvTiGcm',
t0: 17,
t1: 22,
synchronized: true,
},
};

mockPairLiquidityInfoHistoryService.getAllHistoryEntries.mockResolvedValue(
[historyEntry1, historyEntry2],
);

// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledWith(
100,
0,
OrderQueryEnum.asc,
undefined,
undefined,
undefined,
undefined,
);
expect(result.status).toBe(200);
expect(result.body).toEqual([
{
pairAddress: historyEntry1.pair.address,
liquidityInfo: {
totalSupply: historyEntry1.totalSupply,
reserve0: historyEntry1.reserve0,
reserve1: historyEntry1.reserve1,
},
height: historyEntry1.height,
microBlockHash: historyEntry1.microBlockHash,
microBlockTime: historyEntry1.microBlockTime.toString(),
},
{
pairAddress: historyEntry2.pair.address,
liquidityInfo: {
totalSupply: historyEntry2.totalSupply,
reserve0: historyEntry2.reserve0,
reserve1: historyEntry2.reserve1,
},
height: historyEntry2.height,
microBlockHash: historyEntry2.microBlockHash,
microBlockTime: historyEntry2.microBlockTime.toString(),
},
]);
});

it('should parse all query params correctly', async () => {
// Mocks
mockPairLiquidityInfoHistoryService.getAllHistoryEntries.mockResolvedValue(
[],
);

// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?limit=50&offset=50&order=desc&pair-address=ct_22iY9&height=912485&from-block-time=1709027642807&to-block-time=1709027642807',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledWith(
50,
50,
OrderQueryEnum.desc,
'ct_22iY9',
912485,
1709027642807n,
1709027642807n,
);
expect(result.status).toBe(200);
expect(result.body).toEqual([]);
});

it('should validate limit query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?limit=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate offset query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?offset=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate order query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?order=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate height query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?height=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate from-block-time query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?from-block-time=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate to-block-time query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?to-block-time=xyz',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});
});
});
2 changes: 1 addition & 1 deletion src/api/pair-liquidity-info-history/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class PairLiquidityInfoHistoryController {
Number(offset),
order,
pairAddress,
Number(height),
height ? Number(height) : undefined,
fromBlockTime ? BigInt(fromBlockTime) : undefined,
toBlockTime ? BigInt(toBlockTime) : undefined,
)
Expand Down

0 comments on commit 1ff80e6

Please sign in to comment.