generated from hyper63/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.test.js
More file actions
357 lines (300 loc) · 8.5 KB
/
adapter.test.js
File metadata and controls
357 lines (300 loc) · 8.5 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { DB } from './deps.js'
import adapter from './adapter.js'
import { assert, assertEquals, assertObjectMatch, cachePort } from './dev_deps.js'
/**
* Wrap adapter with the port to ensure inputs and outputs are also
* valid and asserted
*/
const cache = cachePort(adapter(new DB(`./test.db`)))
const test = Deno.test
test('adapter', async (t) => {
await t.step('ttl', async (t) => {
await t.step('should expire the document', async () => {
// setup
const store = 'test'
await cache.createStore(store)
await cache.createDoc({
store,
key: 'item-8',
value: { name: 'Temp Item' },
ttl: '1',
})
await new Promise((resolve) => setTimeout(resolve, 10))
const team = await cache.getDoc({
store,
key: 'item-8',
})
assertEquals(team.ok, false)
assertEquals(team.status, 404)
await cache.createDoc({
store,
key: 'item-9',
value: { name: 'Temp Item' },
ttl: '1',
})
await cache.createDoc({
store,
key: 'item-10',
value: { name: 'Temp Item' },
ttl: '1',
})
await new Promise((resolve) => setTimeout(resolve, 10))
const res = await cache.listDocs({
store,
pattern: 'item-*',
})
assertEquals(res.docs.length, 0)
// clean up
await cache.destroyStore('test')
})
await t.step('should expire only documents who ttl has elapsed', async () => {
// setup
const store = 'test'
await cache.createStore(store)
// this document will be expired and removed
await cache.createDoc({
store,
key: 'item-8',
value: { name: 'Temp Item' },
ttl: '1',
})
// these will not
await cache.createDoc({
store,
key: 'item-9',
value: { name: 'Temp Item 9' },
ttl: String(1000 * 60 * 60),
})
await cache.createDoc({
store,
key: 'item-10',
value: { name: 'Temp Item 10' },
ttl: String(1000 * 60 * 60),
})
await new Promise((resolve) => setTimeout(resolve, 10))
const res = await cache.listDocs({
store,
pattern: 'item-*',
})
assert(res.ok)
assertEquals(res.docs.length, 2)
// clean up
await cache.destroyStore('test')
})
await t.step('should return cached docs whose ttl has not elapsed', async () => {
// setup
const store = 'test'
await cache.createStore(store)
await cache.createDoc({
store,
key: 'item-10',
value: { name: 'Temp Item 2' },
ttl: String(1000 * 60 * 60),
})
const team = await cache.getDoc({
store,
key: 'item-10',
})
assertEquals(team.name, 'Temp Item 2')
const res = await cache.listDocs({
store,
pattern: 'item-10',
})
assertEquals(res.docs.length, 1)
// clean up
await cache.destroyStore('test')
})
await t.step('should immediately expire doc with negative ttl', async () => {
// setup
await cache.createStore('test')
await cache.createDoc({
store: 'test',
key: '1',
value: { type: 'movie', title: 'Ghostbusters' },
ttl: String(-100),
})
await new Promise((resolve) => setTimeout(resolve, 10))
const res = await cache.getDoc({
store: 'test',
key: '1',
})
assertObjectMatch(res, {
ok: false,
status: 404,
})
// clean up
await cache.destroyStore('test')
})
})
await t.step('createStore', async (t) => {
await t.step('should escape/quote special characters', async () => {
const res = await cache.createStore('test-special_default~characters')
assert(res.ok)
// clean up
await cache.destroyStore('test-special_default~characters')
})
await t.step('should 409 if cache already exists', async () => {
// setup
await cache.createStore('test')
const res = await cache.createStore('test')
assert(!res.ok)
assertEquals(res.status, 409)
// clean up
await cache.destroyStore('test')
})
})
await t.step('createDoc', async (t) => {
await t.step('should create the cache doc', async () => {
// setup
await cache.createStore('test')
const res = await cache.createDoc({
store: 'test',
key: '1',
value: { type: 'movie', title: 'Ghostbusters' },
})
assert(res.ok)
// clean up
await cache.destroyStore('test')
})
})
await t.step('getDoc', async (t) => {
await t.step('should get the cached document', async () => {
// setup
await cache.createStore('test')
await cache.createDoc({
store: 'test',
key: '2',
value: { type: 'movie', title: 'Star Wars' },
})
const res = await cache.getDoc({ store: 'test', key: '2' })
assertEquals(res.type, 'movie')
assertEquals(res.title, 'Star Wars')
// clean up
await cache.destroyStore('test')
})
await t.step('should return a HyperErr with status 404 if not found', async () => {
// setup
await cache.createStore('test')
const res = await cache.getDoc({ store: 'test', key: '3' })
assertObjectMatch(res, {
ok: false,
status: 404,
})
// clean up
await cache.destroyStore('test')
})
})
await t.step('updateDoc', async (t) => {
await t.step('should update the cache doc', async () => {
// setup
await cache.createStore('test')
await cache.createDoc({
store: 'test',
key: '4',
value: { type: 'movie', title: 'Star Trek' },
})
const res = await cache.updateDoc({
store: 'test',
key: '4',
value: { type: 'movie', title: 'Star Trek', year: '1981' },
})
assert(res.ok)
const movie = await cache.getDoc({
store: 'test',
key: '4',
})
assertEquals(movie.year, '1981')
// clean up
await cache.destroyStore('test')
})
test('should upsert the cached document if not found', async () => {
// setup
await cache.createStore('test')
const res = await cache.updateDoc({
store: 'test',
key: '6',
value: { type: 'movie', title: 'The last start fighter', year: '1989' },
})
assert(res.ok)
const movie = await cache.getDoc({
store: 'test',
key: '6',
})
assertEquals(movie.year, '1989')
// clean up
await cache.destroyStore('test')
})
})
await t.step('deleteDoc', async (t) => {
await t.step('should remove the document', async () => {
// setup
await cache.createStore('test')
await cache.createDoc({
store: 'test',
key: '4',
value: { type: 'movie', title: 'Star Trek' },
})
const res = await cache.deleteDoc({
store: 'test',
key: '4',
})
assert(res.ok)
const notFound = await cache.getDoc({
store: 'test',
key: '4',
})
assertObjectMatch(notFound, {
ok: false,
status: 404,
})
// clean up
await cache.destroyStore('test')
})
})
await t.step('listDocs', async (t) => {
await t.step('should return documents whose key matches the pattern', async () => {
// setup
const store = 'test'
await cache.createStore(store)
await cache.createDoc({
store,
key: 'team-1',
value: { name: 'Atlanta Braves' },
})
await cache.createDoc({
store,
key: 'team-2',
value: { name: 'Carolina Panthers' },
})
await cache.createDoc({
store,
key: 'team-3',
value: { name: 'Georgia Bulldogs' },
})
// test
const res = await cache.listDocs({
store: 'test',
pattern: 'team-*',
})
assert(res.ok)
assertEquals(res.docs.length, 3)
// clean up
await cache.destroyStore('test')
})
})
await t.step('destroyStore', async (t) => {
await t.step('should remove the cache store', async () => {
// setup
const store = 'test'
await cache.createStore(store)
const res = await cache.destroyStore(store)
assert(res.ok)
})
test('should return a HyperErr with status 404 if cache store does not exist', async () => {
const store = 'test'
const res = await cache.destroyStore(store)
assert(!res.ok)
assertEquals(res.status, 404)
})
})
})