-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
206 lines (153 loc) · 7.15 KB
/
index.test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @jest-environment node
*/
const GenericStore = require('./index');
const fs = require('fs');
describe('tests for invalid store', () => {
test('should throw an error', async () => {
let storeType = 'xxx'
expect(() => new GenericStore(storeType)).toThrow(`The module could not be found. Try installing it first: npm install ${storeType}`);
});
});
let store1;
describe('tests for redis-store', () => {
beforeEach(async () => {
let storeType = 'connect-redis'
const redis = require("redis-mock");
let redisClient = redis.createClient()
let storeOptions = { client: redisClient };
let store = new GenericStore(storeType, storeOptions)
await store.setProm('0', { 'a': 0 })
await store.setProm('1', { 'b': 1 })
await store.setProm('2', { 'c': 2 })
store1 = store
});
test('all methods happy path', async () => {
let store = store1
let length = await store.lengthProm()
expect(length).toBe(3)
// redis support .all while file-store doesnt
await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ a: 0 })]))
await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ b: 1 })]))
await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ c: 2 })]))
let data = await store.getProm('1')
expect(data).toEqual(expect.objectContaining({ 'b': 1 }))
await store.setProm('1', {})
await expect(store.getProm('1')).resolves.toEqual(expect.objectContaining({}))
await store.touchProm('1', { "a": 99 })
await expect(store.getProm('1')).resolves.toEqual(expect.objectContaining({}))
await expect(store.lengthProm()).resolves.toBe(3)
await store.destroyProm('1')
await expect(store.lengthProm()).resolves.toBe(2)
// redis resolve to undefined while file-store rejects
await expect(store.getProm('1')).resolves.toBe(undefined)
await store.clearProm()
expect(await store.lengthProm()).toBe(0)
});
});
let store2;
describe('tests for file-store', () => {
beforeEach(async () => {
let storeType = 'session-file-store'
let storeOptions = { path: './_data' }
let store = new GenericStore(storeType, storeOptions)
await store.setProm('0', { 'a': 0 })
await store.setProm('1', { 'b': 1 })
await store.setProm('2', { 'c': 2 })
store2 = store
});
test('all methods happy path', async () => {
let store = store2
let length = await store.lengthProm()
expect(length).toBe(3)
// redis support .all while file-store doesnt
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({a:0})]))
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({b:1})]))
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({c:2})]))
let data = await store.getProm('1')
expect(data).toEqual(expect.objectContaining({ 'b': 1 }))
await store.setProm('1', {})
await expect(store.getProm('1')).resolves.toEqual(expect.objectContaining({}))
await store.touchProm('1', { "a": 99 })
await expect(store.getProm('1')).resolves.toEqual(expect.objectContaining({}))
await expect(store.lengthProm()).resolves.toBe(3)
await store.destroyProm('1')
await expect(store.lengthProm()).resolves.toBe(2)
// redis resolve to undefined while file-store rejects
await expect(store.getProm('1')).rejects.toThrow()
await store.clearProm()
expect(await store.lengthProm()).toBe(0)
});
test('get when key doesnt exist: error', async () => {
let store = store2
let data = await store.getProm('2')
expect(data).toEqual(expect.objectContaining({ 'c': 2 }))
await expect(store.getProm('99')).rejects.toThrow('s')
await store.destroyProm('2')
});
});
let store3;
describe('tests for file-store and kv', () => {
beforeEach(async () => {
fs.rmdirSync('./_data', { recursive: true });
let storeType = 'session-file-store'
let storeOptions = { path: './_data' }
store = new GenericStore(storeType, storeOptions)
await store.setProm('0', { 'a': 0 })
await store.setProm('1', { 'b': 1 })
await store.setProm('2', { 'c': 2 })
store3 = store
});
test('all methods happy path', async () => {
let store = store3
let kv = await store.lockProm('0')
// redis support .all while file-store doesnt
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({a:0})]))
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({b:1})]))
// await expect(store.allProm()).resolves.toEqual(expect.arrayContaining([expect.objectContaining({c:2})]))
let value = await kv.getProm()
expect(value).toEqual(expect.objectContaining({ 'a': 0 }))
await kv.setProm({})
await expect(kv.getProm()).resolves.toEqual(expect.objectContaining({}))
await kv.touchProm({ "a": 99 })
await expect(kv.getProm()).resolves.toEqual(expect.objectContaining({}))
await expect(kv.lengthProm()).resolves.toBe(3)
await kv.destroyProm()
await expect(kv.lengthProm()).resolves.toBe(2)
// redis resolve to undefined while file-store rejects
await expect(kv.getProm()).rejects.toThrow()
await kv.clearProm()
expect(await kv.lengthProm()).toBe(0)
await kv.unlockProm()
});
test('lock, increment, set, get ', async () => {
let store = store3
let kv = await store.lockProm('0', true)
let data = await kv.getProm()
data.a++
await kv.setProm(data)
await kv.unlockProm()
data = await store.getProm('0')
expect(data).toEqual(expect.objectContaining(data))
});
test('three locks for the same key: kv, kv1 & kv2. kv1.wait=false so throws. kv2.wait=true so waits', async () => {
let store = store3
let kv = await store.lockProm('0')
let data = await kv.getProm()
let kv1 = store.lockProm('0', {wait:false})
await expect(kv1).rejects.toThrow('the resource with key is already locked: 0')
let kv2 = store.lockProm('0')
await kv.unlockProm()
return kv2.then(async (kv2) => {
await expect(kv2.getProm()).resolves.toEqual(expect.objectContaining(data))
await kv2.unlockProm()
})
});
test('kv locks. store.get with store.wait=false so throw', async () => {
let store = store3
let kv = await store.lockProm('0', {wait:false})
await expect(store.getProm('0', {wait: false})).rejects.toThrow('the resource with key is already locked: 0')
await kv.unlockProm()
await expect(store.getProm('0', {wait: false})).resolves.toEqual(expect.objectContaining({a: 0}))
});
});