From 241e97c07670eeef06c2b62574b00a1b01ec9acd Mon Sep 17 00:00:00 2001 From: Magnus Date: Sun, 21 Jan 2018 21:11:52 +0100 Subject: [PATCH] Abstract leveldown v4 (#415) * rename ranges-test.js iterator-range-test.js * add missing tearDown for compact range test * assert no errors after cleanup * :truck: move over implementation of approximateSize() from abstract-leveldown (fixes tests) * update tests using testCommon defaults * use .. consistenly when requiring leveldown * :fire: remove testBuffer * use abstract-leveldown@4.0.0 --- leveldown.js | 16 +++- package.json | 2 +- test/approximate-size-test.js | 127 ++++++++++++++++++++++++- test/batch-test.js | 5 +- test/chained-batch-test.js | 5 +- test/cleanup-hanging-iterators-test.js | 3 +- test/close-test.js | 2 +- test/compact-range-test.js | 6 +- test/compression-test.js | 2 +- test/data/testdata.bin | Bin 4688 -> 0 bytes test/del-test.js | 5 +- test/destroy-test.js | 3 +- test/get-test.js | 5 +- test/getproperty-test.js | 2 +- test/iterator-range-test.js | 5 + test/iterator-recursion-test.js | 2 +- test/iterator-test.js | 7 +- test/leveldown-test.js | 2 +- test/make.js | 61 ++++++------ test/open-test.js | 5 +- test/put-get-del-test.js | 8 +- test/put-test.js | 5 +- test/ranges-test.js | 6 -- test/repair-test.js | 3 +- test/stack-blower.js | 2 +- 25 files changed, 207 insertions(+), 82 deletions(-) delete mode 100644 test/data/testdata.bin create mode 100644 test/iterator-range-test.js delete mode 100644 test/ranges-test.js diff --git a/leveldown.js b/leveldown.js index e6b64b66..ce2e1236 100644 --- a/leveldown.js +++ b/leveldown.js @@ -53,7 +53,21 @@ LevelDOWN.prototype._batch = function (operations, options, callback) { } -LevelDOWN.prototype._approximateSize = function (start, end, callback) { +LevelDOWN.prototype.approximateSize = function (start, end, callback) { + if (start == null || + end == null || + typeof start === 'function' || + typeof end === 'function') { + throw new Error('approximateSize() requires valid `start`, `end` and `callback` arguments') + } + + if (typeof callback !== 'function') { + throw new Error('approximateSize() requires a callback argument') + } + + start = this._serializeKey(start) + end = this._serializeKey(end) + this.binding.approximateSize(start, end, callback) } diff --git a/package.json b/package.json index 232e5d3a..876df2e8 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "main": "leveldown.js", "typings": "leveldown.d.ts", "dependencies": { - "abstract-leveldown": "~3.0.0", + "abstract-leveldown": "~4.0.0", "bindings": "~1.3.0", "fast-future": "~1.0.2", "nan": "~2.8.0", diff --git a/test/approximate-size-test.js b/test/approximate-size-test.js index 31038a48..0e662d64 100644 --- a/test/approximate-size-test.js +++ b/test/approximate-size-test.js @@ -1,6 +1,123 @@ -const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') - , abstract = require('abstract-leveldown/abstract/approximate-size-test') +const test = require('tape') +const leveldown = require('..') +const testCommon = require('abstract-leveldown/testCommon') -abstract.all(leveldown, test, testCommon) +var db + +test('setUp common for approximate size', testCommon.setUp) + +test('setUp db', function (t) { + db = leveldown(testCommon.location()) + console.log('db', db) + db.open(t.end.bind(t)) +}) + +test('test argument-less approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db) + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } + , 'no-arg approximateSize() throws' + ) + t.end() +}) + +test('test callback-less, 1-arg, approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db, 'foo') + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } + , 'callback-less, 1-arg approximateSize() throws' + ) + t.end() +}) + +test('test callback-less, 2-arg, approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db, 'foo', 'bar') + , { name: 'Error', message: 'approximateSize() requires a callback argument' } + , 'callback-less, 2-arg approximateSize() throws' + ) + t.end() +}) + +test('test callback-less, 3-arg, approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db, function () {}) + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } + , 'callback-only approximateSize() throws' + ) + t.end() +}) + +test('test callback-only approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db, function () {}) + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } + , 'callback-only approximateSize() throws' + ) + t.end() +}) + +test('test 1-arg + callback approximateSize() throws', function (t) { + t.throws( + db.approximateSize.bind(db, 'foo', function () {}) + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } + , '1-arg + callback approximateSize() throws' + ) + t.end() +}) + +test('test custom _serialize*', function (t) { + t.plan(4) + var db = leveldown(testCommon.location()) + db._serializeKey = function (data) { return data } + db.approximateSize = function (start, end, callback) { + t.deepEqual(start, { foo: 'bar' }) + t.deepEqual(end, { beep: 'boop' }) + process.nextTick(callback) + } + db.open(function () { + db.approximateSize({ foo: 'bar' }, { beep: 'boop' }, function (err) { + t.error(err) + db.close(t.error.bind(t)) + }) + }) +}) + +test('test approximateSize()', function (t) { + var data = Array.apply(null, Array(10000)).map(function () { + return 'aaaaaaaaaa' + }).join('') + + db.batch(Array.apply(null, Array(10)).map(function (x, i) { + return { type: 'put', key: 'foo' + i, value: data } + }), function (err) { + t.error(err) + + // cycle open/close to ensure a pack to .sst + + db.close(function (err) { + t.error(err) + + db.open(function (err) { + t.error(err) + + db.approximateSize('!', '~', function (err, size) { + t.error(err) + + t.equal(typeof size, 'number') + // account for snappy compression, original would be ~100000 + t.ok(size > 40000, 'size reports a reasonable amount (' + size + ')') + + db.close(function (err) { + t.error(err) + t.end() + }) + }) + }) + }) + }) +}) + +test('tearDown', function (t) { + db.close(testCommon.tearDown.bind(null, t)) +}) diff --git a/test/batch-test.js b/test/batch-test.js index c93e6eff..903eadc3 100644 --- a/test/batch-test.js +++ b/test/batch-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/batch-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/chained-batch-test.js b/test/chained-batch-test.js index cc20f9e4..efe8e22a 100644 --- a/test/chained-batch-test.js +++ b/test/chained-batch-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/chained-batch-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/cleanup-hanging-iterators-test.js b/test/cleanup-hanging-iterators-test.js index 3ed394e6..01793783 100644 --- a/test/cleanup-hanging-iterators-test.js +++ b/test/cleanup-hanging-iterators-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , makeTest = require('./make') makeTest('test ended iterator', function (db, t, done) { diff --git a/test/close-test.js b/test/close-test.js index ae5d3bf8..a1030a3d 100644 --- a/test/close-test.js +++ b/test/close-test.js @@ -1,6 +1,6 @@ const test = require('tape') , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/close-test') module.exports.setUp = function () { diff --git a/test/compact-range-test.js b/test/compact-range-test.js index 135029af..94baca5d 100644 --- a/test/compact-range-test.js +++ b/test/compact-range-test.js @@ -1,6 +1,6 @@ const test = require('tape') , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') var db @@ -35,3 +35,7 @@ test('test compactRange() frees disk space after key deletion', function (t) { }); }); }); + +test('tearDown', function (t) { + db.close(testCommon.tearDown.bind(null, t)); +}); diff --git a/test/compression-test.js b/test/compression-test.js index 91c28eb7..2c8101b2 100644 --- a/test/compression-test.js +++ b/test/compression-test.js @@ -7,7 +7,7 @@ var async = require('async') , du = require('du') , delayed = require('delayed') , common = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , test = require('tape') , compressableData = new Buffer(Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('')) diff --git a/test/data/testdata.bin b/test/data/testdata.bin deleted file mode 100644 index 59229b942a3cb91ad45a4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