From 19e1967d24623bb04d373083885e111065e8e84c Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 5 Apr 2020 18:53:08 +0200 Subject: [PATCH] Increase abstract-leveldown parity - Add db property to chained batch - Remove type checks that are also performed by abstract-leveldown Goes hand in hand with Level/abstract-leveldown#364. --- lib/batch.js | 3 +- lib/levelup.js | 19 +-------- test/argument-checking-test.js | 37 ----------------- test/get-put-del-test.js | 9 ----- test/index.js | 1 - test/null-and-undefined-test.js | 72 +++++++++++++++------------------ 6 files changed, 36 insertions(+), 105 deletions(-) delete mode 100644 test/argument-checking-test.js diff --git a/lib/batch.js b/lib/batch.js index 45535eb6..c59b5f0d 100644 --- a/lib/batch.js +++ b/lib/batch.js @@ -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 diff --git a/lib/levelup.js b/lib/levelup.js index 60e2ef74..efc30d36 100644 --- a/lib/levelup.js +++ b/lib/levelup.js @@ -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) @@ -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 @@ -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 @@ -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() diff --git a/test/argument-checking-test.js b/test/argument-checking-test.js deleted file mode 100644 index 35023de4..00000000 --- a/test/argument-checking-test.js +++ /dev/null @@ -1,37 +0,0 @@ -module.exports = function (test, testCommon) { - test('argument checking', function (t) { - var db = testCommon.factory() - - 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' - ) - - t.throws( - db.batch.bind(db, null, {}), - /^WriteError: batch\(\) requires an array argument$/, - 'null-arg batch() throws' - ) - - t.throws( - db.batch.bind(db, {}), - /^WriteError: batch\(\) requires an array argument$/, - '1-arg, no array batch() throws' - ) - - db.close(t.end.bind(t)) - }) -} diff --git a/test/get-put-del-test.js b/test/get-put-del-test.js index 0e3c825a..c624193c 100644 --- a/test/get-put-del-test.js +++ b/test/get-put-del-test.js @@ -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() - }) - }) } diff --git a/test/index.js b/test/index.js index 726e0b7a..55fc2a47 100644 --- a/test/index.js +++ b/test/index.js @@ -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) diff --git a/test/null-and-undefined-test.js b/test/null-and-undefined-test.js index 283b6fe4..1ea4e30f 100644 --- a/test/null-and-undefined-test.js +++ b/test/null-and-undefined-test.js @@ -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() + }) }) }) })