Skip to content

Commit

Permalink
test: fixes all currently active tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodressel authored and mmpetarpeshev committed Jun 25, 2024
1 parent 138d45c commit 0fee94c
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 79 deletions.
21 changes: 21 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,24 @@ POSTGRES_HOST_PORT=5432
POSTGRES_HOST_ADDRESS=localhost

DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST_ADDRESS}:${POSTGRES_HOST_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}

AUTH_TOKEN=774a64fdc5dc8d5b9dd579bb9e6f556baaecd90610667c69db64dbc0a8667af1

# if SUBSCRIBE_TO_ALL_TXS is omitted or has 0 value it will subscribe only to ROUTER_ADDRESS
SUBSCRIBE_TO_ALL_TXS=1

# set MDW_PING_TIMOUT_MS to 0 or undefined for indefinite ping timeout on mdw websocket
MDW_PING_TIMEOUT_MS=10000

SHOW_INVALID_TOKENS=true

NETWORK_NAME=testnet
ROUTER_ADDRESS=ct_MLXQEP12MBn99HL6WDaiTqDbG4bJQ3Q9Bzr57oLfvEkghvpFb
FACTORY_ADDRESS=ct_NhbxN8wg8NLkGuzwRNDQhMDKSKBwDAQgxQawK7tkigi2aC7i9
WAE_ADDRESS=ct_JDp175ruWd7mQggeHewSLS1PFXt9AzThCDaFedxon8mF8xTRF
# these are contract addresses for swagger examples and can be omitted
DOC_PAIR=ct_2JZNDfAQHZMfoBuh32Aijd9TR8A5SHUVBzxC6x5d4sS7o8xeqN
DOC_TOKEN1=ct_b7FZHQzBcAW4r43ECWpV3qQJMQJp5BxkZUGNKrqqLyjVRN3SC
DOC_TOKEN2=ct_JDp175ruWd7mQggeHewSLS1PFXt9AzThCDaFedxon8mF8xTRF

COIN_MARKET_CAP_API_KEY=c6eb1196-9946-4256-b90b-a64dc3db241f
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"serve:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint:check": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"test": "jest",
"test:watch": "test jest --watch",
"test:cov": "jest --coverage",
"test": "dotenv -e .env.test jest",
"test:watch": "npm run test -- --watch",
"test:cov": "npm run test -- --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "dotenv -e .env.test -- jest --runInBand --config ./test/e2e/jest-e2e.json",
"test:e2e": "npm run test -- --runInBand --config ./test/e2e/jest-e2e.json",
"db": "docker compose up",
"db:deploy": "docker compose up -d",
"db:remove": "docker compose down -v",
Expand All @@ -44,7 +44,6 @@
"@prisma/client": "^5.10.2",
"bignumber.js": "^9.1.2",
"dex-contracts-v2": "github:aeternity/dex-contracts-v2",
"dotenv": "^16.4.3",
"limiter": "^2.1.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.2.1",
Expand Down Expand Up @@ -96,7 +95,6 @@
"^.+\\.(t|j)s$": "ts-jest"
},
"setupFiles": [
"dotenv/config",
"./test/setup.ts"
],
"collectCoverageFrom": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('PairLiquidityInfoHistoryController', () => {
await app.init();
});

