Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing options to abstract db.open() #1

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function compose () {
// If db is levelup, it will auto-open and call the callback. If
// abstract-leveldown, it won't. If abstract-db (a concept), it might.
if (callback && !isLevelup(db) && db.status === 'new') {
db.open(options, function (err) {
db.open(xtend(layer.defaults, options), function (err) {
if (err) return callback(err)
callback(null, db)
})
Expand All @@ -54,7 +54,8 @@ module.exports = function compose () {
return shell
}

layers.push(function (db, options, callback) {
layers.push(function wrapped (db, options, callback) {
wrapped.defaults = defaults
return layer(db, xtend(defaults, options), callback)
})

Expand Down
104 changes: 103 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var packager = function (down) {
}

// Tests copied from level-packager
// TODO: write own tests

test('packager - Level constructor has access to levelup errors', function (t) {
function Down () {}
Expand Down Expand Up @@ -93,3 +92,106 @@ test('packager - Level constructor with custom options', function (t) {
t.is(levelup._db.codec.opts.keyEncoding, 'binary')
t.is(levelup._db.codec.opts.valueEncoding, 'binary')
})

// Own tests
// TODO: write more

test('passes defaults only to preceding layer', function (t) {
t.plan(12)

compose(negative, positive, { test: true }, negative)('test-location')
compose([negative, positive, { test: true }, negative])('test-location')
compose().use([negative, positive, { test: true }, negative])('test-location')
compose().use(negative).use(positive, { test: true }).use(negative)('test-location')

function negative (dbOrLocation, options) {
t.same(options, {}, 'did not get defaults')
return dbOrLocation
}

function positive (dbOrLocation, options) {
t.same(options, { test: true }, 'did get defaults')
return dbOrLocation
}
})

test('passes defaults only to preceding layers (in array)', function (t) {
t.plan(4)

compose(negative, [positive, positive], { test: true }, negative)('test-location')

function negative (dbOrLocation, options) {
t.same(options, {}, 'did not get defaults')
return dbOrLocation
}

function positive (dbOrLocation, options) {
t.same(options, { test: true }, 'did get defaults')
return dbOrLocation
}
})

test('merges defaults into preceding layers', function (t) {
t.plan(9)

const objects = { a: { a: 1 }, b: { b: 2 } }

compose().use([b, [ab, ab], objects.a, b], objects.b)('test-location')
compose().use(none).use([a, a], objects.a).use(b, objects.b)('test-location')

t.same(objects, { a: { a: 1 }, b: { b: 2 } }, 'did not mutate original objects')

function a (dbOrLocation, options) {
t.same(options, { a: 1 }, 'only a')
return dbOrLocation
}

function b (dbOrLocation, options) {
t.same(options, { b: 2 }, 'only b')
return dbOrLocation
}

function ab (dbOrLocation, options) {
t.same(options, { a: 1, b: 2 }, 'a and b')
return dbOrLocation
}

function none (dbOrLocation, options) {
t.same(options, {}, 'none')
return dbOrLocation
}
})

test('passes defaults to abstract db.open() if callback is provided', function (t) {
t.plan(20)

compose(lastLayer, { test: true })('test-location', onOpen)
compose([firstLayer, lastLayer], { test: true })('test-location', onOpen)
compose([[firstLayer, lastLayer], { test: false }], { test: true })('test-location', onOpen)

function firstLayer (location, options, callback) {
t.is(callback, undefined, 'only last layer receives callback')
return location
}

function lastLayer (location, options, callback) {
t.is(location, 'test-location', 'got location')
t.same(options, { test: true }, 'got options')
t.is(callback, onOpen, 'got callback')

// Mock abstract-leveldown
return {
status: 'new',
mocked: true,
open: function (options, callback) {
t.same(options, { test: true }, 'got open() options')
process.nextTick(callback)
}
}
}

function onOpen (err, db) {
t.ifError(err, 'no open error')
t.is(db.mocked, true, 'got db')
}
})