UNPKG

19.1 kBJavaScriptView Raw
1const tap = require('tap')
2 , sinon = require('sinon')
3 , util = require('util')
4 , testCommon = require('./testCommon')
5 , AbstractLevelDOWN = require('./').AbstractLevelDOWN
6 , AbstractIterator = require('./').AbstractIterator
7 , AbstractChainedBatch = require('./').AbstractChainedBatch
8
9
10function factory (location) {
11 return new AbstractLevelDOWN(location)
12}
13
14/*** compatibility with basic LevelDOWN API ***/
15
16require('./abstract/error-test').all(factory, tap.test, testCommon)
17
18require('./abstract/leveldown-test').args(factory, tap.test, testCommon)
19
20require('./abstract/open-test').args(factory, tap.test, testCommon)
21
22require('./abstract/del-test').setUp(factory, tap.test, testCommon)
23require('./abstract/del-test').args(tap.test)
24
25require('./abstract/isExists-test').setUp(factory, tap.test, testCommon)
26require('./abstract/isExists-test').args(tap.test)
27
28require('./abstract/get-test').setUp(factory, tap.test, testCommon)
29require('./abstract/get-test').args(tap.test)
30
31require('./abstract/put-test').setUp(factory, tap.test, testCommon)
32require('./abstract/put-test').args(tap.test)
33
34require('./abstract/put-get-del-test').setUp(factory, tap.test, testCommon)
35require('./abstract/put-get-del-test').errorKeys(tap.test)
36//require('./abstract/put-get-del-test').nonErrorKeys(tap.test, testCommon)
37require('./abstract/put-get-del-test').errorValues(tap.test)
38//require('./abstract/test/put-get-del-test').nonErrorKeys(tap.test, testCommon)
39require('./abstract/put-get-del-test').tearDown(tap.test, testCommon)
40
41require('./abstract/approximate-size-test').setUp(factory, tap.test, testCommon)
42require('./abstract/approximate-size-test').args(tap.test)
43
44require('./abstract/batch-test').setUp(factory, tap.test, testCommon)
45require('./abstract/batch-test').args(tap.test)
46
47require('./abstract/chained-batch-test').setUp(factory, tap.test, testCommon)
48require('./abstract/chained-batch-test').args(tap.test)
49
50require('./abstract/close-test').close(factory, tap.test, testCommon)
51
52require('./abstract/iterator-test').setUp(factory, tap.test, testCommon)
53require('./abstract/iterator-test').args(tap.test)
54require('./abstract/iterator-test').sequence(tap.test)
55
56/*** extensibility ***/
57
58tap.test('test core extensibility', function (t) {
59 function Test (location) {
60 AbstractLevelDOWN.call(this, location)
61 t.equal(this.location, location, 'location set on `this`')
62 t.end()
63 }
64
65 util.inherits(Test, AbstractLevelDOWN)
66
67 ;new Test('foobar')
68})
69
70tap.test('test open() extensibility', function (t) {
71 var spy = sinon.spy()
72 , expectedCb = function () {}
73 , expectedOptions = { createIfMissing: true, errorIfExists: false }
74 , test
75
76 function Test (location) {
77 AbstractLevelDOWN.call(this, location)
78 }
79
80 util.inherits(Test, AbstractLevelDOWN)
81
82 Test.prototype._open = spy
83
84 test = new Test('foobar')
85 test.open(expectedCb)
86
87 t.equal(spy.callCount, 1, 'got _open() call')
88 t.equal(spy.getCall(0).thisValue, test, '`this` on _open() was correct')
89 t.equal(spy.getCall(0).args.length, 2, 'got two arguments')
90 t.deepEqual(spy.getCall(0).args[0], expectedOptions, 'got default options argument')
91 //t.equal(spy.getCall(0).args[1], expectedCb, 'got expected cb argument')
92
93 test.open({ options: 1 }, expectedCb)
94
95 expectedOptions.options = 1
96
97 t.equal(spy.callCount, 2, 'got _open() call')
98 t.equal(spy.getCall(1).thisValue, test, '`this` on _open() was correct')
99 t.equal(spy.getCall(1).args.length, 2, 'got two arguments')
100 t.deepEqual(spy.getCall(1).args[0], expectedOptions, 'got expected options argument')
101 //t.equal(spy.getCall(1).args[1], expectedCb, 'got expected cb argument')
102 t.end()
103})
104
105tap.test('test close() extensibility', function (t) {
106 var spy = sinon.spy()
107 , expectedCb = function () {}
108 , test
109
110 function Test (location) {
111 AbstractLevelDOWN.call(this, location)
112 }
113
114 util.inherits(Test, AbstractLevelDOWN)
115
116 Test.prototype._close = spy
117
118 test = new Test('foobar')
119 test.close(expectedCb)
120
121 t.equal(spy.callCount, 1, 'got _close() call')
122 t.equal(spy.getCall(0).thisValue, test, '`this` on _close() was correct')
123 t.equal(spy.getCall(0).args.length, 1, 'got one arguments')
124 //t.equal(spy.getCall(0).args[0], expectedCb, 'got expected cb argument')
125 t.end()
126})
127
128tap.test('test get() extensibility', function (t) {
129 var spy = sinon.spy()
130 , expectedCb = function () {}
131 , expectedOptions = { asBuffer: true }
132 , expectedKey = 'a key'
133 , test
134
135 function Test (location) {
136 AbstractLevelDOWN.call(this, location)
137 }
138
139 util.inherits(Test, AbstractLevelDOWN)
140
141 Test.prototype._get = spy
142
143 test = new Test('foobar')
144 test.get(expectedKey, expectedCb)
145
146 t.equal(spy.callCount, 1, 'got _get() call')
147 t.equal(spy.getCall(0).thisValue, test, '`this` on _get() was correct')
148 t.equal(spy.getCall(0).args.length, 3, 'got three arguments')
149 t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument')
150 t.deepEqual(spy.getCall(0).args[1], expectedOptions, 'got default options argument')
151 t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument')
152
153 test.get(expectedKey, { options: 1 }, expectedCb)
154
155 expectedOptions.options = 1
156
157 t.equal(spy.callCount, 2, 'got _get() call')
158 t.equal(spy.getCall(1).thisValue, test, '`this` on _get() was correct')
159 t.equal(spy.getCall(1).args.length, 3, 'got three arguments')
160 t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument')
161 t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument')
162 t.equal(spy.getCall(1).args[2], expectedCb, 'got expected cb argument')
163 t.end()
164})
165
166tap.test('test del() extensibility', function (t) {
167 var spy = sinon.spy()
168 , expectedCb = function () {}
169 , expectedOptions = { options: 1 }
170 , expectedKey = 'a key'
171 , test
172
173 function Test (location) {
174 AbstractLevelDOWN.call(this, location)
175 }
176
177 util.inherits(Test, AbstractLevelDOWN)
178
179 Test.prototype._del = spy
180
181 test = new Test('foobar')
182 test.del(expectedKey, expectedCb)
183
184 t.equal(spy.callCount, 1, 'got _del() call')
185 t.equal(spy.getCall(0).thisValue, test, '`this` on _del() was correct')
186 t.equal(spy.getCall(0).args.length, 3, 'got three arguments')
187 t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument')
188 t.deepEqual(spy.getCall(0).args[1], {}, 'got blank options argument')
189 t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument')
190
191 test.del(expectedKey, expectedOptions, expectedCb)
192
193 t.equal(spy.callCount, 2, 'got _del() call')
194 t.equal(spy.getCall(1).thisValue, test, '`this` on _del() was correct')
195 t.equal(spy.getCall(1).args.length, 3, 'got three arguments')
196 t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument')
197 t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument')
198 t.equal(spy.getCall(1).args[2], expectedCb, 'got expected cb argument')
199 t.end()
200})
201
202tap.test('test put() extensibility', function (t) {
203 var spy = sinon.spy()
204 , expectedCb = function () {}
205 , expectedOptions = { options: 1 }
206 , expectedKey = 'a key'
207 , expectedValue = 'a value'
208 , test
209
210 function Test (location) {
211 AbstractLevelDOWN.call(this, location)
212 }
213
214 util.inherits(Test, AbstractLevelDOWN)
215
216 Test.prototype._put = spy
217
218 test = new Test('foobar')
219 test.put(expectedKey, expectedValue, expectedCb)
220
221 t.equal(spy.callCount, 1, 'got _put() call')
222 t.equal(spy.getCall(0).thisValue, test, '`this` on _put() was correct')
223 t.equal(spy.getCall(0).args.length, 4, 'got four arguments')
224 t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument')
225 t.equal(spy.getCall(0).args[1], expectedValue, 'got expected value argument')
226 t.deepEqual(spy.getCall(0).args[2], {}, 'got blank options argument')
227 t.equal(spy.getCall(0).args[3], expectedCb, 'got expected cb argument')
228
229 test.put(expectedKey, expectedValue, expectedOptions, expectedCb)
230
231 t.equal(spy.callCount, 2, 'got _put() call')
232 t.equal(spy.getCall(1).thisValue, test, '`this` on _put() was correct')
233 t.equal(spy.getCall(1).args.length, 4, 'got four arguments')
234 t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument')
235 t.equal(spy.getCall(1).args[1], expectedValue, 'got expected value argument')
236 t.deepEqual(spy.getCall(1).args[2], expectedOptions, 'got blank options argument')
237 t.equal(spy.getCall(1).args[3], expectedCb, 'got expected cb argument')
238 t.end()
239})
240
241tap.test('test approximateSize() extensibility', function (t) {
242 var spy = sinon.spy()
243 , expectedCb = function () {}
244 , expectedStart = 'a start'
245 , expectedEnd = 'an end'
246 , test
247
248 function Test (location) {
249 AbstractLevelDOWN.call(this, location)
250 }
251
252 util.inherits(Test, AbstractLevelDOWN)
253
254 Test.prototype._approximateSize = spy
255
256 test = new Test('foobar')
257 test.approximateSize(expectedStart, expectedEnd, expectedCb)
258
259 t.equal(spy.callCount, 1, 'got _approximateSize() call')
260 t.equal(spy.getCall(0).thisValue, test, '`this` on _approximateSize() was correct')
261 t.equal(spy.getCall(0).args.length, 3, 'got three arguments')
262 t.equal(spy.getCall(0).args[0], expectedStart, 'got expected start argument')
263 t.equal(spy.getCall(0).args[1], expectedEnd, 'got expected end argument')
264 t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument')
265 t.end()
266})
267
268tap.test('test batch() extensibility', function (t) {
269 var spy = sinon.spy()
270 , expectedCb = function () {}
271 , expectedOptions = { options: 1 }
272 , expectedArray = [ 1, 2 ]
273 , test
274
275 function Test (location) {
276 AbstractLevelDOWN.call(this, location)
277 }
278
279 util.inherits(Test, AbstractLevelDOWN)
280
281 Test.prototype._batch = spy
282
283 test = new Test('foobar')
284
285 test.batch(expectedArray, expectedCb)
286
287 t.equal(spy.callCount, 1, 'got _batch() call')
288 t.equal(spy.getCall(0).thisValue, test, '`this` on _batch() was correct')
289 t.equal(spy.getCall(0).args.length, 3, 'got three arguments')
290 t.equal(spy.getCall(0).args[0], expectedArray, 'got expected array argument')
291 t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument')
292 t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument')
293
294 test.batch(expectedArray, expectedOptions, expectedCb)
295
296 t.equal(spy.callCount, 2, 'got _batch() call')
297 t.equal(spy.getCall(1).thisValue, test, '`this` on _batch() was correct')
298 t.equal(spy.getCall(1).args.length, 3, 'got three arguments')
299 t.equal(spy.getCall(1).args[0], expectedArray, 'got expected array argument')
300 t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument')
301 t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument')
302
303 test.batch(expectedArray, null, expectedCb)
304
305 t.equal(spy.callCount, 3, 'got _batch() call')
306 t.equal(spy.getCall(2).thisValue, test, '`this` on _batch() was correct')
307 t.equal(spy.getCall(2).args.length, 3, 'got three arguments')
308 t.equal(spy.getCall(2).args[0], expectedArray, 'got expected array argument')
309 t.ok(spy.getCall(2).args[1], 'options should not be null')
310 t.equal(spy.getCall(2).args[2], expectedCb, 'got expected callback argument')
311 t.end()
312})
313
314tap.test('test chained batch() (array) extensibility', function (t) {
315 var spy = sinon.spy()
316 , expectedCb = function () {}
317 , expectedOptions = { options: 1 }
318 , expectedArray = [ 1, 2 ]
319 , test
320
321 function Test (location) {
322 AbstractLevelDOWN.call(this, location)
323 }
324
325 util.inherits(Test, AbstractLevelDOWN)
326
327 Test.prototype._batch = spy
328
329 test = new Test('foobar')
330
331 test.batch().put('foo', 'bar').del('bang').write(expectedCb)
332
333 t.equal(spy.callCount, 1, 'got _batch() call')
334 t.equal(spy.getCall(0).thisValue, test, '`this` on _batch() was correct')
335 t.equal(spy.getCall(0).args.length, 3, 'got three arguments')
336 t.equal(spy.getCall(0).args[0].length, 2, 'got expected array argument')
337 t.deepEqual(spy.getCall(0).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]')
338 t.deepEqual(spy.getCall(0).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]')
339 t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument')
340 t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument')
341
342 test.batch().put('foo', 'bar').del('bang').write(expectedOptions, expectedCb)
343
344 t.equal(spy.callCount, 2, 'got _batch() call')
345 t.equal(spy.getCall(1).thisValue, test, '`this` on _batch() was correct')
346 t.equal(spy.getCall(1).args.length, 3, 'got three arguments')
347 t.equal(spy.getCall(1).args[0].length, 2, 'got expected array argument')
348 t.deepEqual(spy.getCall(1).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]')
349 t.deepEqual(spy.getCall(1).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]')
350 t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument')
351 t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument')
352
353 t.end()
354})
355
356tap.test('test chained batch() (custom _chainedBatch) extensibility', function (t) {
357 var spy = sinon.spy()
358 , test
359
360 function Test (location) {
361 AbstractLevelDOWN.call(this, location)
362 }
363
364 util.inherits(Test, AbstractLevelDOWN)
365
366 Test.prototype._chainedBatch = spy
367
368 test = new Test('foobar')
369
370 test.batch()
371
372 t.equal(spy.callCount, 1, 'got _chainedBatch() call')
373 t.equal(spy.getCall(0).thisValue, test, '`this` on _chainedBatch() was correct')
374
375 test.batch()
376
377 t.equal(spy.callCount, 2, 'got _chainedBatch() call')
378 t.equal(spy.getCall(1).thisValue, test, '`this` on _chainedBatch() was correct')
379
380 t.end()
381})
382
383tap.test('test AbstractChainedBatch extensibility', function (t) {
384 function Test (db) {
385 AbstractChainedBatch.call(this, db)
386 t.equal(this._db, db, 'db set on `this`')
387 t.end()
388 }
389
390 util.inherits(Test, AbstractChainedBatch)
391
392 new Test('foobar')
393})
394
395tap.test('test write() extensibility', function (t) {
396 var spy = sinon.spy()
397 , spycb = sinon.spy()
398 , test
399
400 function Test (db) {
401 AbstractChainedBatch.call(this, db)
402 }
403
404 util.inherits(Test, AbstractChainedBatch)
405
406 Test.prototype._write = spy
407
408 test = new Test('foobar')
409 test.write(spycb)
410
411 t.equal(spy.callCount, 1, 'got _write() call')
412 t.equal(spy.getCall(0).thisValue, test, '`this` on _write() was correct')
413 t.equal(spy.getCall(0).args.length, 1, 'got one argument')
414 // awkward here cause of nextTick & an internal wrapped cb
415 t.type(spy.getCall(0).args[0], 'function', 'got a callback function')
416 t.equal(spycb.callCount, 0, 'spycb not called')
417 spy.getCall(0).args[0]()
418 t.equal(spycb.callCount, 1, 'spycb called, i.e. was our cb argument')
419 t.end()
420})
421
422tap.test('test put() extensibility', function (t) {
423 var spy = sinon.spy()
424 , expectedKey = 'key'
425 , expectedValue = 'value'
426 , returnValue
427 , test
428
429 function Test (db) {
430 AbstractChainedBatch.call(this, db)
431 }
432
433 util.inherits(Test, AbstractChainedBatch)
434
435 Test.prototype._put = spy
436
437 test = new Test(factory('foobar'))
438 returnValue = test.put(expectedKey, expectedValue)
439 t.equal(spy.callCount, 1, 'got _put call')
440 t.equal(spy.getCall(0).thisValue, test, '`this` on _put() was correct')
441 t.equal(spy.getCall(0).args.length, 2, 'got two arguments')
442 t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument')
443 t.equal(spy.getCall(0).args[1], expectedValue, 'got expected value argument')
444 t.equal(returnValue, test, 'get expected return value')
445 t.end()
446})
447
448tap.test('test del() extensibility', function (t) {
449 var spy = sinon.spy()
450 , expectedKey = 'key'
451 , returnValue
452 , test
453
454 function Test (db) {
455 AbstractChainedBatch.call(this, db)
456 }
457
458 util.inherits(Test, AbstractChainedBatch)
459
460 Test.prototype._del = spy
461
462 test = new Test(factory('foobar'))
463 returnValue = test.del(expectedKey)
464 t.equal(spy.callCount, 1, 'got _del call')
465 t.equal(spy.getCall(0).thisValue, test, '`this` on _del() was correct')
466 t.equal(spy.getCall(0).args.length, 1, 'got one argument')
467 t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument')
468 t.equal(returnValue, test, 'get expected return value')
469 t.end()
470})
471
472tap.test('test clear() extensibility', function (t) {
473 var spy = sinon.spy()
474 , returnValue
475 , test
476
477 function Test (db) {
478 AbstractChainedBatch.call(this, db)
479 }
480
481 util.inherits(Test, AbstractChainedBatch)
482
483 Test.prototype._clear = spy
484
485 test = new Test(factory('foobar'))
486 returnValue = test.clear()
487 t.equal(spy.callCount, 1, 'got _clear call')
488 t.equal(spy.getCall(0).thisValue, test, '`this` on _clear() was correct')
489 t.equal(spy.getCall(0).args.length, 0, 'got zero arguments')
490 t.equal(returnValue, test, 'get expected return value')
491 t.end()
492})
493
494tap.test('test iterator() extensibility', function (t) {
495 var spy = sinon.spy()
496 , expectedOptions = { options: 1, reverse: false, keys: true, values: true, limit: -1, keyAsBuffer: false, valueAsBuffer: false }
497 , test
498
499 function Test (location) {
500 AbstractLevelDOWN.call(this, location)
501 }
502
503 util.inherits(Test, AbstractLevelDOWN)
504
505 Test.prototype._iterator = spy
506
507 test = new Test('foobar')
508 test.iterator({ options: 1 })
509
510 t.equal(spy.callCount, 1, 'got _close() call')
511 t.equal(spy.getCall(0).thisValue, test, '`this` on _close() was correct')
512 t.equal(spy.getCall(0).args.length, 1, 'got one arguments')
513 t.deepEqual(spy.getCall(0).args[0], expectedOptions, 'got expected options argument')
514 t.end()
515})
516
517tap.test('test AbstractIterator extensibility', function (t) {
518 function Test (db) {
519 AbstractIterator.call(this, db)
520 t.equal(this.db, db, 'db set on `this`')
521 t.end()
522 }
523
524 util.inherits(Test, AbstractIterator)
525
526 ;new Test('foobar')
527})
528
529tap.test('test next() extensibility', function (t) {
530 var spy = sinon.spy()
531 , spycb = sinon.spy()
532 , test
533
534 function Test (db) {
535 AbstractIterator.call(this, db)
536 }
537
538 util.inherits(Test, AbstractIterator)
539
540 Test.prototype._next = spy
541
542 test = new Test('foobar')
543 test.next(spycb)
544
545 t.equal(spy.callCount, 1, 'got _next() call')
546 t.equal(spy.getCall(0).thisValue, test, '`this` on _next() was correct')
547 t.equal(spy.getCall(0).args.length, 1, 'got one arguments')
548 // awkward here cause of nextTick & an internal wrapped cb
549 t.type(spy.getCall(0).args[0], 'function', 'got a callback function')
550 t.equal(spycb.callCount, 0, 'spycb not called')
551 spy.getCall(0).args[0]()
552 t.equal(spycb.callCount, 1, 'spycb called, i.e. was our cb argument')
553 t.end()
554})
555
556tap.test('test end() extensibility', function (t) {
557 var spy = sinon.spy()
558 , expectedCb = function () {}
559 , test
560
561 function Test (db) {
562 AbstractIterator.call(this, db)
563 }
564
565 util.inherits(Test, AbstractIterator)
566
567 Test.prototype._end = spy
568
569 test = new Test('foobar')
570 test.end(expectedCb)
571
572 t.equal(spy.callCount, 1, 'got _end() call')
573 t.equal(spy.getCall(0).thisValue, test, '`this` on _end() was correct')
574 t.equal(spy.getCall(0).args.length, 1, 'got one arguments')
575 t.equal(spy.getCall(0).args[0], expectedCb, 'got expected cb argument')
576 t.end()
577})