-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GistDatabase.test.ts
101 lines (88 loc) · 2.41 KB
/
GistDatabase.test.ts
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
import { GistDatabase, GistResponse } from '../src'
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
describe('GistDatabase', () => {
let db: GistDatabase
beforeAll(async () => {
db = new GistDatabase({
token: process.env.GIST_TOKEN
})
await db.init()
})
afterAll(async () => {
await db.destroy()
})
it('sets and gets', async () => {
const res = await db.set('test_one', {
value: {
name: 'test_one'
}
})
expect(res).toMatchObject({
value: {
name: 'test_one'
},
id: expect.any(String),
gist: expect.any(Object)
})
expect(await db.get('test_one')).toEqual({
value: {
name: 'test_one'
},
id: expect.any(String),
gist: expect.any(Object)
})
})
it('deletes', async () => {
await db.set('test_two', {
value: {
name: 'test_two'
}
})
expect(await db.get('test_two')).toEqual({
value: {
name: 'test_two'
},
id: expect.any(String),
gist: expect.any(Object)
})
await db.delete('test_two')
expect(await db.get('test_two')).toBeUndefined()
})
it('key with a ttl gets deleted', async () => {
const res = await db.set('test_ttl', {
value: {},
ttl: 250
})
await sleep(500)
expect(await db.get('test_ttl')).toBeUndefined()
const found = (await db.gistApi(`/gists/${res.id}`, 'GET')) as GistResponse
expect(found).toEqual({})
}, 10000)
it('gets and deletes many', async () => {
await db.set('test_many_one', {})
await db.set('test_many_two', {})
expect(await db.getMany(['test_many_one', 'test_many_two'])).toHaveLength(2)
await db.deleteMany(['test_many_one', 'test_many_two'])
expect(await db.getMany(['test_many_one', 'test_many_two'])).toEqual([
undefined,
undefined
])
}, 10000)
})
it('get and set and del', () => {
const obj = {
a: 1,
b: {
c: {}
}
}
expect(GistDatabase.get(obj, ['a'])).toBe(1)
expect(GistDatabase.get(obj, ['b', 'c'])).toEqual({})
let res = GistDatabase.set(obj, ['a'], 2)
expect(GistDatabase.get(res, ['a'])).toBe(2)
res = GistDatabase.set(res, ['b', 'c'], { d: 3 })
expect(GistDatabase.get(res, ['b', 'c'])).toEqual({ d: 3 })
res = GistDatabase.del(res, ['b', 'c'])
expect(GistDatabase.get(res, ['b', 'c'])).toBeUndefined()
expect(GistDatabase.get(res, ['a'])).toBe(2)
})