-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
chained-batch-test.js
279 lines (246 loc) · 7.27 KB
/
chained-batch-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
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
var db
function collectBatchOps (batch) {
var _put = batch._put
, _del = batch._del
, _operations = []
if (typeof _put !== 'function' || typeof _del !== 'function')
return batch._operations
batch._put = function (key, value) {
_operations.push({ type: 'put', key: key, value: value })
return _put.apply(this, arguments)
}
batch._del = function (key) {
_operations.push({ type: 'del', key: key })
return _del.apply(this, arguments)
}
return _operations
}
module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp)
test('setUp db', function (t) {
db = leveldown(testCommon.location())
db.open(t.end.bind(t))
})
}
module.exports.args = function (test) {
test('test batch#put() with missing `value`', function (t) {
db.batch().put('foo1')
t.end()
})
test('test batch#put() with null `value`', function (t) {
db.batch().put('foo1', null)
t.end()
})
test('test batch#put() with missing `key`', function (t) {
try {
db.batch().put(undefined, 'foo1')
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#put() with null `key`', function (t) {
try {
db.batch().put(null, 'foo1')
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#put() with missing `key` and `value`', function (t) {
try {
db.batch().put()
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#del() with missing `key`', function (t) {
try {
db.batch().del()
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#del() with null `key`', function (t) {
try {
db.batch().del(null)
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#del() with null `key`', function (t) {
try {
db.batch().del(null)
} catch (err) {
t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#clear() doesn\'t throw', function (t) {
db.batch().clear()
t.end()
})
test('test batch#write() with no callback', function (t) {
try {
db.batch().write()
} catch (err) {
t.equal(err.message, 'write() requires a callback argument', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#put() after write()', function (t) {
var batch = db.batch().put('foo', 'bar')
batch.write(function () {})
try {
batch.put('boom', 'bang')
} catch (err) {
t.equal(err.message, 'write() already called on this batch', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#del() after write()', function (t) {
var batch = db.batch().put('foo', 'bar')
batch.write(function () {})
try {
batch.del('foo')
} catch (err) {
t.equal(err.message, 'write() already called on this batch', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#clear() after write()', function (t) {
var batch = db.batch().put('foo', 'bar')
batch.write(function () {})
try {
batch.clear()
} catch (err) {
t.equal(err.message, 'write() already called on this batch', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test batch#write() after write()', function (t) {
var batch = db.batch().put('foo', 'bar')
batch.write(function () {})
try {
batch.write(function (err) {})
} catch (err) {
t.equal(err.message, 'write() already called on this batch', 'correct error message')
return t.end()
}
t.fail('should have thrown')
t.end()
})
test('test serialize object', function (t) {
var batch = db.batch()
, ops = collectBatchOps(batch)
batch
.put({ foo: 'bar' }, { beep: 'boop' })
.del({ bar: 'baz' })
ops.forEach(function (op) {
t.ok(op.key, '.key is set for .put and .del operations')
if (op.type === 'put') {
t.ok(op.value, '.value is set for .put operation')
}
})
t.end()
})
test('test serialize buffer', function (t) {
var batch = db.batch()
, ops = collectBatchOps(batch)
batch
.put(Buffer('foo'), Buffer('bar'))
.del(Buffer('baz'))
t.equal(ops[0].key.toString(), 'foo')
t.equal(ops[0].value.toString(), 'bar')
t.equal(ops[1].key.toString(), 'baz')
t.end()
})
test('test custom _serialize*', function (t) {
var _db = Object.create(db)
_db._serializeKey = _db._serializeValue = function (data) { return data }
var batch = _db.batch()
, ops = collectBatchOps(batch)
batch
.put({ foo: 'bar' }, { beep: 'boop' })
.del({ bar: 'baz' })
t.deepEqual(ops, [
{ type: 'put', key: { foo: 'bar' }, value: { beep: 'boop' } }
, { type: 'del', key: { bar: 'baz' } }
])
t.end()
})
}
module.exports.batch = function (test, testCommon) {
test('test basic batch', function (t) {
db.batch(
[
{ type: 'put', key: 'one', value: '1' }
, { type: 'put', key: 'two', value: '2' }
, { type: 'put', key: 'three', value: '3' }
]
, function (err) {
t.error(err)
db.batch()
.put('1', 'one')
.del('2', 'two')
.put('3', 'three')
.clear()
.put('one', 'I')
.put('two', 'II')
.del('three')
.put('foo', 'bar')
.write(function (err) {
t.error(err)
testCommon.collectEntries(
db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
, function (err, data) {
t.error(err)
t.equal(data.length, 3, 'correct number of entries')
var expected = [
{ key: 'foo', value: 'bar' }
, { key: 'one', value: 'I' }
, { key: 'two', value: 'II' }
]
t.deepEqual(data, expected)
t.end()
}
)
})
}
)
})
}
module.exports.tearDown = function (test, testCommon) {
test('tearDown', function (t) {
db.close(testCommon.tearDown.bind(null, t))
})
}
module.exports.all = function (leveldown, test, testCommon) {
module.exports.setUp(leveldown, test, testCommon)
module.exports.args(test)
module.exports.batch(test, testCommon)
module.exports.tearDown(test, testCommon)
}