Skip to content

Commit

Permalink
feat(2981): Test case for cache on wallet service and asset.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Nov 20, 2024
1 parent a305682 commit f80bdec
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 38 deletions.
174 changes: 174 additions & 0 deletions packages/backend/src/asset/service.cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import assert from 'assert'
import { v4 as uuid } from 'uuid'

import { AssetError, isAssetError } from './errors'
import { AssetService } from './service'
import { Asset } from './model'
import { Pagination, SortOrder } from '../shared/baseModel'
import { getPageTests } from '../shared/baseModel.test'
import { createTestApp, TestContainer } from '../tests/app'
import { createAsset, randomAsset } from '../tests/asset'
import { truncateTables } from '../tests/tableManager'
import { Config } from '../config/app'
import { IocContract } from '@adonisjs/fold'
import { initIocContainer } from '../'
import { AppServices } from '../app'

describe('Asset Service using Cache', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
let assetService: AssetService

beforeAll(async (): Promise<void> => {
deps = initIocContainer({
...Config,
localCacheDuration: 5_000 // 5-second default.
})
appContainer = await createTestApp(deps)
assetService = await deps.use('assetService')
})

afterEach(async (): Promise<void> => {
await truncateTables(appContainer.knex)
})

afterAll(async (): Promise<void> => {
await appContainer.shutdown()
})

describe('create', (): void => {
test.each`
withdrawalThreshold | liquidityThreshold
${undefined} | ${undefined}
${BigInt(5)} | ${undefined}
${undefined} | ${BigInt(5)}
${BigInt(5)} | ${BigInt(5)}
`(
'Asset can be created and fetched',
async ({ withdrawalThreshold, liquidityThreshold }): Promise<void> => {
const options = {
...randomAsset(),
withdrawalThreshold,
liquidityThreshold
}
const asset = await assetService.create(options)
assert.ok(!isAssetError(asset))
expect(asset).toMatchObject({
...options,
id: asset.id,
ledger: asset.ledger,
withdrawalThreshold: withdrawalThreshold || null,
liquidityThreshold: liquidityThreshold || null
})
await expect(assetService.get(asset.id)).resolves.toEqual(asset)
}
)
})

describe('get', (): void => {
test('Can get asset by id', async (): Promise<void> => {
const asset = await assetService.create(randomAsset())
assert.ok(!isAssetError(asset))
await expect(assetService.get(asset.id)).resolves.toEqual(asset)
})

test('Cannot get unknown asset', async (): Promise<void> => {
await expect(assetService.get(uuid())).resolves.toBeUndefined()
})
})

describe('update', (): void => {
describe.each`
withdrawalThreshold | liquidityThreshold
${null} | ${null}
${BigInt(0)} | ${null}
${BigInt(5)} | ${null}
${null} | ${BigInt(0)}
${null} | ${BigInt(5)}
${BigInt(0)} | ${BigInt(0)}
${BigInt(5)} | ${BigInt(5)}
`(
'Asset threshold can be updated from withdrawalThreshold: $withdrawalThreshold, liquidityThreshold: $liquidityThreshold',
({ withdrawalThreshold, liquidityThreshold }): void => {
let assetId: string

beforeEach(async (): Promise<void> => {
const asset = await assetService.create({
...randomAsset(),
withdrawalThreshold,
liquidityThreshold
})
assert.ok(!isAssetError(asset))
expect(asset.withdrawalThreshold).toEqual(withdrawalThreshold)
assetId = asset.id
})

test.each`
withdrawalThreshold | liquidityThreshold
${null} | ${null}
${BigInt(0)} | ${null}
${BigInt(5)} | ${null}
${null} | ${BigInt(0)}
${null} | ${BigInt(5)}
${BigInt(0)} | ${BigInt(0)}
${BigInt(5)} | ${BigInt(5)}
`(
'to withdrawalThreshold: $withdrawalThreshold, liquidityThreshold: $liquidityThreshold',
async ({
withdrawalThreshold,
liquidityThreshold
}): Promise<void> => {
const asset = await assetService.update({
id: assetId,
withdrawalThreshold,
liquidityThreshold
})
assert.ok(!isAssetError(asset))
expect(asset.withdrawalThreshold).toEqual(withdrawalThreshold)
expect(asset.liquidityThreshold).toEqual(liquidityThreshold)
await expect(assetService.get(assetId)).resolves.toEqual(asset)
}
)
}
)
})

describe('getPage', (): void => {
getPageTests({
createModel: () => createAsset(deps),
getPage: (pagination?: Pagination, sortOrder?: SortOrder) =>
assetService.getPage(pagination, sortOrder)
})
})

describe('getAll', (): void => {
test('returns all assets', async (): Promise<void> => {
const assets: (Asset | AssetError)[] = []
for (let i = 0; i < 3; i++) {
const asset = await assetService.create(randomAsset())
assets.push(asset)
}

await expect(assetService.getAll()).resolves.toEqual(assets)
})

test('returns empty array if no assets', async (): Promise<void> => {
await expect(assetService.getAll()).resolves.toEqual([])
})
})

describe('delete', (): void => {
test('Can delete asset', async (): Promise<void> => {
const newAsset = await assetService.create(randomAsset())
assert.ok(!isAssetError(newAsset))
const newAssetId = newAsset.id

const deletedAsset = await assetService.delete({
id: newAssetId,
deletedAt: new Date()
})
assert.ok(!isAssetError(deletedAsset))
expect(deletedAsset.deletedAt).not.toBeNull()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import { Knex } from 'knex'
import { v4 as uuid } from 'uuid'

import { isWalletAddressError, WalletAddressError } from './errors'
import {
WalletAddress,
WalletAddressEvent,
WalletAddressEventType
} from './model'
import { CreateOptions, FORBIDDEN_PATHS, WalletAddressService } from './service'
import { AccountingService } from '../../accounting/service'
import { WalletAddress } from './model'
import { CreateOptions, WalletAddressService } from './service'
import { createTestApp, TestContainer } from '../../tests/app'
import { createAsset } from '../../tests/asset'
import { createWalletAddress } from '../../tests/walletAddress'
Expand All @@ -22,9 +17,7 @@ import { faker } from '@faker-js/faker'
import { createIncomingPayment } from '../../tests/incomingPayment'
import { getPageTests } from '../../shared/baseModel.test'
import { Pagination, SortOrder } from '../../shared/baseModel'
import { sleep } from '../../shared/utils'
import { withConfigOverride } from '../../tests/helpers'
import { WalletAddressAdditionalProperty } from './additional_property/model'

describe('Open Payments Wallet Address Service using Cache', (): void => {
let deps: IocContract<AppServices>
Expand All @@ -36,7 +29,7 @@ describe('Open Payments Wallet Address Service using Cache', (): void => {
beforeAll(async (): Promise<void> => {
deps = initIocContainer({
...Config,
localCacheDuration: 5_000// 5-second default.
localCacheDuration: 5_000 // 5-second default.
})
config = await deps.use('config')
appContainer = await createTestApp(deps)
Expand Down Expand Up @@ -97,8 +90,20 @@ describe('Open Payments Wallet Address Service using Cache', (): void => {
const walletAddress = await createWalletAddress(deps)

if (!initialIsActive) {
// Only update the database:
await walletAddress.$query(knex).patch({ deactivatedAt: new Date() })
const fromCacheActive = await walletAddressService.get(
walletAddress.id
)

// We don't expect a match here, since the cache and database is out-of-sync:
expect(fromCacheActive!.isActive).toEqual(false)

// Update through the service, will also update the wallet-address cache:
await walletAddressService.update({ id: walletAddress.id, status: 'INACTIVE' })
await walletAddressService.update({
id: walletAddress.id,
status: 'INACTIVE'
})
}

const updatedWalletAddress = await walletAddressService.update({
Expand Down Expand Up @@ -239,33 +244,6 @@ describe('Open Payments Wallet Address Service using Cache', (): void => {
})
})

describe('onCredit with cache', (): void => {
let walletAddress: WalletAddress

beforeEach(async (): Promise<void> => {
walletAddress = await createWalletAddress(deps)
})

describe.each`
withdrawalThrottleDelay
${undefined}
${0}
${60_000}
`(
'withdrawalThrottleDelay: $withdrawalThrottleDelay',
({ withdrawalThrottleDelay }): void => {
let delayProcessAt: Date | null = null

beforeEach((): void => {
jest.useFakeTimers({ now: Date.now() })
if (withdrawalThrottleDelay !== undefined) {
delayProcessAt = new Date(Date.now() + withdrawalThrottleDelay)
}
})
}
)
})

describe('processNext', (): void => {
let walletAddress: WalletAddress

Expand Down

0 comments on commit f80bdec

Please sign in to comment.