Skip to content

Commit

Permalink
Location was removed from abstract-leveldown (#494)
Browse files Browse the repository at this point in the history
* Location was removed from abstract-leveldown

* handle location in LevelDOWN constructor
* pass a factory function to abstract tests
* use tempy and remove cleanup code

* Add type check for location in constructor
  • Loading branch information
ralphtheninja authored and vweevers committed Sep 22, 2018
1 parent ab7c1a5 commit e1ac18d
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 143 deletions.
8 changes: 7 additions & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ function LevelDOWN (location) {
return new LevelDOWN(location)
}

AbstractLevelDOWN.call(this, location)
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument')
}

AbstractLevelDOWN.call(this)

this.location = location
this.binding = binding(location)
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"delayed": "~1.0.1",
"du": "~0.1.0",
"iota-array": "~1.0.0",
"level-concat-iterator": "^2.0.0",
"lexicographic-integer": "~1.1.0",
"mkfiletree": "~1.0.1",
"monotonic-timestamp": "~0.0.8",
Expand All @@ -39,6 +40,7 @@
"slump": "~2.0.0",
"standard": "^12.0.0",
"tape": "^4.5.1",
"tempy": "^0.2.1",
"uuid": "^3.2.1",
"verify-travis-appveyor": "^3.0.0"
},
Expand Down
7 changes: 3 additions & 4 deletions test/approximate-size-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const test = require('tape')
const leveldown = require('..')
const testCommon = require('abstract-leveldown/testCommon')
const testCommon = require('./common')

var db

