Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Increase abstract-leveldown parity #692

Merged
merged 1 commit into from
Apr 11, 2020
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
3 changes: 2 additions & 1 deletion lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions

function Batch (levelup) {
this._levelup = levelup
// TODO (next major): remove this._levelup alias
this.db = this._levelup = levelup
this.batch = levelup.db.batch()
this.ops = []
this.length = 0
Expand Down
19 changes: 2 additions & 17 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ LevelUP.prototype.isClosed = function () {
}

LevelUP.prototype.get = function (key, options, callback) {
if (key === null || key === undefined) {
throw new ReadError('get() requires a key argument')
}

var promise

callback = getCallback(options, callback)
Expand Down Expand Up @@ -202,10 +198,6 @@ LevelUP.prototype.get = function (key, options, callback) {
}

LevelUP.prototype.put = function (key, value, options, callback) {
if (key === null || key === undefined) {
throw new WriteError('put() requires a key argument')
}

var self = this
var promise

Expand All @@ -232,10 +224,6 @@ LevelUP.prototype.put = function (key, value, options, callback) {
}

LevelUP.prototype.del = function (key, options, callback) {
if (key === null || key === undefined) {
throw new WriteError('del() requires a key argument')
}

var self = this
var promise

Expand Down Expand Up @@ -266,14 +254,11 @@ LevelUP.prototype.batch = function (arr, options, callback) {
return new Batch(this)
}

if (!Array.isArray(arr)) {
throw new WriteError('batch() requires an array argument')
}

var self = this
var promise

callback = getCallback(options, callback)
if (typeof arr === 'function') callback = arr
else callback = getCallback(options, callback)

if (!callback) {
callback = promisify()
Expand Down
37 changes: 0 additions & 37 deletions test/argument-checking-test.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/get-put-del-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,4 @@ module.exports = function (test, testCommon) {
], done)
})
})

test('get() / put() / del(): throw if no key is provided', function (t) {
discardable(t, testCommon, function (db, done) {
t.throws(db.get.bind(db), /^ReadError: get\(\) requires a key argument/, 'no-arg get() throws')
t.throws(db.put.bind(db), /^WriteError: put\(\) requires a key argument/, 'no-arg put() throws')
t.throws(db.del.bind(db), /^WriteError: del\(\) requires a key argument/, 'no-arg del() throws')
done()
})
})
}
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function suite (options) {
var testCommon = common(options)
var test = testCommon.test

require('./argument-checking-test')(test, testCommon)
require('./manifest-test')(test, testCommon)
require('./batch-test')(test, testCommon)
if (testCommon.encodings) require('./binary-test')(test, testCommon)
Expand Down
72 changes: 32 additions & 40 deletions test/null-and-undefined-test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
var errors = require('../lib/levelup').errors
var after = require('after')
var discardable = require('./util/discardable')

module.exports = function (test, testCommon) {
test('null & undefined keys & values: cause error', function (t) {
discardable(t, testCommon, function (db, done) {
t.throws(db.get.bind(db, null), /^ReadError: get\(\) requires a key argument/)
t.throws(db.get.bind(db, undefined), /^ReadError: get\(\) requires a key argument/)
t.throws(db.del.bind(db, null), /^WriteError: del\(\) requires a key argument/)
t.throws(db.del.bind(db, undefined), /^WriteError: del\(\) requires a key argument/)
t.throws(db.put.bind(db, null, 'foo'), /^WriteError: put\(\) requires a key argument/)
t.throws(db.put.bind(db, undefined, 'foo'), /^WriteError: put\(\) requires a key argument/)

var next = after(6, done)

db.put('foo', null, function (err, value) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})

db.put('foo', undefined, function (err, value) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})

db.batch([{ key: 'foo', value: undefined, type: 'put' }], function (err) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})

db.batch([{ key: 'foo', value: null, type: 'put' }], function (err) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})

db.batch([{ key: undefined, value: 'bar', type: 'put' }], function (err) {
t.ok(err instanceof Error)
t.ok(err instanceof errors.LevelUPError)
next()
})

db.batch([{ key: null, value: 'bar', type: 'put' }], function (err) {
t.ok(err instanceof Error)
t.ok(err instanceof errors.LevelUPError)
next()
var next = after(12, done)

;[null, undefined].forEach(function (nullish) {
db.get(nullish, function (err) {
t.is(err.message, 'key cannot be `null` or `undefined`')
next()
})

db.del(nullish, function (err) {
t.is(err.message, 'key cannot be `null` or `undefined`')
next()
})

db.put(nullish, 'value', function (err) {
t.is(err.message, 'key cannot be `null` or `undefined`')
next()
})

db.put('foo', nullish, function (err, value) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})

db.batch([{ key: nullish, value: 'bar', type: 'put' }], function (err) {
t.is(err.message, 'key cannot be `null` or `undefined`')
next()
})

db.batch([{ key: 'foo', value: nullish, type: 'put' }], function (err) {
t.is(err.message, 'value cannot be `null` or `undefined`')
next()
})
})
})
})
Expand Down