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 59229b94..00000000 Binary files a/test/data/testdata.bin and /dev/null differ diff --git a/test/del-test.js b/test/del-test.js index b21712be..fc140dd9 100644 --- a/test/del-test.js +++ b/test/del-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/del-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/destroy-test.js b/test/destroy-test.js index d1be840e..4181ee6e 100644 --- a/test/destroy-test.js +++ b/test/destroy-test.js @@ -3,8 +3,7 @@ const test = require('tape') , path = require('path') , mkfiletree = require('mkfiletree') , readfiletree = require('readfiletree') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , makeTest = require('./make') test('test argument-less destroy() throws', function (t) { diff --git a/test/get-test.js b/test/get-test.js index 52270ad3..8df01706 100644 --- a/test/get-test.js +++ b/test/get-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/get-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/getproperty-test.js b/test/getproperty-test.js index 46bce118..62e99d70 100644 --- a/test/getproperty-test.js +++ b/test/getproperty-test.js @@ -1,6 +1,6 @@ const test = require('tape') , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') var db diff --git a/test/iterator-range-test.js b/test/iterator-range-test.js new file mode 100644 index 00000000..c6a2a164 --- /dev/null +++ b/test/iterator-range-test.js @@ -0,0 +1,5 @@ +const test = require('tape') + , leveldown = require('..') + , abstract = require('abstract-leveldown/abstract/iterator-range-test') + +abstract.all(leveldown, test) diff --git a/test/iterator-recursion-test.js b/test/iterator-recursion-test.js index 716626ec..884e75ee 100644 --- a/test/iterator-recursion-test.js +++ b/test/iterator-recursion-test.js @@ -1,6 +1,6 @@ const test = require('tape') , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , child_process = require('child_process') var db diff --git a/test/iterator-test.js b/test/iterator-test.js index 21f2ce79..7c3064cb 100644 --- a/test/iterator-test.js +++ b/test/iterator-test.js @@ -1,13 +1,12 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/iterator-test') , make = require('./make') , iota = require('iota-array') , lexi = require('lexicographic-integer') , util = require('util') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) make('iterator throws if key is not a string or buffer', function (db, t, done) { var keys = [null, undefined, 1, true, false] @@ -288,4 +287,4 @@ function not (n) { function even (n) { return n % 2 === 0 -} \ No newline at end of file +} diff --git a/test/leveldown-test.js b/test/leveldown-test.js index 11db1c38..dc005fc0 100644 --- a/test/leveldown-test.js +++ b/test/leveldown-test.js @@ -1,5 +1,5 @@ const test = require('tape') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/leveldown-test') abstract.args(leveldown, test) diff --git a/test/make.js b/test/make.js index aeccf3b4..1ed47ee4 100644 --- a/test/make.js +++ b/test/make.js @@ -1,35 +1,40 @@ -const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , cleanup = testCommon.cleanup - , location = testCommon.location - , leveldown = require('../') +const test = require('tape') +const testCommon = require('abstract-leveldown/testCommon') +const cleanup = testCommon.cleanup +const leveldown = require('..') function makeTest (name, testFn) { test(name, function (t) { - cleanup(function () { - var loc = location() - , db = leveldown(loc) - , done = function (close) { - if (close === false) - return cleanup(t.end.bind(t)) - db.close(function (err) { - t.notOk(err, 'no error from close()') - cleanup(t.end.bind(t)) - }) - } + cleanup(function (err) { + t.error(err, 'no error after cleanup') + var location = testCommon.location() + var db = leveldown(location) + var done = function (close) { + if (close === false) { + cleanup(function (err) { + t.error(err, 'no error after cleanup') + t.end() + }) + return + } + db.close(function (err) { + t.error(err, 'no error from close()') + cleanup(function (err) { + t.error(err, 'no error after cleanup') + t.end() + }) + }) + } db.open(function (err) { - t.notOk(err, 'no error from open()') - db.batch( - [ - { type: 'put', key: 'one', value: '1' } - , { type: 'put', key: 'two', value: '2' } - , { type: 'put', key: 'three', value: '3' } - ] - , function (err) { - t.notOk(err, 'no error from batch()') - testFn(db, t, done, loc) - } - ) + t.error(err, 'no error from open()') + 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, 'no error from batch()') + testFn(db, t, done, location) + }) }) }) }) diff --git a/test/open-test.js b/test/open-test.js index 0da638a3..bd5ad06c 100644 --- a/test/open-test.js +++ b/test/open-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/open-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/put-get-del-test.js b/test/put-get-del-test.js index 0f688585..56cd272e 100644 --- a/test/put-get-del-test.js +++ b/test/put-get-del-test.js @@ -1,9 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') - , fs = require('fs') - , path = require('path') - , testBuffer = fs.readFileSync(path.join(__dirname, 'data/testdata.bin')) + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/put-get-del-test') -abstract.all(leveldown, test, testCommon, testBuffer) +abstract.all(leveldown, test) diff --git a/test/put-test.js b/test/put-test.js index e437f5ee..43bc438c 100644 --- a/test/put-test.js +++ b/test/put-test.js @@ -1,6 +1,5 @@ const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , abstract = require('abstract-leveldown/abstract/put-test') -abstract.all(leveldown, test, testCommon) +abstract.all(leveldown, test) diff --git a/test/ranges-test.js b/test/ranges-test.js deleted file mode 100644 index 7f16a2d0..00000000 --- a/test/ranges-test.js +++ /dev/null @@ -1,6 +0,0 @@ -const test = require('tape') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') - , abstract = require('abstract-leveldown/abstract/ranges-test') - -abstract.all(leveldown, test, testCommon) diff --git a/test/repair-test.js b/test/repair-test.js index c878bdeb..8cc56879 100644 --- a/test/repair-test.js +++ b/test/repair-test.js @@ -3,8 +3,7 @@ const test = require('tape') , path = require('path') , mkfiletree = require('mkfiletree') , readfiletree = require('readfiletree') - , testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') , makeTest = require('./make') test('test argument-less repair() throws', function (t) { diff --git a/test/stack-blower.js b/test/stack-blower.js index 00c48e1e..8c151f12 100644 --- a/test/stack-blower.js +++ b/test/stack-blower.js @@ -6,7 +6,7 @@ * directly, we check for a command-line argument. */ const testCommon = require('abstract-leveldown/testCommon') - , leveldown = require('../') + , leveldown = require('..') if (process.argv[2] == 'run') { testCommon.cleanup(function () {