forked from aeternity/dex-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test: TasksService, ImporterService, ValidatorService #10
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5e63ac
test: adjust isRunning logic, implement unit tests for TasksService
tmrdlt 771a95d
test: implement unit tests for ImporterService
tmrdlt 64b24c0
test: implement unit tests for ValidatorService
tmrdlt 45341c2
test: run test ci on pr lvl, fix e2e tests
tmrdlt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
157 changes: 157 additions & 0 deletions
157
src/tasks/pair-liquidity-info-history-importer.service.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { PairLiquidityInfoHistoryImporterService } from './pair-liquidity-info-history-importer.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MdwClientService } from '../clients/mdw-client.service'; | ||
import { PairService } from '../database/pair.service'; | ||
import { PairLiquidityInfoHistoryService } from '../database/pair-liquidity-info-history.service'; | ||
import { PairLiquidityInfoHistoryErrorService } from '../database/pair-liquidity-info-history-error.service'; | ||
import { ContractAddress } from '../lib/utils'; | ||
import { Contract } from '../clients/mdw-client.model'; | ||
|
||
const mockMdwClientService = { | ||
getContract: jest.fn(), | ||
getMicroBlock: jest.fn(), | ||
getContractLogsUntilCondition: jest.fn(), | ||
getContractBalancesAtMicroBlockHashV1: jest.fn(), | ||
getAccountBalanceForContractAtMicroBlockHash: jest.fn(), | ||
}; | ||
|
||
const mockPairService = { | ||
getAll: jest.fn(), | ||
}; | ||
|
||
const mockPairLiquidityInfoHistoryService = { | ||
getLastlySyncedBlockByPairId: jest.fn(), | ||
upsert: jest.fn(), | ||
}; | ||
|
||
const mockPairLiquidityInfoHistoryErrorService = { | ||
getErrorByPairIdAndMicroBlockHashWithinHours: jest.fn(), | ||
}; | ||
|
||
describe('PairLiquidityInfoHistoryImporterService', () => { | ||
let service: PairLiquidityInfoHistoryImporterService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
PairLiquidityInfoHistoryImporterService, | ||
{ provide: MdwClientService, useValue: mockMdwClientService }, | ||
{ provide: PairService, useValue: mockPairService }, | ||
{ | ||
provide: PairLiquidityInfoHistoryService, | ||
useValue: mockPairLiquidityInfoHistoryService, | ||
}, | ||
{ | ||
provide: PairLiquidityInfoHistoryErrorService, | ||
useValue: mockPairLiquidityInfoHistoryErrorService, | ||
}, | ||
], | ||
}).compile(); | ||
service = module.get<PairLiquidityInfoHistoryImporterService>( | ||
PairLiquidityInfoHistoryImporterService, | ||
); | ||
}); | ||
|
||
describe('import', () => { | ||
it('should import liquidity correctly', async () => { | ||
// Mock data | ||
const pair1 = { | ||
id: 1, | ||
address: 'ct_pair' as ContractAddress, | ||
token0: { address: 'ct_token0' }, | ||
token1: { address: 'ct_token1' }, | ||
}; | ||
const pair1Contract: Contract = { | ||
aexn_type: '', | ||
block_hash: 'mh_hash0', | ||
contract: pair1.address, | ||
source_tx_hash: 'th_', | ||
source_tx_type: '', | ||
create_tx: {}, | ||
}; | ||
const initialMicroBlock = { | ||
hash: pair1Contract.block_hash, | ||
height: '10000', | ||
time: '1000000000000', | ||
}; | ||
const pairContractLog1 = { | ||
block_time: '1000000000001', | ||
block_hash: 'mh_hash1', | ||
height: '10001', | ||
}; | ||
const pairContractLog2 = { | ||
block_time: '1000000000002', | ||
block_hash: 'mh_hash2', | ||
height: '10002', | ||
}; | ||
|
||
// Mock functions | ||
mockPairService.getAll.mockResolvedValue([pair1]); | ||
mockPairLiquidityInfoHistoryErrorService.getErrorByPairIdAndMicroBlockHashWithinHours.mockResolvedValue( | ||
undefined, | ||
); | ||
mockPairLiquidityInfoHistoryService.getLastlySyncedBlockByPairId.mockReturnValue( | ||
undefined, | ||
); | ||
mockMdwClientService.getContract.mockResolvedValue(pair1Contract); | ||
mockMdwClientService.getMicroBlock.mockResolvedValue(initialMicroBlock); | ||
mockPairLiquidityInfoHistoryService.upsert.mockResolvedValue(null); | ||
mockMdwClientService.getContractLogsUntilCondition.mockResolvedValue([ | ||
pairContractLog1, | ||
pairContractLog2, | ||
]); | ||
mockMdwClientService.getContractBalancesAtMicroBlockHashV1.mockResolvedValue( | ||
{ amounts: { ak_a: '1', ak_b: '1' } }, | ||
); | ||
mockMdwClientService.getAccountBalanceForContractAtMicroBlockHash.mockResolvedValue( | ||
{ amount: '1' }, | ||
); | ||
jest.spyOn(service.logger, 'log'); | ||
|
||
// Start import | ||
await service.import(); | ||
|
||
// Assertions | ||
// Insert initial liquidity | ||
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({ | ||
pairId: pair1.id, | ||
totalSupply: '0', | ||
reserve0: '0', | ||
reserve1: '0', | ||
height: parseInt(initialMicroBlock.height), | ||
microBlockHash: initialMicroBlock.hash, | ||
microBlockTime: BigInt(initialMicroBlock.time), | ||
}); | ||
expect( | ||
mockPairLiquidityInfoHistoryErrorService.getErrorByPairIdAndMicroBlockHashWithinHours, | ||
).toHaveBeenCalledTimes(3); | ||
expect(service.logger.log).toHaveBeenCalledWith( | ||
`Started syncing pair ${pair1.id} ${pair1.address}. Need to sync 2 micro block(s). This can take some time.`, | ||
); | ||
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({ | ||
pairId: pair1.id, | ||
totalSupply: '2', | ||
reserve0: '1', | ||
reserve1: '1', | ||
height: parseInt(pairContractLog1.height), | ||
microBlockHash: pairContractLog1.block_hash, | ||
microBlockTime: BigInt(pairContractLog1.block_time), | ||
}); | ||
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({ | ||
pairId: pair1.id, | ||
totalSupply: '2', | ||
reserve0: '1', | ||
reserve1: '1', | ||
height: parseInt(pairContractLog2.height), | ||
microBlockHash: pairContractLog2.block_hash, | ||
microBlockTime: BigInt(pairContractLog2.block_time), | ||
}); | ||
expect(service.logger.log).toHaveBeenCalledWith( | ||
`Completed sync for pair ${pair1.id} ${pair1.address}. Synced 2 micro block(s).`, | ||
); | ||
expect(service.logger.log).toHaveBeenCalledWith( | ||
'Finished liquidity info history sync for all pairs.', | ||
); | ||
}); | ||
}); | ||
}); |
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
110 changes: 110 additions & 0 deletions
110
src/tasks/pair-liquidity-info-history-validator.service.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { PairLiquidityInfoHistoryValidatorService } from './pair-liquidity-info-history-validator.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MdwClientService } from '../clients/mdw-client.service'; | ||
import { PairLiquidityInfoHistoryService } from '../database/pair-liquidity-info-history.service'; | ||
|
||
const mockMdwClientService = { | ||
getKeyBlockMicroBlocks: jest.fn(), | ||
}; | ||
|
||
const mockPairLiquidityInfoHistoryService = { | ||
getWithinHeightSorted: jest.fn(), | ||
deleteFromMicroBlockTime: jest.fn(), | ||
}; | ||
|
||
describe('PairLiquidityInfoHistoryValidatorService', () => { | ||
let service: PairLiquidityInfoHistoryValidatorService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
PairLiquidityInfoHistoryValidatorService, | ||
{ provide: MdwClientService, useValue: mockMdwClientService }, | ||
{ | ||
provide: PairLiquidityInfoHistoryService, | ||
useValue: mockPairLiquidityInfoHistoryService, | ||
}, | ||
], | ||
}).compile(); | ||
service = module.get<PairLiquidityInfoHistoryValidatorService>( | ||
PairLiquidityInfoHistoryValidatorService, | ||
); | ||
}); | ||
|
||
describe('validate', () => { | ||
it('should validate history correctly', async () => { | ||
// Mock data | ||
const historyEntry1 = { | ||
height: 10001, | ||
microBlockTime: 1000000000001n, | ||
microBlockHash: 'mh_hash1', | ||
}; | ||
const historyEntry2 = { | ||
height: 10001, | ||
microBlockTime: 1000000000002n, | ||
microBlockHash: 'mh_hash2', | ||
}; | ||
const historyEntry3 = { | ||
height: 10003, | ||
microBlockTime: 1000000000003n, | ||
microBlockHash: 'mh_hash3', | ||
}; | ||
const historyEntry4 = { | ||
height: 10004, | ||
microBlockTime: 1000000000004n, | ||
microBlockHash: 'mh_hash4', | ||
}; | ||
const historyEntry5 = { | ||
height: 10005, | ||
microBlockTime: 1000000000005n, | ||
microBlockHash: 'mh_hash5', | ||
}; | ||
// Mock functions | ||
mockPairLiquidityInfoHistoryService.getWithinHeightSorted.mockResolvedValue( | ||
[historyEntry1, historyEntry2, historyEntry3, historyEntry4], | ||
); | ||
mockMdwClientService.getKeyBlockMicroBlocks.mockImplementation( | ||
(height: number) => { | ||
if (height === historyEntry1.height) { | ||
return [ | ||
{ hash: historyEntry1.microBlockHash }, | ||
{ hash: historyEntry2.microBlockHash }, | ||
{ hash: 'mh_xyz' }, | ||
]; | ||
} else if (height === historyEntry3.height) { | ||
return [{ hash: historyEntry3.microBlockHash }, { hash: 'mh_xyz' }]; | ||
} else { | ||
return []; | ||
} | ||
}, | ||
); | ||
mockPairLiquidityInfoHistoryService.deleteFromMicroBlockTime.mockResolvedValue( | ||
{ count: 2 }, | ||
); | ||
jest.spyOn(service.logger, 'log'); | ||
|
||
// Start validation | ||
await service.validate(); | ||
|
||
// Assertions | ||
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith( | ||
historyEntry1.height, | ||
); | ||
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith( | ||
historyEntry3.height, | ||
); | ||
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith( | ||
historyEntry4.height, | ||
); | ||
expect( | ||
mockMdwClientService.getKeyBlockMicroBlocks, | ||
).not.toHaveBeenCalledWith(historyEntry5.height); | ||
expect( | ||
mockPairLiquidityInfoHistoryService.deleteFromMicroBlockTime, | ||
).toHaveBeenCalledWith(historyEntry4.microBlockTime); | ||
expect(service.logger.log).toHaveBeenCalledWith( | ||
`Found an inconsistency in pair liquidity info history. Deleted 2 entries.`, | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to keep this somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned above, I removed it because linting now also happens in the Code Check CI (At least
npm run lint:check
.npm run lint
also fixes potential errors, but I don't see why this is needed in the tests).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was on the wrong branch 🤦