Skip to content

Commit

Permalink
feat: add increment to CommonKeyValueDao
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnagydavid committed Sep 27, 2024
1 parent 00da6d6 commit e13d94f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/kv/commonKeyValueDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,8 @@ export class CommonKeyValueDao<T> {
},
)
}

async increment(id: string, by = 1): Promise<number> {
return await this.cfg.db.increment(this.cfg.table, id, by)
}
}
5 changes: 5 additions & 0 deletions src/testing/hashKeyValueDBTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export function runCommonHashKeyValueDBTest(db: CommonKeyValueDB): void {
await db.deleteByIds(TEST_TABLE, ids)
})

afterAll(async () => {
const ids = await db.streamIds(TEST_TABLE).toArray()
await db.deleteByIds(TEST_TABLE, ids)
})

runCommonKeyValueDBTest(db)

test('increment on a non-existing field should set the value to 1', async () => {
Expand Down
35 changes: 35 additions & 0 deletions src/testing/hashKeyValueDaoTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
import { runCommonKeyValueDaoTest } from './keyValueDaoTest'

export function runCommonHashKeyValueDaoTest(dao: CommonKeyValueDao<Buffer>): void {
beforeAll(async () => {
// Tests in this suite are not isolated,
// and failing tests can leave the DB in an unexpected state for other tests,
// including the following test run.
// Here we clear the table before running the tests.
const ids = await dao.streamIds().toArray()
await dao.deleteByIds(ids)
})

afterAll(async () => {
const ids = await dao.streamIds().toArray()
await dao.deleteByIds(ids)
})

runCommonKeyValueDaoTest(dao)

test('increment on a non-existing field should set the value to 1', async () => {
const result = await dao.increment('nonExistingField')
expect(result).toBe(1)
})

test('increment on a existing field should increase the value by one', async () => {
const result = await dao.increment('nonExistingField')
expect(result).toBe(2)
})

test('increment should increase the value by the specified amount', async () => {
const result = await dao.increment('nonExistingField', 2)
expect(result).toBe(4)
})
}
2 changes: 2 additions & 0 deletions src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { runCommonDaoTest } from './daoTest'
import { CommonDBImplementationQuirks, runCommonDBTest } from './dbTest'
import { runCommonHashKeyValueDaoTest } from './hashKeyValueDaoTest'
import { runCommonHashKeyValueDBTest } from './hashKeyValueDBTest'
import { runCommonKeyValueDBTest } from './keyValueDBTest'
import {
Expand All @@ -25,6 +26,7 @@ export {
createTestItemsDBM,
runCommonDaoTest,
runCommonDBTest,
runCommonHashKeyValueDaoTest,
runCommonHashKeyValueDBTest,
runCommonKeyValueDBTest,
TEST_TABLE,
Expand Down

0 comments on commit e13d94f

Please sign in to comment.