-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
235 lines (196 loc) · 5.47 KB
/
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
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
import test from 'socket:test'
import process from 'socket:process'
import Indexed from './index.js'
const db = await Indexed.open('test')
test('open', async t => {
const databases = await Indexed.list()
console.log(databases.length, 'some databases')
for (const { name } of databases) await Indexed.drop(name)
console.log((await window.indexedDB.databases()).length, 'no databases')
t.ok(db, 'created db')
})
test('drop table', async t => {
let db2 = await Indexed.open('test2')
t.ok(db2, 'created db')
await db2.put('foo', 100)
await Indexed.drop('test2')
db2 = await Indexed.open('test2')
t.equal((await db2.count()).data, 0, 'test2 has 0 rows after being dropped')
})
test('put', async t => {
const { err } = await db.put('foo', 100)
t.ok(!err, 'a value was added to the database')
})
test('get/has', async t => {
{
const { err, data } = await db.get('foo')
t.ok(!err, 'no error getting value')
t.ok(data === 100, 'the same value put was retrieved')
}
{
const { err, data } = await db.has('foo')
t.ok(!err, 'no error getting value')
t.ok(data === true, 'the store has the key')
}
})
test('get a key that does not exist', async t => {
const { err } = await db.get('quxx')
t.ok(err, 'no such key')
})
test('delete', async t => {
const { err } = await db.del('foo')
t.ok(!err, 'the key was successfully deleted')
})
test('batch puts', async t => {
const { err } = await db.batch([
{ type: 'put', key: 'a', value: 1 },
{ type: 'put', key: 'b', value: 2 }
])
t.ok(!err, 'batch with two puts successfully committed')
t.equal((await db.count()).data, 100022, 'two items counted')
})
test('get from batch', async t => {
{
const { err, data } = await db.get('a')
t.ok(!err, 'no error retrieving first key committed with last batch')
t.ok(data === 1, 'the correct value from the first put was retrieved')
}
{
const { err, data } = await db.get('b')
t.ok(!err, 'no error retrieving second key committed with last batch')
t.ok(data === 2, 'the correct value from the second put was retrieved')
}
})
test('put perf...', async t => {
const DATA = {
squadName: 'Super hero squad',
homeTown: 'Metro City',
formed: 2016,
secretBase: 'Super tower',
active: true,
members: [
{
name: 'Molecule Man',
age: 29,
secretIdentity: 'Dan Jukes',
powers: ['Radiation resistance', 'Turning tiny', 'Radiation blast']
},
{
name: 'Madame Uppercut',
age: 39,
secretIdentity: 'Jane Wilson',
powers: [
'Million tonne punch',
'Damage resistance',
'Superhuman reflexes'
]
},
{
name: 'Eternal Flame',
age: 1000000,
secretIdentity: 'Unknown',
powers: [
'Immortality',
'Heat Immunity',
'Inferno',
'Teleportation',
'Interdimensional travel'
]
}
]
}
const MAX = 100_000
const now = Date.now()
for (let i = 0; i < MAX; i++) {
await db.put('K' + i, DATA)
}
t.ok(true, `PUT TIME: ${Date.now() - now}ms`)
})
test('get perf...', async t => {
const MAX = 100_000
const now = Date.now()
for (let i = 0; i < MAX; i++) {
await db.get('K' + i)
}
t.ok(true, `GET TIME: ${Date.now() - now}ms`)
})
test('read range', async t => {
for (let i = 0; i < 20; i++) {
await db.put('KX' + i, {})
}
// eslint-disable-next-line
await new Promise(async resolve => {
const { err, events } = await db.read({
gt: 'KX',
lt: 'KXÿ'
})
t.ok(!err, 'an iterator was successfully created')
t.ok(events, 'an events object was recieved')
let items = 0
const now = Date.now()
events.ondata = (key, value) => {
items++
}
events.onerror = err => {
t.ok(false, '', `an error occurred ${err.message}`)
}
events.onend = () => {
t.equal(items, 20, `${items} total items`)
t.ok(true, `${Date.now() - now}ms to read ${items} rows`)
resolve()
}
})
// eslint-disable-next-line
await new Promise(async resolve => {
const { err, events } = await db.read({
gt: 'KX1',
lt: 'KX1ÿ'
})
t.ok(!err, 'an iterator was successfully created')
t.ok(events, 'an events object was recieved')
let items = 0
const now = Date.now()
events.ondata = (key, value) => {
items++
}
events.onerror = err => {
t.ok(false, '', `an error occurred ${err.message}`)
}
events.onend = () => {
t.equal(items, 10, `${items} total items (gt)`)
t.ok(true, `${Date.now() - now}ms to read ${items} rows`)
resolve()
}
})
// eslint-disable-next-line
await new Promise(async resolve => {
const { err, events } = await db.read({
gte: 'KX1',
lte: 'KX1ÿ'
})
t.ok(!err, 'an iterator was successfully created')
t.ok(events, 'an events object was recieved')
let items = 0
const now = Date.now()
events.ondata = (key, value) => {
items++
}
events.onerror = err => {
t.ok(false, '', `an error occurred ${err.message}`)
}
events.onend = () => {
t.equal(items, 11, `${items} total items (gte)`)
t.ok(true, `${Date.now() - now}ms to read ${items} rows`)
resolve()
}
})
})
test('readall...', async t => {
const { err, data } = await db.readAll()
t.ok(!err, 'no errors reading all')
t.ok(data.size > 100_000, 'got all records')
})
test('cleanup', t => {
process.exit(0)
window.close()
})