-
Notifications
You must be signed in to change notification settings - Fork 0
/
all-tests.ts
270 lines (241 loc) · 9.23 KB
/
all-tests.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
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
import { assertEquals, assertNotEquals } from 'jsr:@std/assert'
type StorageKey = string[]
type Chunk = {
key: StorageKey
data: Uint8Array | undefined
}
interface StorageAdapterInterface {
load(key: StorageKey): Promise<Uint8Array | undefined>
save(key: StorageKey, data: Uint8Array): Promise<void>
remove(key: StorageKey): Promise<void>
loadRange(keyPrefix: StorageKey): Promise<Chunk[]>
removeRange(keyPrefix: StorageKey): Promise<void>
}
const PAYLOAD_A = () => new Uint8Array([0, 1, 127, 99, 154, 235])
const PAYLOAD_B = () => new Uint8Array([1, 76, 160, 53, 57, 10, 230])
const PAYLOAD_C = () => new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193])
const LARGE_PAYLOAD = new Uint8Array(100_000).map(() => Math.random() * 256)
type CleanupFunction = () => Promise<void>
export function testAll(
adapter: StorageAdapterInterface,
cleanup: CleanupFunction
) {
Deno.test(`Storage adapter acceptance tests`, async t => {
await cleanup()
await t.step('load', async t => {
await t.step(
'should return undefined if there is no data',
async t => {
const actual = await adapter.load([
'AAAAA',
'sync-state',
'xxxxx'
])
assertEquals(actual, undefined)
}
)
})
await t.step('save and load', async t => {
await t.step('should return data that was saved', async t => {
await adapter.save(['storage-adapter-id'], PAYLOAD_A())
const actual = await adapter.load(['storage-adapter-id'])
assertEquals(actual, PAYLOAD_A())
})
await t.step('should work with composite keys', async t => {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
const actual = await adapter.load([
'AAAAA',
'sync-state',
'xxxxx'
])
assertEquals(actual, PAYLOAD_A())
})
await t.step('should work with a large payload', async t => {
await cleanup()
try {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
LARGE_PAYLOAD
)
} catch (e) {
assertEquals(true, false, e as string)
}
const actual = await adapter.load([
'AAAAA',
'sync-state',
'xxxxx'
])
assertEquals(actual, LARGE_PAYLOAD)
})
})
await t.step('loadRange', async t => {
await cleanup()
await t.step(
'should return an empty array if there is no data',
async () => {
assertEquals(await adapter.loadRange(['AAAAA']), [])
}
)
})
await t.step('save and loadRange', async t => {
await t.step(
'should return all the data that matches the key',
async t => {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
await adapter.save(
['AAAAA', 'snapshot', 'yyyyy'],
PAYLOAD_B()
)
await adapter.save(
['AAAAA', 'sync-state', 'zzzzz'],
PAYLOAD_C()
)
const sortfn = (
a: { key: string[] },
b: { key: string[] }
) => a.key.join().localeCompare(b.key.join())
assertEquals(
(await adapter.loadRange(['AAAAA'])).sort(sortfn),
[
{
key: ['AAAAA', 'sync-state', 'xxxxx'],
data: PAYLOAD_A()
},
{
key: ['AAAAA', 'snapshot', 'yyyyy'],
data: PAYLOAD_B()
},
{
key: ['AAAAA', 'sync-state', 'zzzzz'],
data: PAYLOAD_C()
}
].sort(sortfn)
)
assertEquals(
await adapter.loadRange(['AAAAA', 'sync-state']),
[
{
key: ['AAAAA', 'sync-state', 'xxxxx'],
data: PAYLOAD_A()
},
{
key: ['AAAAA', 'sync-state', 'zzzzz'],
data: PAYLOAD_C()
}
]
)
}
)
await t.step(
'should only load values that match they key',
async t => {
await cleanup()
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
await adapter.save(
['BBBBB', 'sync-state', 'zzzzz'],
PAYLOAD_C()
)
const actual = await adapter.loadRange(['AAAAA'])
assertEquals(actual, [
{
key: ['AAAAA', 'sync-state', 'xxxxx'],
data: PAYLOAD_A()
}
])
assertNotEquals(actual, [
{
key: ['BBBBB', 'sync-state', 'zzzzz'],
data: PAYLOAD_C()
}
])
}
)
})
await t.step('save and remove', async t => {
await t.step('after removing, should be empty', async t => {
await cleanup()
await adapter.save(['AAAAA', 'snapshot', 'xxxxx'], PAYLOAD_A())
await adapter.remove(['AAAAA', 'snapshot', 'xxxxx'])
assertEquals(await adapter.loadRange(['AAAAA']), [])
assertEquals(
await adapter.load(['AAAAA', 'snapshot', 'xxxxx']),
undefined
)
})
})
await t.step('save and save', async t => {
await t.step(
'should overwrite data saved with the same key',
async () => {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_B()
)
assertEquals(
await adapter.loadRange(['AAAAA', 'sync-state']),
[
{
key: ['AAAAA', 'sync-state', 'xxxxx'],
data: PAYLOAD_B()
}
]
)
}
)
})
await t.step('removeRange', async t => {
await t.step('should remove a range of records', async t => {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
await adapter.save(['AAAAA', 'snapshot', 'yyyyy'], PAYLOAD_B())
await adapter.save(
['AAAAA', 'sync-state', 'zzzzz'],
PAYLOAD_C()
)
await adapter.removeRange(['AAAAA', 'sync-state'])
assertEquals(await adapter.loadRange(['AAAAA']), [
{
key: ['AAAAA', 'snapshot', 'yyyyy'],
data: PAYLOAD_B()
}
])
})
await t.step(
"should not remove records that don't match",
async t => {
await adapter.save(
['AAAAA', 'sync-state', 'xxxxx'],
PAYLOAD_A()
)
await adapter.save(
['BBBBB', 'sync-state', 'zzzzz'],
PAYLOAD_B()
)
await adapter.removeRange(['AAAAA'])
const actual = await adapter.loadRange(['BBBBB'])
assertEquals(actual, [
{
key: ['BBBBB', 'sync-state', 'zzzzz'],
data: PAYLOAD_B()
}
])
}
)
})
})
}