Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

test: state snapshotter #127

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/helpers/stateSnapshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { SubstrateBlock } from '@subql/types'
import { PoolService } from '../mappings/services/poolService'
import { stateSnapshotter } from './stateSnapshot'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getByField = store.getByField as jest.Mock
const set = store.set as jest.Mock
const block = {
block: { header: { number: { toNumber: () => 11246 } } },
timestamp: new Date(),
} as unknown as SubstrateBlock

const poolId = '123456789',
timestamp = new Date(),
blockNumber = 11234

describe('Given a populated pool,', () => {
const pool = PoolService.init(poolId, timestamp, blockNumber)

test('when a snapshot is taken, then the id is set correctly', async () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await stateSnapshotter('Pool', 'PoolSnapshot', block)
expect(store.getByField).toHaveBeenCalledWith('Pool', 'type', 'ALL', expect.anything())
expect(store.set).toHaveBeenNthCalledWith(1, 'Pool', poolId, expect.anything())
expect(store.set).toHaveBeenNthCalledWith(2, 'PoolSnapshot', `${poolId}-11246`, expect.anything())
})

test('when a snapshot is taken, then the timestamp and blocknumber are added', async () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await stateSnapshotter('Pool', 'PoolSnapshot', block)
expect(store.set).toHaveBeenNthCalledWith(
2,
'PoolSnapshot',
`${poolId}-11246`,
expect.objectContaining({ timestamp: block.timestamp, blockNumber: 11246 })
)
})

test('when filters are specified, then the correct values are fetched', async () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await stateSnapshotter('Pool', 'PoolSnapshot', block, undefined, 'active', true)
expect(store.getByField).toHaveBeenNthCalledWith(1, 'Pool', 'active', true, expect.anything())
})

test('when a foreigh key is set, then the correct foreign key value is set on the snapshot', async () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await stateSnapshotter('Pool', 'PoolSnapshot', block, 'poolId')
expect(store.set).toHaveBeenNthCalledWith(
2,
'PoolSnapshot',
`${poolId}-11246`,
expect.objectContaining({ poolId: poolId })
)
})
})

describe('Given a pool with non zero accumulators, ', () => {
const pool = PoolService.init(poolId, timestamp, blockNumber)
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])

test('when a entity is snapshotted, then the accumulators are reset to 0', async () => {
const accumulatorProps = Object.getOwnPropertyNames(pool).filter((prop) => prop.endsWith('_'))
const zeroedProps = accumulatorProps.reduce((acc, currentProp) => ({ ...acc, [currentProp]: BigInt(0) }), {})
const accumulatedProps = accumulatorProps.reduce(
(acc, currentProp) => ({ ...acc, [currentProp]: BigInt('999999999999999999') }),
{}
)

Object.assign(pool, accumulatedProps)

await stateSnapshotter('Pool', 'PoolSnapshot', block)

expect(store.set).toHaveBeenNthCalledWith(1, 'Pool', poolId, expect.objectContaining(zeroedProps))
expect(store.set).toHaveBeenNthCalledWith(
2,
'PoolSnapshot',
`${poolId}-11246`,
expect.objectContaining(zeroedProps)
)
})
})
2 changes: 1 addition & 1 deletion src/mappings/services/trancheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ describe('Given an existing tranche,', () => {
await tranches[1].updatePriceFromRpc().catch(errorLogger)
expect((api.rpc as ExtendedRpc).pools.trancheTokenPrices).toBeCalled()
expect(logger.error).toBeCalled()
expect(tranches[1].price).toBe(BigInt('1000000000000000000'))
expect(tranches[1].price).toBe(BigInt('1000000000000000000000000000'))
})
})