Skip to content

Commit

Permalink
test: add missing tests for increment/decrement methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Melchyore committed May 2, 2023
1 parent b10ed48 commit d20b74d
Showing 1 changed file with 181 additions and 33 deletions.
214 changes: 181 additions & 33 deletions tests/functional/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import TaggableStore from '../../src/Stores/TaggableStore'
import InMemory from '../../src/Stores/InMemory'
import Memcached from '../../src/Stores/Memcached'
import Redis from '../../src/Stores/Redis'
import Repository from '../../src/Repository'

const cacheConfig = getCacheConfig()

Expand All @@ -26,6 +27,14 @@ test.group('Cache Manager', (group) => {
await fs.cleanup()
})

test('raise error when config is missing', async ({ expect }) => {
const app = await setup('test', {})

expect(() => app.container.use('Adonis/Addons/Cache')).toThrowError(
'Invalid "cache" config. Missing value for "store". Make sure to set it inside the "config/cache" file'
)
})

test('get default store', async ({ expect }) => {
const app = await setup('test', {})
const Cache = new CacheManager(app, cacheConfig)
Expand All @@ -48,39 +57,22 @@ test.group('Cache Manager', (group) => {
expect(RedisCache.store).toBeInstanceOf(Redis)
})

test('extend by adding a new store should throw exception if config is not provided', async ({
expect
}) => {
const app = await setup('test', {})

const Cache = new CacheManager(app, cacheConfig)

const DummyStore = getDummyStore(BaseStore)

Cache.extend('dummy', () => {
// @ts-ignore
return Cache.repository(null, new DummyStore())
})

expect(() => Cache.use('dummy')).toThrowError(
'You must provide the driver configuration as first argument'
)
})

test('extend by adding a new store', async ({ expect }) => {
const app = await setup('test', {})

const Cache = new CacheManager(app, cacheConfig)

const DummyStore = getDummyStore(BaseStore)

Cache.extend('dummy', (_, __, config: CacheStoreConfig) => {
return Cache.repository(config, new DummyStore())
Cache.extend('dummy', (_, __, ___: CacheStoreConfig) => {
return new DummyStore()
})

const Dummy = Cache.use('dummy')

expect(Dummy).toBeInstanceOf(Repository)
expect(Dummy.store).toBeInstanceOf(DummyStore)
expect(Dummy.driverConfig.driver).toStrictEqual('dummy')
})

test('extend by adding a new taggable store', async ({ expect }) => {
Expand All @@ -90,8 +82,8 @@ test.group('Cache Manager', (group) => {

const DummyStore = getTaggableDummyStore(TaggableStore)

Cache.extend('dummy', (_, __, config: CacheStoreConfig) => {
return Cache.repository(config, new DummyStore())
Cache.extend('dummy', (_, __, ___: CacheStoreConfig) => {
return new DummyStore()
})

const Dummy = Cache.use('dummy')
Expand Down Expand Up @@ -327,6 +319,20 @@ test.group('Cache Manager - Database store', (group) => {
expect(await Cache.get(key)).toStrictEqual(7)
})

test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('database', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
})

test('increment method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -361,6 +367,20 @@ test.group('Cache Manager - Database store', (group) => {
expect(await Cache.get(key)).toStrictEqual(3)
})

test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('database', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
})

test('decrement method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -671,8 +691,6 @@ test.group('Cache Manager - Database store', (group) => {
await Cache.put(key, 'John Doe')

expect(await Cache.missing(key)).toBeFalsy()

await Cache.clear()
})
})

Expand Down Expand Up @@ -904,6 +922,20 @@ test.group('Cache Manager - DynamoDB', (group) => {
expect(await Cache.get(key)).toStrictEqual(7)
}).disableTimeout()

test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('dynamodb', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
}).disableTimeout()

test('increment method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -938,6 +970,20 @@ test.group('Cache Manager - DynamoDB', (group) => {
expect(await Cache.get(key)).toStrictEqual(3)
}).disableTimeout()

test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('dynamodb', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
}).disableTimeout()

test('decrement method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -1444,6 +1490,20 @@ test.group('Cache Manager - File', (group) => {
expect(await Cache.get(key)).toStrictEqual(7)
})

test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('file', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
})

test('increment method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -1478,6 +1538,20 @@ test.group('Cache Manager - File', (group) => {
expect(await Cache.get(key)).toStrictEqual(3)
})

test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('file', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
})

test('decrement method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -1797,8 +1871,6 @@ test.group('Cache Manager - File', (group) => {
await Cache.put(key, 'John Doe')

expect(await Cache.missing(key)).toBeFalsy()

await Cache.clear()
})
})

Expand Down Expand Up @@ -2045,6 +2117,20 @@ test.group('Cache Manager - InMemory', (group) => {
expect(await Cache.get(key)).toStrictEqual(7)
})

test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('in_memory', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
})

test('increment method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -2079,6 +2165,20 @@ test.group('Cache Manager - InMemory', (group) => {
expect(await Cache.get(key)).toStrictEqual(3)
})

test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('in_memory', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
})

test('decrement method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -2545,8 +2645,6 @@ test.group('Cache Manager - Memcached', (group) => {
})

expect(value).toStrictEqual(data)

await Cache.flush()
})

test('get method should not cache value if key not found and fallback defined', async ({
Expand All @@ -2559,8 +2657,6 @@ test.group('Cache Manager - Memcached', (group) => {
await Cache.get(key, 'John Doe')

expect(await Cache.has(key)).toBeFalsy()

await Cache.flush()
})

test('get method should return cached value using forever method', async ({ expect }) => {
Expand Down Expand Up @@ -2618,8 +2714,20 @@ test.group('Cache Manager - Memcached', (group) => {

expect(await Cache.increment(key, 2)).toStrictEqual(7)
expect(await Cache.get(key)).toStrictEqual(7)
})

await Cache.flush()
test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('memcached', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
})

test('increment method should not change value if it is not a number and return false', async ({
Expand Down Expand Up @@ -2654,8 +2762,20 @@ test.group('Cache Manager - Memcached', (group) => {

expect(await Cache.decrement(key, 2)).toStrictEqual(3)
expect(await Cache.get(key)).toStrictEqual(3)
})

await Cache.flush()
test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('memcached', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
})

test('decrement method should not change value if it is not a number and return false', async ({
Expand Down Expand Up @@ -3223,6 +3343,20 @@ test.group('Cache Manager - Redis', (group) => {
expect(await Cache.get(key)).toStrictEqual(7)
})

test('increment method should increment the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('redis', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.increment(key)).toStrictEqual(6)
expect(await Cache.get(key)).toStrictEqual(6)
})

test('increment method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down Expand Up @@ -3257,6 +3391,20 @@ test.group('Cache Manager - Redis', (group) => {
expect(await Cache.get(key)).toStrictEqual(3)
})

test('decrement method should decrement the cached value by 1 if no value specified', async ({
expect
}) => {
const Cache = await getCache('redis', cacheConfig)

const key = 'test'
const value = 5

await Cache.put(key, value)

expect(await Cache.decrement(key)).toStrictEqual(4)
expect(await Cache.get(key)).toStrictEqual(4)
})

test('decrement method should not change value if it is not a number and return false', async ({
expect
}) => {
Expand Down

0 comments on commit d20b74d

Please sign in to comment.