-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorageRedis.js
84 lines (68 loc) · 2.16 KB
/
storageRedis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
const test = require('ava')
const Redis = require('ioredis')
const { StorageRedis } = require('..')
const fakes = require('../mock/fakes')
let storageRedis
const redisInstance = new Redis(fakes.redisPort, fakes.redisHost)
test('should throws error when create instance without params', t => {
const error = t.throws(() => { return new StorageRedis() })
t.is(error.message, 'Redis instance not passed to constructor')
})
test('should create instance success', t => {
t.notThrows(() => { return new StorageRedis(redisInstance) })
})
test.before(t => {
storageRedis = new StorageRedis(redisInstance)
t.pass()
})
test('should set value success and return it', async t => {
const key = 'key'
const value = 'value'
await storageRedis.set(key, value, 10)
t.is(await storageRedis.get(key), value)
})
test('should not set value if maxAge not passed', async t => {
const key = 'key1'
const value = 'value'
await storageRedis.set(key, value)
t.is(await storageRedis.get(key), null)
})
test('should not set value if maxAge = 0', async t => {
const key = 'key2'
const value = 'value'
await storageRedis.set(key, value, 0)
t.is(await storageRedis.get(key), null)
})
test('should not set value if maxAge < 0', async t => {
const key = 'key3'
const value = 'value'
await storageRedis.set(key, value, -10)
t.is(await storageRedis.get(key), null)
})
test('should not set value if maxAge is non-number', async t => {
const key = 'key4'
const value = 'value'
await storageRedis.set(key, value, 'dsfdsfsf')
t.is(await storageRedis.get(key), null)
})
test('should not set null value', async t => {
const key = 'key5'
const value = null
await storageRedis.set(key, value, 10)
t.is(await storageRedis.get(key), null)
})
test('should not set undefined value', async t => {
const key = 'key6'
const value = undefined
await storageRedis.set(key, value, 10)
t.is(await storageRedis.get(key), null)
})
test('should delete value', async t => {
const key = 'key7'
const value = 'value'
await storageRedis.set(key, value, 10)
t.is(await storageRedis.get(key), value)
await storageRedis.del(key)
t.is(await storageRedis.get(key), null)
})