From 889de5626416ce34bf0dc3b8f08ba377d2f4a291 Mon Sep 17 00:00:00 2001 From: Calvin Metcalf Date: Fri, 7 Mar 2014 11:41:21 -0500 Subject: [PATCH] add destroy method --- README.md | 1 + abstract-leveldown.js | 16 +++++++++++ abstract/destroy-test.js | 60 ++++++++++++++++++++++++++++++++++++++++ test.js | 25 +++++++++++++++++ testCommon.js | 2 ++ 5 files changed, 104 insertions(+) create mode 100644 abstract/destroy-test.js diff --git a/README.md b/README.md index 1db6d6b5..acf3dcd5 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Remember that each of these methods, if you implement them, will receive exactly ### AbstractLevelDOWN(location) ### AbstractLevelDOWN#_open(options, callback) +### AbstractLevelDOWN#_destroy(options, callback) ### AbstractLevelDOWN#_close(callback) ### AbstractLevelDOWN#_get(key, options, callback) ### AbstractLevelDOWN#_put(key, value, options, callback) diff --git a/abstract-leveldown.js b/abstract-leveldown.js index e56a6410..00881b14 100644 --- a/abstract-leveldown.js +++ b/abstract-leveldown.js @@ -231,6 +231,22 @@ AbstractLevelDOWN.prototype.iterator = function (options) { return new AbstractIterator(this) } +AbstractLevelDOWN.prototype.destroy = function (options, callback) { + if (typeof options == 'function') + callback = options + + if (typeof callback != 'function') + throw new Error('destroy() requires a callback argument') + + if (typeof options != 'object') + options = {} + + if (typeof this._destroy == 'function') + return this._destroy(options, callback) + + process.nextTick(callback) +} + AbstractLevelDOWN.prototype._chainedBatch = function () { return new AbstractChainedBatch(this) } diff --git a/abstract/destroy-test.js b/abstract/destroy-test.js new file mode 100644 index 00000000..b64c725b --- /dev/null +++ b/abstract/destroy-test.js @@ -0,0 +1,60 @@ + + +module.exports.setUp = function (leveldown, test, testCommon) { + test('setUp common', testCommon.setUp) +} + +module.exports.args = function (leveldown, test, testCommon) { + var db = leveldown(testCommon.location()) + test('test database destroy no-arg throws', function (t) { + t.throws( + db.destroy.bind(db) + , { name: 'Error', message: 'destroy() requires a callback argument' } + , 'no-arg destroy() throws' + ) + t.end() + }) + + test('test callback-less, 1-arg, destroy() throws', function (t) { + var db = leveldown(testCommon.location()) + t.throws( + db.destroy.bind(db, {}) + , { name: 'Error', message: 'destroy() requires a callback argument' } + , 'callback-less, 1-arg destroy() throws' + ) + t.end() + }) +} + +module.exports.destroy = function (leveldown, test, testCommon) { + test('test database destroy, no options', function (t) { + var db = leveldown(testCommon.location()) + + // default createIfMissing=true, errorIfExists=false + db.destroy(function (err) { + t.notOk(err, 'no error') + t.end() + }) + }) + + test('test database open, options and callback', function (t) { + var db = leveldown(testCommon.location()) + + // default createIfMissing=true, errorIfExists=false + db.open({}, function (err) { + t.notOk(err, 'no error') + t.end() + }) + }) +} + +module.exports.tearDown = function (test, testCommon) { + test('tearDown', testCommon.tearDown) +} + +module.exports.all = function (leveldown, test, testCommon) { + module.exports.setUp(test, testCommon) + module.exports.args(leveldown, test, testCommon) + module.exports.destroy(leveldown, test, testCommon) + module.exports.tearDown(test, testCommon) +} \ No newline at end of file diff --git a/test.js b/test.js index 4ec28702..d5b52e1f 100644 --- a/test.js +++ b/test.js @@ -35,6 +35,8 @@ require('./abstract/put-get-del-test').tearDown(tap.test, testCommon) require('./abstract/approximate-size-test').setUp(factory, tap.test, testCommon) require('./abstract/approximate-size-test').args(tap.test) +require('./abstract/destroy-test').args(factory, tap.test, testCommon) + require('./abstract/batch-test').setUp(factory, tap.test, testCommon) require('./abstract/batch-test').args(tap.test) @@ -255,6 +257,29 @@ tap.test('test approximateSize() extensibility', function (t) { t.end() }) +tap.test('test destroy() extensibility', function (t) { + var spy = sinon.spy() + , expectedCb = function () {} + , test + + function Test (location) { + AbstractLevelDOWN.call(this, location) + } + + util.inherits(Test, AbstractLevelDOWN) + + Test.prototype._destroy = spy + + test = new Test('foobar') + test.destroy(expectedCb) + + t.equal(spy.callCount, 1, 'got _destroy() call') + t.equal(spy.getCall(0).thisValue, test, '`this` on _destroy() was correct') + t.equal(spy.getCall(0).args.length, 2, 'got two arguments') + t.equal(spy.getCall(0).args[1], expectedCb, 'got expected cb argument') + t.end() +}) + tap.test('test batch() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} diff --git a/testCommon.js b/testCommon.js index 3f9d29da..e368f5da 100644 --- a/testCommon.js +++ b/testCommon.js @@ -26,6 +26,8 @@ var dbidx = 0 if (!list.length) return callback() + return callback() + var ret = 0 list.forEach(function (f) {