test('setUp common for approximate size', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down Expand Up @@ -67,7 +66,7 @@ test('test 1-arg + callback approximateSize() throws', function (t) {

test('test custom _serialize*', function (t) {
t.plan(4)
var db = leveldown(testCommon.location())
var db = testCommon.factory()
db._serializeKey = function (data) { return data }
db.approximateSize = function (start, end, callback) {
t.deepEqual(start, { foo: 'bar' })
Expand Down
6 changes: 3 additions & 3 deletions test/batch-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/batch-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/batch-test')

abstract.all(leveldown, test)
abstract.all(testCommon.factory, test)
6 changes: 3 additions & 3 deletions test/chained-batch-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/chained-batch-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/chained-batch-test')

abstract.all(leveldown, test)
abstract.all(testCommon.factory, test)
23 changes: 5 additions & 18 deletions test/close-test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/close-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/close-test')

module.exports.setUp = function () {
test('setUp', testCommon.setUp)
}
test('setUp', testCommon.setUp)

module.exports.close = abstract.close
abstract.close(testCommon.factory, test, testCommon)

module.exports.tearDown = function () {
test('tearDown', testCommon.tearDown)
}

module.exports.all = function (leveldown) {
module.exports.setUp()
module.exports.close(leveldown, test, testCommon)
module.exports.tearDown()
}

module.exports.all(leveldown)
test('tearDown', testCommon.tearDown)
9 changes: 9 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const testCommon = require('abstract-leveldown/test/common')
const tempy = require('tempy')
const leveldown = require('..')

testCommon.factory = function () {
return leveldown(tempy.directory())
}

module.exports = testCommon
5 changes: 2 additions & 3 deletions test/compact-range-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')

let db

test('setUp common', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down
15 changes: 5 additions & 10 deletions test/compression-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* Copyright (c) 2012-2017 LevelUP contributors
* See list at <https://github.com/level/leveldown#contributing>
* MIT License <https://github.com/level/leveldown/blob/master/LICENSE.md>
*/

const async = require('async')
const du = require('du')
const delayed = require('delayed')
const common = require('abstract-leveldown/testCommon')
const testCommon = require('./common')
const leveldown = require('..')
const test = require('tape')

Expand Down Expand Up @@ -46,10 +41,10 @@ const cycle = function (db, compression, t, callback) {
}

test('Compression', function (t) {
t.test('set up', common.setUp)
t.test('set up', testCommon.setUp)

t.test('test data is compressed by default (db.put())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open(function (err) {
t.error(err)
async.forEach(
Expand All @@ -63,7 +58,7 @@ test('Compression', function (t) {
})

t.test('test data is not compressed with compression=false on open() (db.put())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open({ compression: false }, function (err) {
t.error(err)
async.forEach(
Expand All @@ -77,7 +72,7 @@ test('Compression', function (t) {
})

t.test('test data is compressed by default (db.batch())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open(function (err) {
t.error(err)
db.batch(
Expand Down
6 changes: 3 additions & 3 deletions test/del-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/del-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/del-test')

abstract.all(leveldown, test)
abstract.all(testCommon.factory, test)
10 changes: 6 additions & 4 deletions test/destroy-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const tempy = require('tempy')
const fs = require('fs')
const path = require('path')
const mkfiletree = require('mkfiletree')
Expand Down Expand Up @@ -27,7 +27,7 @@ test('test callback-less, 1-arg, destroy() throws', function (t) {
test('test destroy non-existent directory', function (t) {
t.plan(4)

var location = testCommon.location()
var location = tempy.directory()
var parent = path.dirname(location)

// For symmetry with the opposite test below.
Expand Down Expand Up @@ -86,7 +86,8 @@ test('test destroy non leveldb directory', function (t) {
})
})

makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t, done, location) {
makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t, done) {
var location = db.location
db.close(function (err) {
t.ifError(err, 'no close error')

Expand All @@ -99,7 +100,8 @@ makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t,
})
})

makeTest('test destroy() cleans and removes only leveldb parts of a dir', function (db, t, done, location) {
makeTest('test destroy() cleans and removes only leveldb parts of a dir', function (db, t, done) {
var location = db.location
fs.writeFileSync(path.join(location, 'foo'), 'FOO')

db.close(function (err) {
Expand Down
6 changes: 3 additions & 3 deletions test/get-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/get-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/get-test')

abstract.all(leveldown, test)
abstract.all(testCommon.factory, test)
5 changes: 2 additions & 3 deletions test/getproperty-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')

let db

test('setUp common', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down
8 changes: 4 additions & 4 deletions test/iterator-gc-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const collectEntries = require('level-concat-iterator')
const testCommon = require('./common')
const sourceData = []

for (let i = 0; i < 1e3; i++) {
Expand All @@ -20,7 +20,7 @@ test('setUp', testCommon.setUp)
test('db without ref does not get GCed while iterating', function (t) {
t.plan(6)

let db = leveldown(testCommon.location())
let db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand Down Expand Up @@ -50,7 +50,7 @@ test('db without ref does not get GCed while iterating', function (t) {

function iterate (it) {
// No reference to db here, could be GCed. It shouldn't..
testCommon.collectEntries(it, function (err, entries) {
collectEntries(it, function (err, entries) {
t.ifError(err, 'no iterator error')
t.is(entries.length, sourceData.length, 'got data')

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

abstract.all(leveldown, test)
abstract.all(testCommon.factory, test)
5 changes: 2 additions & 3 deletions test/iterator-recursion-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')
const fork = require('child_process').fork
const path = require('path')

Expand Down Expand Up @@ -43,7 +42,7 @@ test('try to create an iterator with a blown stack', function (t) {
})

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(function (err) {
t.error(err)
db.batch(sourceData, t.end.bind(t))
Expand Down
6 changes: 3 additions & 3 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const test = require('tape')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/iterator-test')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/iterator-test')
const make = require('./make')
const iota = require('iota-array')
const lexi = require('lexicographic-integer')
const util = require('util')

abstract.all(leveldown, test)
abstract.all(testCommon.factory, 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
12 changes: 5 additions & 7 deletions test/leak-tester-batch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const BUFFERS = false
const CHAINED = false

const leveldown = require('..')
const testCommon = require('./common')
const crypto = require('crypto')
const assert = require('assert')

Expand Down Expand Up @@ -72,10 +72,8 @@ var run = CHAINED
print()
}

leveldown.destroy('./leakydb', function () {
db = leveldown('./leakydb')
db.open({ xcacheSize: 0, xmaxOpenFiles: 10 }, function () {
rssBase = process.memoryUsage().rss
run()
})
db = testCommon.factory()
db.open({ xcacheSize: 0, xmaxOpenFiles: 10 }, function () {
rssBase = process.memoryUsage().rss
run()
})
12 changes: 5 additions & 7 deletions test/leak-tester.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BUFFERS = false

const leveldown = require('..')
const testCommon = require('./common')
const crypto = require('crypto')

let putCount = 0
Expand Down Expand Up @@ -40,10 +40,8 @@ function run () {
}
}

leveldown.destroy('./leakydb', function () {
db = leveldown('./leakydb')
db.open({ xcacheSize: 0, xmaxOpenFiles: 10 }, function () {
rssBase = process.memoryUsage().rss
run()
})
db = testCommon.factory()
db.open({ xcacheSize: 0, xmaxOpenFiles: 10 }, function () {
rssBase = process.memoryUsage().rss
run()
})
14 changes: 12 additions & 2 deletions test/leveldown-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const test = require('tape')
const testCommon = require('./common')
const abstract = require('abstract-leveldown/test/leveldown-test')
const leveldown = require('..')
const abstract = require('abstract-leveldown/abstract/leveldown-test')

abstract.args(leveldown, test)
abstract.args(testCommon.factory, test)

test('test database creation non-string location throws', function (t) {
t.throws(
leveldown.bind(null, {}),
/constructor requires a location string argument/,
'non-string location leveldown() throws'
)
t.end()
})
Loading

0 comments on commit e1ac18d

Please sign in to comment.