Skip to content

Commit

Permalink
test(delete): deletion tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowplay1 committed Nov 2, 2023
1 parent 9091ba6 commit 83e9897
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/lib/managers/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ export class CacheManager<K extends string, V> {
updatedData = updatedData?.[keys[i]]
}

this._cache.set(keys[0] as K, data[keys[0]])
if (keys.length == 1) {
this._cache.delete(keys[0] as K)
} else {
this._cache.set(keys[0] as K, updatedData[keys[0]])
}

return true
}

Expand Down
51 changes: 38 additions & 13 deletions tests/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ describe('get, set, delete operations', () => {
await database.set<number>('someNumber', 1)
]


return expect(setResults).toEqual(['hello', 'hello123', 1])
})

test.concurrent('set objects data', async () => {
await quickMongoClient.connect()

const setResult = await database.set('someObject.someProperty.hello', 'hi')
return expect(setResult).toEqual('hi')
const setResults = [
await database.set('someObject.someProperty.hello', 'hi'),
await database.set('someObject.someProperty.hi', 'hello')
]

return expect(setResults).toEqual(['hi', 'hello'])
})


Expand All @@ -60,8 +65,12 @@ describe('get, set, delete operations', () => {
await database.loadCache()
await sleep(1000)

const getResult = database.get<string>('someObject.someProperty.hello')
return expect(getResult).toEqual('hi')
const getResults = [
database.get<string>('someObject.someProperty.hello'),
database.get<string>('someObject.someProperty.hi')
]

return expect(getResults).toEqual(['hi', 'hello'])
})

test.concurrent('get unexistent value', async () => {
Expand Down Expand Up @@ -91,15 +100,19 @@ describe('get, set, delete operations', () => {
test.concurrent('has objects data', async () => {
await database.loadCache()

const hasResult = database.has('someObject.someProperty.hello')
return expect(hasResult).toEqual(true)
const hasResults = [
database.has('someObject.someProperty.hello'),
database.has('someObject.someProperty.hi')
]

return expect(hasResults).toEqual([true, true])
})

test.concurrent('has unexistent value', async () => {
await database.loadCache()

const hasResult = database.has('somethingElse')
return expect(hasResult).toEqual(false)
return expect(hasResult).toBeFalsy()
})


Expand All @@ -109,24 +122,36 @@ describe('get, set, delete operations', () => {
await database.loadCache()
await sleep(1000)

const hasResult = await database.delete('someString123')
return expect(hasResult).toEqual(true)
const deleteResults = [
await database.delete('someString123'),
await database.delete('someObject.someProperty.hi')
]

return expect(deleteResults).toEqual([true, true])
})

test.concurrent('attempt to get deleted data', async () => {
await database.loadCache()
await sleep(1000)

const deletedResult = database.get('someString123')
return expect(deletedResult).toBeNull()
const deletedGetResults = [
database.get('someString123'),
database.get('someObject.someProperty.hi')
]

return expect(deletedGetResults).toEqual([null, null])
})

test.concurrent('delete unexistent data', async () => {
await database.loadCache()
await sleep(1000)

const hasResult = await database.delete('somethingElse')
return expect(hasResult).toEqual(false)
const unexistentDeleteResults = [
await database.delete('somethingElse'),
await database.delete('someObject.someProperty.hi')
]

return expect(unexistentDeleteResults).toEqual([false, false])
})
})

Expand Down
4 changes: 2 additions & 2 deletions tests/subtract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe('subtraction operation', () => {
return expect(subtractionResult).toEqual(-4)
})

test.concurrent('subtract -5 to number in object', async () => {
test.concurrent('subtract -5 from number in object', async () => {
await database.loadCache()
await sleep(1000)

const subtractionResult = await database.subtract('numbers.number', 5)
return expect(subtractionResult).toBeDefined()
})

test.concurrent('subtract -5 to unexistent number', async () => {
test.concurrent('subtract -5 from unexistent number', async () => {
await database.loadCache()
await sleep(1000)

Expand Down

0 comments on commit 83e9897

Please sign in to comment.