Skip to content

Commit

Permalink
Abstract leveldown v4 (#415)
Browse files Browse the repository at this point in the history
* rename ranges-test.js iterator-range-test.js

* add missing tearDown for compact range test

* assert no errors after cleanup

* 🚚 move over implementation of approximateSize() from abstract-leveldown (fixes tests)

* update tests using testCommon defaults

* use .. consistenly when requiring leveldown

* 🔥 remove testBuffer

* use abstract-leveldown@4.0.0
  • Loading branch information
ralphtheninja committed Jan 21, 2018
1 parent 990e22f commit 241e97c
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 82 deletions.
16 changes: 15 additions & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
127 changes: 122 additions & 5 deletions test/approximate-size-test.js
Original file line number Diff line number Diff line change
@@ -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))
})
5 changes: 2 additions & 3 deletions test/batch-test.js
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 2 additions & 3 deletions test/chained-batch-test.js
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions test/cleanup-hanging-iterators-test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/close-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
6 changes: 5 additions & 1 deletion test/compact-range-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('tape')
, testCommon = require('abstract-leveldown/testCommon')
, leveldown = require('../')
, leveldown = require('..')

var db

Expand Down Expand Up @@ -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));
});
2 changes: 1 addition & 1 deletion test/compression-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(''))
Expand Down
Binary file removed test/data/testdata.bin
Binary file not shown.
5 changes: 2 additions & 3 deletions test/del-test.js
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions test/destroy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions test/get-test.js
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion test/getproperty-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('tape')
, testCommon = require('abstract-leveldown/testCommon')
, leveldown = require('../')
, leveldown = require('..')

var db

Expand Down
5 changes: 5 additions & 0 deletions test/iterator-range-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const test = require('tape')
, leveldown = require('..')
, abstract = require('abstract-leveldown/abstract/iterator-range-test')

abstract.all(leveldown, test)
2 changes: 1 addition & 1 deletion test/iterator-recursion-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('tape')
, testCommon = require('abstract-leveldown/testCommon')
, leveldown = require('../')
, leveldown = require('..')
, child_process = require('child_process')

var db
Expand Down
7 changes: 3 additions & 4 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -288,4 +287,4 @@ function not (n) {

function even (n) {
return n % 2 === 0
}
}
2 changes: 1 addition & 1 deletion test/leveldown-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
, leveldown = require('../')
, leveldown = require('..')
, abstract = require('abstract-leveldown/abstract/leveldown-test')

abstract.args(leveldown, test)
61 changes: 33 additions & 28 deletions test/make.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
Expand Down
5 changes: 2 additions & 3 deletions test/open-test.js
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 2 additions & 6 deletions test/put-get-del-test.js
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 2 additions & 3 deletions test/put-test.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 241e97c

Please sign in to comment.