This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from centrifuge/api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
112 develop suitable unit testing approach (#117)
* test: fundamental approach * test: poolHandlers testing * test: run pools and tranche tests in CI * test: include generation of entities * ci: execute testing in correct order
- Loading branch information
Showing
16 changed files
with
3,456 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export DB_USER=postgres | ||
export DB_PASS=postgres | ||
export DB_DATABASE=postgres | ||
export DB_HOST=localhost | ||
export DB_PORT=5432 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug Jest Tests", | ||
"type": "node", | ||
"request": "launch", | ||
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen" | ||
}, | ||
{ | ||
"name": "Debug SubQL Node", | ||
"type": "node", | ||
"request": "launch", | ||
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/subql-node", "-f", "${workspaceRoot}"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Config } from 'jest' | ||
|
||
const config: Config = { | ||
verbose: true, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
setupFiles: ['./jest/globals.ts'], | ||
transform: { | ||
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest` | ||
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest` | ||
'^.+\\.ts?$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: './jest/tsconfig.jest.json', | ||
}, | ||
], | ||
}, | ||
} | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
declare const api: any | ||
declare const logger: any | ||
declare const store: any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { jest } from '@jest/globals' | ||
|
||
const globalHere: any = global | ||
|
||
globalHere.store = { | ||
get: jest.fn(), | ||
getByField: jest.fn(), | ||
getOneByField: jest.fn(), | ||
set: jest.fn((...args) => args[2]), | ||
bulkCreate: jest.fn(), | ||
bulkUpdate: jest.fn(), | ||
remove: jest.fn(), | ||
} | ||
|
||
globalHere.logger = { | ||
fatal: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
trace: jest.fn(), | ||
} | ||
|
||
globalHere.api = { query: {}, rpc: {} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
"importHelpers": true, | ||
"resolveJsonModule": true, | ||
"module": "commonjs", | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"target": "es2017", | ||
"paths": { | ||
"centrifuge-subql/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["./src/**/*", "./jest/*.ts"], | ||
"exports": { | ||
"chaintypes": "./src/chaintypes.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { AccountService } from './accountService' | ||
|
||
test('Account is created in database', async () => { | ||
const id = 'ABCDE' | ||
const account = AccountService.init(id) | ||
await account.save() | ||
|
||
expect(logger.info).toHaveBeenCalled() | ||
expect(store.set).toHaveBeenCalledWith('Account', id, { id }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CurrencyService } from './currencyService' | ||
|
||
const entityName = 'Currency' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
api.query['ormlAssetRegistry'] = { metadata: jest.fn(() => ({ isSome: false })) } as any | ||
|
||
const usdDecimals = 12 | ||
const stdDecimals = 18 | ||
|
||
test('AUSD is initialised with 12 decimals', async () => { | ||
const ticker = 'AUSD' | ||
await CurrencyService.getOrInit(ticker) | ||
|
||
expect(store.set).toBeCalledWith(entityName, ticker, { id: ticker, decimals: usdDecimals }) | ||
}) | ||
|
||
test('ACA is initialised with 18 decimals', async () => { | ||
const ticker = 'ACA' | ||
await CurrencyService.getOrInit(ticker) | ||
|
||
expect(store.set).toBeCalledWith(entityName, ticker, { id: ticker, decimals: stdDecimals }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { PoolService } from './poolService' | ||
|
||
api.query['pools'] = { | ||
pool: jest.fn(() => ({ | ||
isSome: true, | ||
isNone: false, | ||
toHuman: jest.fn(), | ||
unwrap: () => ({ | ||
currency: 'AUSD', | ||
metadata: { isSome: true, unwrap: () => ({ toUtf8: () => 'AAA' }) }, | ||
reserve: { | ||
total: { toBigInt: () => BigInt(91000000000000) }, | ||
available: { toBigInt: () => BigInt(92000000000000) }, | ||
max: { toBigInt: () => BigInt(192000000000000) }, | ||
}, | ||
parameters: { | ||
minEpochTime: { toNumber: () => 500 }, | ||
maxNavAge: { toNumber: () => 500 }, | ||
}, | ||
}), | ||
})), | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any | ||
|
||
api.query['loans'] = { | ||
poolNAV: jest.fn(() => ({ | ||
isSome: true, | ||
unwrap: jest.fn(() => ({ | ||
latest: { | ||
toBigInt: () => BigInt(100000000000000), | ||
}, | ||
})), | ||
})), | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any | ||
|
||
const poolId = '4355663', | ||
now = new Date(), | ||
block = 235443 | ||
|
||
const pool = PoolService.init(poolId, now, block) | ||
|
||
describe('Given a new pool, when initialised', () => { | ||
test('then type is set to "ALL"', () => { | ||
expect(pool.pool.type).toBe('ALL') | ||
}) | ||
|
||
test('then "created at" property is set correctly', () => { | ||
expect(pool.pool.createdAt).toBe(now) | ||
}) | ||
|
||
test('then "created at block" property is set correctly', () => { | ||
expect(pool.pool.createdAtBlockNumber).toBe(block) | ||
}) | ||
|
||
test('then reset accumulators are set to 0', () => { | ||
const resetAccumulators = Object.getOwnPropertyNames(pool.poolState).filter((prop) => prop.endsWith('_')) | ||
for (const resetAccumulator of resetAccumulators) { | ||
expect(pool.poolState[resetAccumulator]).toBe(BigInt(0)) | ||
} | ||
}) | ||
|
||
test('when the pool data is initialised, then the correct values are fetched and set', async () => { | ||
await pool.initData(async () => 'AUSD') | ||
expect(api.query.pools.pool).toBeCalledWith(poolId) | ||
expect(pool.pool).toMatchObject({ currencyId: 'AUSD', metadata: 'AAA', minEpochTime: 500, maxNavAge: 500 }) | ||
}) | ||
|
||
test('then it can be saved to the database', async () => { | ||
await pool.save() | ||
expect(store.set).toHaveBeenNthCalledWith(1, 'PoolState', poolId, expect.anything()) | ||
expect(store.set).toHaveBeenNthCalledWith(2, 'Pool', poolId, expect.anything()) | ||
}) | ||
}) | ||
|
||
describe('Given an existing pool,', () => { | ||
test('when the nav is updated, then the value is fetched and set correctly', async () => { | ||
await pool.updateNav() | ||
expect(api.query.loans.poolNAV).toBeCalled() | ||
expect(pool.poolState.netAssetValue).toBe(BigInt(100000000000000)) | ||
}) | ||
|
||
test('when the pool state is updated, then the values are fetched and set correctly', async () => { | ||
await pool.updateState() | ||
expect(api.query.pools.pool).toBeCalledWith(poolId) | ||
expect(pool.poolState).toMatchObject({ | ||
totalReserve: BigInt(91000000000000), | ||
availableReserve: BigInt(92000000000000), | ||
maxReserve: BigInt(192000000000000), | ||
}) | ||
}) | ||
|
||
test('when total borrowings are registered, then values are incremented correctly', async () => { | ||
await pool.increaseTotalBorrowings(BigInt('34999000000000000')) | ||
expect(pool.poolState).toMatchObject({ | ||
totalBorrowed_: BigInt('34999000000000000'), | ||
totalEverBorrowed: BigInt('34999000000000000'), | ||
totalNumberOfLoans_: BigInt(1), | ||
totalEverNumberOfLoans: BigInt(1), | ||
}) | ||
}) | ||
|
||
test('when an epoch is closed, then the corresponding current epoch and last counters are increased', async () => { | ||
await pool.closeEpoch(1) | ||
expect(pool.pool).toMatchObject({ | ||
currentEpoch: 2, | ||
lastEpochClosed: 1, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.