describe('GET /history/liquidity', () => {
describe('GET /history', () => {
afterEach(() => {
jest.clearAllMocks();
});
Expand All @@ -58,9 +58,7 @@ describe('PairLiquidityInfoHistoryController', () => {
);

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

// Assertions
expect(
Expand Down Expand Up @@ -107,29 +105,30 @@ describe('PairLiquidityInfoHistoryController', () => {

// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?limit=50&offset=50&order=desc&pairAddress=ct_22iY9&height=912485&fromBlockTime=1709027642807&toBlockTime=1709027642807',
'/history?limit=50&offset=50&order=desc&pairAddress=ct_22iY9&height=912485&fromBlockTime=1709027642807&toBlockTime=1709027642807',
);

// Assertions
expect(
mockPairLiquidityInfoHistoryService.getAllHistoryEntries,
).toHaveBeenCalledWith(
50,
50,
OrderQueryEnum.desc,
'ct_22iY9',
912485,
1709027642807n,
1709027642807n,
);
).toHaveBeenCalledWith({
fromBlockTime: 1709027642807n,
height: 912485,
limit: 50,
offset: 50,
order: 'desc',
pairAddress: 'ct_22iY9',
toBlockTime: 1709027642807n,
tokenAddress: undefined,
});
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',
'/history?limit=xyz',
);

// Assertions
Expand All @@ -142,7 +141,7 @@ describe('PairLiquidityInfoHistoryController', () => {
it('should validate offset query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?offset=xyz',
'/history?offset=xyz',
);

// Assertions
Expand All @@ -155,7 +154,7 @@ describe('PairLiquidityInfoHistoryController', () => {
it('should validate order query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?order=xyz',
'/history?order=xyz',
);

// Assertions
Expand All @@ -168,7 +167,7 @@ describe('PairLiquidityInfoHistoryController', () => {
it('should validate height query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?height=xyz',
'/history?height=xyz',
);

// Assertions
Expand All @@ -181,7 +180,7 @@ describe('PairLiquidityInfoHistoryController', () => {
it('should validate fromBlockTime query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?fromBlockTime=xyz',
'/history?fromBlockTime=xyz',
);

// Assertions
Expand All @@ -194,7 +193,7 @@ describe('PairLiquidityInfoHistoryController', () => {
it('should validate toBlockTime query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/history/liquidity?toBlockTime=xyz',
'/history?toBlockTime=xyz',
);

// Assertions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PairLiquidityInfoHistoryImporterService import should catch and insert any error on log level (e.g. API rate limit reached) 1`] = `
[
[
{
"aeUsdPrice": "0.050559",
"deltaReserve0": "1",
"deltaReserve1": "-1",
"eventType": "SwapTokens",
"height": 30003,
"logIndex": 2,
"microBlockHash": "mh_hash3",
"microBlockTime": 3000000000003n,
"pairId": 1,
"reserve0": "201",
"reserve1": "199",
"senderAccount": "ak_sender",
"transactionHash": "th_hash3",
"transactionIndex": 30000003n,
},
],
]
`;

exports[`PairLiquidityInfoHistoryImporterService import should import liquidity correctly 1`] = `
[
[
Expand All @@ -15,6 +38,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should import liquidity
"pairId": 1,
"reserve0": "0",
"reserve1": "0",
"senderAccount": "ak_sender",
"transactionHash": "th_",
"transactionIndex": 0n,
},
Expand All @@ -32,6 +56,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should import liquidity
"pairId": 1,
"reserve0": "100",
"reserve1": "100",
"senderAccount": "ak_sender",
"transactionHash": "th_hash1",
"transactionIndex": 10000001n,
},
Expand All @@ -49,6 +74,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should import liquidity
"pairId": 1,
"reserve0": "200",
"reserve1": "200",
"senderAccount": "ak_sender",
"transactionHash": "th_hash2",
"transactionIndex": 20000002n,
},
Expand All @@ -66,6 +92,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should import liquidity
"pairId": 1,
"reserve0": "201",
"reserve1": "199",
"senderAccount": "ak_sender",
"transactionHash": "th_hash3",
"transactionIndex": 30000003n,
},
Expand All @@ -83,6 +110,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should import liquidity
"pairId": 1,
"reserve0": "100",
"reserve1": "100",
"senderAccount": "ak_sender",
"transactionHash": "th_hash4",
"transactionIndex": 40000004n,
},
Expand All @@ -105,28 +133,7 @@ exports[`PairLiquidityInfoHistoryImporterService import should skip a log if the
"pairId": 1,
"reserve0": "201",
"reserve1": "199",
"transactionHash": "th_hash3",
"transactionIndex": 30000003n,
},
],
]
`;

exports[`PairLiquidityInfoHistoryImporterService import should catch and insert any error on log level (e.g. API rate limit reached) 1`] = `
[
[
{
"aeUsdPrice": "0.050559",
"deltaReserve0": "1",
"deltaReserve1": "-1",
"eventType": "SwapTokens",
"height": 30003,
"logIndex": 2,
"microBlockHash": "mh_hash3",
"microBlockTime": 3000000000003n,
"pairId": 1,
"reserve0": "201",
"reserve1": "199",
"senderAccount": "ak_sender",
"transactionHash": "th_hash3",
"transactionIndex": 30000003n,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
initialMicroBlock,
pairContract,
pairWithTokens,
senderAccount,
} from '@/test/mock-data/pair-liquidity-info-history-mock-data';

const mockPairDb = { getAll: jest.fn() };
Expand All @@ -40,6 +41,7 @@ const mockMdwClient = {
getContract: jest.fn(),
getMicroBlock: jest.fn(),
getContractLogsUntilCondition: jest.fn(),
getSenderAccountForTransaction: jest.fn(),
};

const mockCoinmarketcapClient = {
Expand Down Expand Up @@ -95,6 +97,9 @@ describe('PairLiquidityInfoHistoryImporterService', () => {
});
mockMdwClient.getContract.mockResolvedValue(pairContract);
mockMdwClient.getMicroBlock.mockResolvedValue(initialMicroBlock);
mockMdwClient.getSenderAccountForTransaction.mockResolvedValue(
senderAccount,
);
mockCoinmarketcapClient.getHistoricalPriceDataThrottled.mockResolvedValue(
coinmarketcapResponseAeUsdQuoteData,
);
Expand Down Expand Up @@ -199,6 +204,9 @@ describe('PairLiquidityInfoHistoryImporterService', () => {
contractLog4,
contractLog5,
]);
mockMdwClient.getSenderAccountForTransaction.mockResolvedValue(
senderAccount,
);

// Start import
await service.import();
Expand Down Expand Up @@ -289,6 +297,9 @@ describe('PairLiquidityInfoHistoryImporterService', () => {
contractLog4,
contractLog5,
]);
mockMdwClient.getSenderAccountForTransaction.mockResolvedValue(
senderAccount,
);
mockPairLiquidityInfoHistoryDb.getLastlySyncedLogByPairId
.mockResolvedValueOnce({})
.mockResolvedValueOnce(undefined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,8 @@ export class PairLiquidityInfoHistoryImporterService {
}

private async fetchPrice(microBlockTime: number): Promise<number> {
// mock with random value between 0.05 and 0.5
return 0.1;

return this.coinmarketcapClient
.getHistoricalPriceDataThrottled(microBlockTime)
.then((res) => res.data['1700'].quotes[0].quote.USD.price)
.catch(() => 0.1);
.then((res) => res.data['1700'].quotes[0].quote.USD.price);
}
}
Loading

0 comments on commit 0fee94c

Please sign in to comment.