This repository was archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathbatch-test.js
More file actions
389 lines (357 loc) · 11.7 KB
/
batch-test.js
File metadata and controls
389 lines (357 loc) · 11.7 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var levelup = require('../lib/levelup')
var errors = levelup.errors
var async = require('async')
var common = require('./common')
var assert = require('referee').assert
var refute = require('referee').refute
var buster = require('bustermove')
buster.testCase('batch()', {
'setUp': common.commonSetUp,
'tearDown': common.commonTearDown,
'batch() with multiple puts': function (done) {
this.openTestDatabase(function (db) {
db.batch([
{ type: 'put', key: 'foo', value: 'afoovalue' },
{ type: 'put', key: 'bar', value: 'abarvalue' },
{ type: 'put', key: 'baz', value: 'abazvalue' }
], function (err) {
refute(err)
async.forEach(['foo', 'bar', 'baz'], function (key, callback) {
db.get(key, function (err, value) {
refute(err)
assert.equals(value, 'a' + key + 'value')
callback()
})
}, done)
})
})
},
'batch() with promise interface': function (done) {
this.openTestDatabase(function (db) {
db.batch([
{ type: 'put', key: 'foo', value: 'afoovalue' },
{ type: 'put', key: 'bar', value: 'abarvalue' },
{ type: 'put', key: 'baz', value: 'abazvalue' }
])
.then(function () {
async.forEach(['foo', 'bar', 'baz'], function (key, callback) {
db.get(key, function (err, value) {
refute(err)
assert.equals(value, 'a' + key + 'value')
callback()
})
}, done)
})
.catch(done)
})
},
'batch() with multiple puts and deletes': function (done) {
this.openTestDatabase(function (db) {
async.series([
function (callback) {
db.batch([
{ type: 'put', key: '1', value: 'one' },
{ type: 'put', key: '2', value: 'two' },
{ type: 'put', key: '3', value: 'three' }
], callback)
},
function (callback) {
db.batch([
{ type: 'put', key: 'foo', value: 'afoovalue' },
{ type: 'del', key: '1' },
{ type: 'put', key: 'bar', value: 'abarvalue' },
{ type: 'del', key: 'foo' },
{ type: 'put', key: 'baz', value: 'abazvalue' }
], callback)
},
function (callback) {
// these should exist
async.forEach(['2', '3', 'bar', 'baz'], function (key, callback) {
db.get(key, function (err, value) {
refute(err)
refute.isNull(value)
callback()
})
}, callback)
},
function (callback) {
// these shouldn't exist
async.forEach(['1', 'foo'], function (key, callback) {
db.get(key, function (err, value) {
assert(err)
assert.isInstanceOf(err, errors.NotFoundError)
refute(value)
callback()
})
}, callback)
}
], done)
})
},
'batch() with chained interface': function (done) {
this.openTestDatabase(function (db) {
db.put('1', 'one', function (err) {
refute(err)
db.batch()
.put('one', '1')
.del('two')
.put('three', '3')
.clear()
.del('1')
.put('2', 'two')
.put('3', 'three')
.del('3')
.write(function (err) {
refute(err)
async.forEach([ 'one', 'three', '1', '2', '3' ], function (key, callback) {
db.get(key, function (err) {
if ([ 'one', 'three', '1', '3' ].indexOf(key) > -1) { assert(err) } else { refute(err) }
callback()
})
}, done)
})
})
})
},
'batch() with chained interface - options': function (done) {
this.openTestDatabase(function (db) {
var batch = db.batch()
var write = batch.batch.write.bind(batch.batch)
batch.batch.write = function (options, cb) {
assert.equals(options, { foo: 'bar' })
write(options, cb)
}
batch.put('one', '1')
.write({ foo: 'bar' }, function (err) {
refute(err)
done()
})
})
},
'batch() with chained promise interface - options': function (done) {
this.openTestDatabase(function (db) {
var batch = db.batch()
var write = batch.batch.write.bind(batch.batch)
batch.batch.write = function (options, cb) {
assert.equals(options, { foo: 'bar' })
write(options, cb)
}
batch.put('one', '1')
.write({ foo: 'bar' })
.then(done)
.catch(done)
})
},
'batch() with chained promise interface': function (done) {
this.openTestDatabase(function (db) {
db.put('1', 'one', function (err) {
refute(err)
db.batch()
.put('one', '1')
.del('two')
.put('three', '3')
.clear()
.del('1')
.put('2', 'two')
.put('3', 'three')
.del('3')
.write()
.then(function () {
async.forEach([ 'one', 'three', '1', '2', '3' ], function (key, callback) {
db.get(key, function (err) {
if ([ 'one', 'three', '1', '3' ].indexOf(key) > -1) { assert(err) } else { refute(err) }
callback()
})
}, done)
})
.catch(done)
})
})
},
'batch() exposes ops queue length': function (done) {
this.openTestDatabase(function (db) {
var batch = db.batch()
.put('one', '1')
.del('two')
.put('three', '3')
assert.equals(batch.length, 3)
batch.clear()
assert.equals(batch.length, 0)
batch
.del('1')
.put('2', 'two')
.put('3', 'three')
.del('3')
assert.equals(batch.length, 4)
done()
})
},
'batch() with can manipulate data from put()': function (done) {
// checks encoding and whatnot
this.openTestDatabase(function (db) {
async.series(
[
db.put.bind(db, '1', 'one'),
db.put.bind(db, '2', 'two'),
db.put.bind(db, '3', 'three'),
function (callback) {
db.batch([
{ type: 'put', key: 'foo', value: 'afoovalue' },
{ type: 'del', key: '1' },
{ type: 'put', key: 'bar', value: 'abarvalue' },
{ type: 'del', key: 'foo' },
{ type: 'put', key: 'baz', value: 'abazvalue' }
], callback)
},
function (callback) {
// these should exist
async.forEach(['2', '3', 'bar', 'baz'], function (key, callback) {
db.get(key, function (err, value) {
refute(err)
refute.isNull(value)
callback()
})
}, callback)
},
function (callback) {
// these shouldn't exist
async.forEach(['1', 'foo'], function (key, callback) {
db.get(key, function (err, value) {
assert(err)
assert.isInstanceOf(err, errors.NotFoundError)
refute(value)
callback()
})
}, callback)
}
], done)
})
},
'batch() data can be read with get() and del()': function (done) {
this.openTestDatabase(function (db) {
async.series([
function (callback) {
db.batch([
{ type: 'put', key: '1', value: 'one' },
{ type: 'put', key: '2', value: 'two' },
{ type: 'put', key: '3', value: 'three' }
], callback)
},
db.del.bind(db, '1', 'one'),
function (callback) {
// these should exist
async.forEach(['2', '3'], function (key, callback) {
db.get(key, function (err, value) {
refute(err)
refute.isNull(value)
callback()
})
}, callback)
},
function (callback) {
// this shouldn't exist
db.get('1', function (err, value) {
assert(err)
assert.isInstanceOf(err, errors.NotFoundError)
refute(value)
callback()
})
}
], done)
})
},
'chained batch() arguments': {
'setUp': function (done) {
this.openTestDatabase(function (db) {
this.db = db
this.batch = db.batch()
done()
}.bind(this))
},
'test batch#put() with missing `value`': function () {
// value = undefined
assert.exception(this.batch.put.bind(this.batch, 'foo1'), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'value cannot be `null` or `undefined`') { return false }
return true
})
assert.exception(this.batch.put.bind(this.batch, 'foo1', null), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'value cannot be `null` or `undefined`') { return false }
return true
})
},
'test batch#put() with missing `key`': function () {
// key = undefined
assert.exception(this.batch.put.bind(this.batch, undefined, 'foo1'), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
// key = null
assert.exception(this.batch.put.bind(this.batch, null, 'foo1'), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
},
'test batch#put() with missing `key` and `value`': function () {
// undefined
assert.exception(this.batch.put.bind(this.batch), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
// null
assert.exception(this.batch.put.bind(this.batch, null, null), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
},
'test batch#del() with missing `key`': function () {
// key = undefined
assert.exception(this.batch.del.bind(this.batch, undefined, 'foo1'), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
// key = null
assert.exception(this.batch.del.bind(this.batch, null, 'foo1'), function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'key cannot be `null` or `undefined`') { return false }
return true
})
},
'test batch#write() with no callback': function () {
this.batch.write() // should not cause an error with no cb
},
'test batch operations after write()': {
'setUp': function (done) {
this.batch.put('foo', 'bar').put('boom', 'bang').del('foo').write(done)
this.verify = function (cb) {
assert.exception(cb, function (err) {
if (err.name !== 'WriteError') { return false }
if (err.message !== 'write() already called on this batch') { return false }
return true
})
}
},
'test put()': function () {
this.verify(function () {
this.batch.put('whoa', 'dude')
}.bind(this))
},
'test del()': function () {
this.verify(function () {
this.batch.del('foo')
}.bind(this))
},
'test clear()': function () {
this.verify(function () {
this.batch.clear()
}.bind(this))
}
}
}
})