pax_global_header00006660000000000000000000000064126064743340014523gustar00rootroot0000000000000052 comment=04d1ff0885af9f79e91a5cac43ff692162120901 node-abstract-leveldown-2.4.1/000077500000000000000000000000001260647433400162525ustar00rootroot00000000000000node-abstract-leveldown-2.4.1/.gitignore000066400000000000000000000000151260647433400202360ustar00rootroot00000000000000node_modules node-abstract-leveldown-2.4.1/.jshintrc000066400000000000000000000022151260647433400200770ustar00rootroot00000000000000{ "predef": [ ] , "bitwise": false , "camelcase": false , "curly": false , "eqeqeq": false , "forin": false , "immed": false , "latedef": false , "newcap": true , "noarg": true , "noempty": true , "nonew": true , "plusplus": false , "quotmark": true , "regexp": false , "undef": true , "unused": true , "strict": false , "trailing": true , "maxlen": 120 , "asi": true , "boss": true , "debug": true , "eqnull": true , "esnext": true , "evil": true , "expr": true , "funcscope": false , "globalstrict": false , "iterator": false , "lastsemic": true , "laxbreak": true , "laxcomma": true , "loopfunc": true , "multistr": false , "onecase": false , "proto": false , "regexdash": false , "scripturl": true , "smarttabs": false , "shadow": false , "sub": true , "supernew": false , "validthis": true , "browser": true , "couch": false , "devel": false , "dojo": false , "mootools": false , "node": true , "nonstandard": true , "prototypejs": false , "rhino": false , "worker": true , "wsh": false , "nomen": false , "onevar": true , "passfail": false }node-abstract-leveldown-2.4.1/.travis.yml000066400000000000000000000003221260647433400203600ustar00rootroot00000000000000language: node_js before_install: npm install npm -g node_js: - "0.8" - "0.10" - "0.12" - "iojs" branches: only: - master notifications: email: - rod@vagg.org - ralphtheninja@riseup.net node-abstract-leveldown-2.4.1/CHANGELOG.md000066400000000000000000000123631260647433400200700ustar00rootroot00000000000000### 2.3.1 - May 18 2015 * [[`393c781629`](https://github.com/level/abstract-leveldown/commit/393c781629)] - document isLevelDown() (Lars-Magnus Skog) * [[`fd899c49b9`](https://github.com/level/abstract-leveldown/commit/fd899c49b9)] - link to level/community (Lars-Magnus Skog) ### 2.3.0 - May 18 2015 * [[`9a976428e2`](https://github.com/level/abstract-leveldown/commit/9a976428e2)] - export from index.js and factor out into is-leveldown.js (Lars-Magnus Skog) * [[`8051f8f16c`](https://github.com/level/abstract-leveldown/commit/8051f8f16c)] - add isLevelDOWN() f unction (Lars-Magnus Skog) ### 2.2.2 - May 13 2015 * [[`4ff0a9bfbb`](https://github.com/level/abstract-leveldown/commit/4ff0a9bfbb)] - ***Revert*** "Merge pull request #60 from ralphtheninja/empty-location" (Julian Gruber) * [[`fab11e9e3b`](https://github.com/level/abstract-leveldown/commit/fab11e9e3b)] - use t.equal instead of t.ok(a === b) (Julian Gruber) ### 2.2.1 - May 12 2015 * [[`f5051090e4`](https://github.com/level/abstract-leveldown/commit/f5051090e4)] - merge location string checks into one if-statement (Lars-Magnus Skog) * [[`cd362b2b9f`](https://github.com/level/abstract-leveldown/commit/cd362b2b9f)] - empty location string throws (Lars-Magnus Skog) * [[`e6d1cb80ea`](https://github.com/level/abstract-leveldown/commit/e6d1cb80ea)] - .throws is different for tape (Lars-Magnus Skog) * [[`a6f29b62fa`](https://github.com/level/abstract-leveldown/commit/a6f29b62fa)] - copy paste error gave wrong test description (Lars-Magnus Skog) ### 2.2.0 - May 10 2015 * [[`aa867b3760`](https://github.com/level/abstract-leveldown/commit/aa867b3760)] - Merge pull request #58 from Level/add/put-sync (Julian Gruber) * [[`234de997bb`](https://github.com/level/abstract-leveldown/commit/234de997bb)] - add sync put tests (Julian Gruber) ### 2.1.4 - Apr 28 2015 * [[`969116d00f`](https://github.com/level/abstract-leveldown/commit/969116d00f)] - use t.equal() with tape (Lars-Magnus Skog) ### 2.1.3 - Apr 28 2015 * [[`68096e78cd`](https://github.com/level/abstract-leveldown/commit/68096e78cd)] - change from tap to tape (Lars-Magnus Skog) ### 2.1.2 - Apr 27 2015 * [[`d79c060c9d`](https://github.com/level/abstract-leveldown/commit/d79c060c9d)] - convert buffer to string so we can compare (Lars-Magnus Skog) ### 2.1.1 - Apr 27 2015 * [[`3881fc4290`](https://github.com/level/abstract-leveldown/commit/3881fc4290)] - **travis**: update npm so 0.8 works, add 0.12 and iojs (Lars-Magnus Skog) * [[`9f451e8f74`](https://github.com/level/abstract-leveldown/commit/9f451e8f74)] - rvagg/node- -> level/ (Lars-Magnus Skog) * [[`ecd41a72db`](https://github.com/level/abstract-leveldown/commit/ecd41a72db)] - fix typo (Hao-kang Den) * [[`20e91fd234`](https://github.com/level/abstract-leveldown/commit/20e91fd234)] - update logo and copyright (Lars-Magnus Skog) * [[`6ccf134874`](https://github.com/level/abstract-leveldown/commit/6ccf134874)] - added @watson to package.json (Rod Vagg) ### 2.1.0 - Nov 9 2014 * [[`7451cd15e6`](https://github.com/level/abstract-leveldown/commit/7451cd15e6)] - added @watson (Rod Vagg) * [[`f4a3346da7`](https://github.com/level/abstract-leveldown/commit/f4a3346da7)] - Use `error` test function when testing for errors (Thomas Watson Steen) * [[`24668c50e0`](https://github.com/level/abstract-leveldown/commit/24668c50e0)] - Don't fail if no value is returned by _get (Thomas Watson Steen) * [[`865ed9e777`](https://github.com/level/abstract-leveldown/commit/865ed9e777)] - Use `setTimeout` instead of `setImmediate`. (Alan Gutierrez) * [[`9e9069faed`](https://github.com/level/abstract-leveldown/commit/9e9069faed)] - 2.0.3 (Rod Vagg) ### 2.0.3 - Oct 2 2014 * [[`78052c53eb`](https://github.com/level/abstract-leveldown/commit/78052c53eb)] - add test for atomic batch operations (Calvin Metcalf) ### 2.0.1 - Sep 1 2014 * [[`a0b36f6a18`](https://github.com/level/abstract-leveldown/commit/a0b36f6a18)] - Remove default options that's too LevelDOWN specific (Thomas Watson Steen) * [[`1d97993d0b`](https://github.com/level/abstract-leveldown/commit/1d97993d0b)] - Allow boolean options to be falsy/truthy (Thomas Watson Steen) * [[`fb3cf56da5`](https://github.com/level/abstract-leveldown/commit/fb3cf56da5)] - Set defaults for open, get, put, del and batch options (Thomas Watson Steen) * [[`5c2a629e2b`](https://github.com/level/abstract-leveldown/commit/5c2a629e2b)] - Update pattern for setting default options for the iterator (Thomas Watson Steen) ### 2.0.0 - Aug 26 2014 * Lots of stuff between 0.11.1 and now, omitted updating changelog * Switch to allowing writes of empty values: null, undefined, '', [] ### 0.11.1 - Nov 15 2013 * Adjust approximate-size-test.js to account for snappy compression ### 0.11.0 - Oct 14 2013 * Introduce _setupIteratorOptions() method to fix options object prior to _iterator() call; makes working with gt/gte/lt/lte options a little easier (@rvagg) ### 0.10.2 - Sep 6 2013 * Refactor duplicated versions of isTypedArray into util.js (@rvagg) * Refactor duplicated versions of 'NotFound' checks into util.js, fixed too-strict version in get-test.js (@rvagg) ### 0.10.1 - Aug 29 2013 * Relax check for 'Not Found: ' in error message to be case insensitive in get-test.js (@rvagg) ### 0.10.0 - Aug 19 2013 * Added test for gt, gte, lt, lte ranges (@dominictarr) node-abstract-leveldown-2.4.1/LICENSE.md000066400000000000000000000021451260647433400176600ustar00rootroot00000000000000The MIT License (MIT) ===================== Copyright (c) 2014 Rod Vagg --------------------------- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-abstract-leveldown-2.4.1/README.md000066400000000000000000000163021260647433400175330ustar00rootroot00000000000000# Abstract LevelDOWN [![Build Status](https://secure.travis-ci.org/Level/abstract-leveldown.png)](http://travis-ci.org/Level/abstract-leveldown) LevelDB Logo [![NPM](https://nodei.co/npm/abstract-leveldown.png?downloads=true&downloadRank=true)](https://nodei.co/npm/abstract-leveldown/) [![NPM](https://nodei.co/npm-dl/abstract-leveldown.png?months=6&height=3)](https://nodei.co/npm/abstract-leveldown/) An abstract prototype matching the **[LevelDOWN](https://github.com/level/leveldown/)** API. Useful for extending **[LevelUP](https://github.com/level/levelup)** functionality by providing a replacement to LevelDOWN. As of version 0.7, LevelUP allows you to pass a `'db'` option when you create a new instance. This will override the default LevelDOWN store with a LevelDOWN API compatible object. **Abstract LevelDOWN** provides a simple, operational *noop* base prototype that's ready for extending. By default, all operations have sensible "noops" (operations that essentially do nothing). For example, simple operations such as `.open(callback)` and `.close(callback)` will simply invoke the callback (on a *next tick*). More complex operations perform sensible actions, for example: `.get(key, callback)` will always return a `'NotFound'` `Error` on the callback. You add functionality by implementing the underscore versions of the operations. For example, to implement a `put()` operation you add a `_put()` method to your object. Each of these underscore methods override the default *noop* operations and are always provided with **consistent arguments**, regardless of what is passed in by the client. Additionally, all methods provide argument checking and sensible defaults for optional arguments. All bad-argument errors are compatible with LevelDOWN (they pass the LevelDOWN method arguments tests). For example, if you call `.open()` without a callback argument you'll get an `Error('open() requires a callback argument')`. Where optional arguments are involved, your underscore methods will receive sensible defaults. A `.get(key, callback)` will pass through to a `._get(key, options, callback)` where the `options` argument is an empty object. ## Example A simplistic in-memory LevelDOWN replacement ```js var util = require('util') , AbstractLevelDOWN = require('./').AbstractLevelDOWN // constructor, passes through the 'location' argument to the AbstractLevelDOWN constructor function FakeLevelDOWN (location) { AbstractLevelDOWN.call(this, location) } // our new prototype inherits from AbstractLevelDOWN util.inherits(FakeLevelDOWN, AbstractLevelDOWN) // implement some methods FakeLevelDOWN.prototype._open = function (options, callback) { // initialise a memory storage object this._store = {} // optional use of nextTick to be a nice async citizen process.nextTick(function () { callback(null, this) }.bind(this)) } FakeLevelDOWN.prototype._put = function (key, value, options, callback) { key = '_' + key // safety, to avoid key='__proto__'-type skullduggery this._store[key] = value process.nextTick(callback) } FakeLevelDOWN.prototype._get = function (key, options, callback) { var value = this._store['_' + key] if (value === undefined) { // 'NotFound' error, consistent with LevelDOWN API return process.nextTick(function () { callback(new Error('NotFound')) }) } process.nextTick(function () { callback(null, value) }) } FakeLevelDOWN.prototype._del = function (key, options, callback) { delete this._store['_' + key] process.nextTick(callback) } // now use it in LevelUP var levelup = require('levelup') var db = levelup('/who/cares/', { // the 'db' option replaces LevelDOWN db: function (location) { return new FakeLevelDOWN(location) } }) db.put('foo', 'bar', function (err) { if (err) throw err db.get('foo', function (err, value) { if (err) throw err console.log('Got foo =', value) }) }) ``` See [MemDOWN](https://github.com/rvagg/memdown/) if you are looking for a complete in-memory replacement for LevelDOWN. ## Extensible API Remember that each of these methods, if you implement them, will receive exactly the number and order of arguments described. Optional arguments will be converted to sensible defaults. ### AbstractLevelDOWN(location) ### AbstractLevelDOWN#_open(options, callback) ### AbstractLevelDOWN#_close(callback) ### AbstractLevelDOWN#_get(key, options, callback) ### AbstractLevelDOWN#_put(key, value, options, callback) ### AbstractLevelDOWN#_del(key, options, callback) ### AbstractLevelDOWN#_batch(array, options, callback) If `batch()` is called without argument or with only an options object then it should return a `Batch` object with chainable methods. Otherwise it will invoke a classic batch operation. ### AbstractLevelDOWN#_chainedBatch() By default an `batch()` operation without argument returns a blank `AbstractChainedBatch` object. The prototype is available on the main exports for you to extend. If you want to implement chainable batch operations then you should extend the `AbstractChaindBatch` and return your object in the `_chainedBatch()` method. ### AbstractLevelDOWN#_approximateSize(start, end, callback) ### AbstractLevelDOWN#_iterator(options) By default an `iterator()` operation returns a blank `AbstractIterator` object. The prototype is available on the main exports for you to extend. If you want to implement iterator operations then you should extend the `AbstractIterator` and return your object in the `_iterator(options)` method. `AbstractIterator` implements the basic state management found in LevelDOWN. It keeps track of when a `next()` is in progress and when an `end()` has been called so it doesn't allow concurrent `next()` calls, it does it allow `end()` while a `next()` is in progress and it doesn't allow either `next()` or `end()` after `end()` has been called. ### AbstractIterator(db) Provided with the current instance of `AbstractLevelDOWN` by default. ### AbstractIterator#_next(callback) ### AbstractIterator#_end(callback) ### AbstractChainedBatch Provided with the current instance of `AbstractLevelDOWN` by default. ### AbstractChainedBatch#_put(key, value) ### AbstractChainedBatch#_del(key) ### AbstractChainedBatch#_clear() ### AbstractChainedBatch#_write(options, callback) ### isLevelDown(db) Returns `true` if `db` has the same public api as `AbstractLevelDOWN`, otherwise `false`. This is a utility function and it's not part of the extensible api. Contributing ------------ AbstractLevelDOWN is an **OPEN Open Source Project**. This means that: > Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project. See the [contribution guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details. License & Copyright ------------------- Copyright © 2012-2015 **AbstractLevelDOWN** [contributors](https://github.com/level/community#contributors). **AbstractLevelDOWN** is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included `LICENSE.md` file for more details. node-abstract-leveldown-2.4.1/abstract-chained-batch.js000066400000000000000000000034761260647433400230750ustar00rootroot00000000000000/* Copyright (c) 2013 Rod Vagg, MIT License */ function AbstractChainedBatch (db) { this._db = db this._operations = [] this._written = false } AbstractChainedBatch.prototype._checkWritten = function () { if (this._written) throw new Error('write() already called on this batch') } AbstractChainedBatch.prototype.put = function (key, value) { this._checkWritten() var err = this._db._checkKey(key, 'key', this._db._isBuffer) if (err) throw err if (!this._db._isBuffer(key)) key = String(key) if (!this._db._isBuffer(value)) value = String(value) if (typeof this._put == 'function' ) this._put(key, value) else this._operations.push({ type: 'put', key: key, value: value }) return this } AbstractChainedBatch.prototype.del = function (key) { this._checkWritten() var err = this._db._checkKey(key, 'key', this._db._isBuffer) if (err) throw err if (!this._db._isBuffer(key)) key = String(key) if (typeof this._del == 'function' ) this._del(key) else this._operations.push({ type: 'del', key: key }) return this } AbstractChainedBatch.prototype.clear = function () { this._checkWritten() this._operations = [] if (typeof this._clear == 'function' ) this._clear() return this } AbstractChainedBatch.prototype.write = function (options, callback) { this._checkWritten() if (typeof options == 'function') callback = options if (typeof callback != 'function') throw new Error('write() requires a callback argument') if (typeof options != 'object') options = {} this._written = true if (typeof this._write == 'function' ) return this._write(callback) if (typeof this._db._batch == 'function') return this._db._batch(this._operations, options, callback) process.nextTick(callback) } module.exports = AbstractChainedBatchnode-abstract-leveldown-2.4.1/abstract-iterator.js000066400000000000000000000022211260647433400222370ustar00rootroot00000000000000/* Copyright (c) 2013 Rod Vagg, MIT License */ function AbstractIterator (db) { this.db = db this._ended = false this._nexting = false } AbstractIterator.prototype.next = function (callback) { var self = this if (typeof callback != 'function') throw new Error('next() requires a callback argument') if (self._ended) return callback(new Error('cannot call next() after end()')) if (self._nexting) return callback(new Error('cannot call next() before previous next() has completed')) self._nexting = true if (typeof self._next == 'function') { return self._next(function () { self._nexting = false callback.apply(null, arguments) }) } process.nextTick(function () { self._nexting = false callback() }) } AbstractIterator.prototype.end = function (callback) { if (typeof callback != 'function') throw new Error('end() requires a callback argument') if (this._ended) return callback(new Error('end() already called on iterator')) this._ended = true if (typeof this._end == 'function') return this._end(callback) process.nextTick(callback) } module.exports = AbstractIterator node-abstract-leveldown-2.4.1/abstract-leveldown.js000066400000000000000000000154201260647433400224120ustar00rootroot00000000000000/* Copyright (c) 2013 Rod Vagg, MIT License */ var xtend = require('xtend') , AbstractIterator = require('./abstract-iterator') , AbstractChainedBatch = require('./abstract-chained-batch') function AbstractLevelDOWN (location) { if (!arguments.length || location === undefined) throw new Error('constructor requires at least a location argument') if (typeof location != 'string') throw new Error('constructor requires a location string argument') this.location = location this.status = 'new' } AbstractLevelDOWN.prototype.open = function (options, callback) { var self = this , oldStatus = this.status if (typeof options == 'function') callback = options if (typeof callback != 'function') throw new Error('open() requires a callback argument') if (typeof options != 'object') options = {} options.createIfMissing = options.createIfMissing != false options.errorIfExists = !!options.errorIfExists if (typeof this._open == 'function') { this.status = 'opening' this._open(options, function (err) { if (err) { self.status = oldStatus return callback(err) } self.status = 'open' callback() }) } else { this.status = 'open' process.nextTick(callback) } } AbstractLevelDOWN.prototype.close = function (callback) { var self = this , oldStatus = this.status if (typeof callback != 'function') throw new Error('close() requires a callback argument') if (typeof this._close == 'function') { this.status = 'closing' this._close(function (err) { if (err) { self.status = oldStatus return callback(err) } self.status = 'closed' callback() }) } else { this.status = 'closed' process.nextTick(callback) } } AbstractLevelDOWN.prototype.get = function (key, options, callback) { var err if (typeof options == 'function') callback = options if (typeof callback != 'function') throw new Error('get() requires a callback argument') if (err = this._checkKey(key, 'key', this._isBuffer)) return callback(err) if (!this._isBuffer(key)) key = String(key) if (typeof options != 'object') options = {} options.asBuffer = options.asBuffer != false if (typeof this._get == 'function') return this._get(key, options, callback) process.nextTick(function () { callback(new Error('NotFound')) }) } AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { var err if (typeof options == 'function') callback = options if (typeof callback != 'function') throw new Error('put() requires a callback argument') if (err = this._checkKey(key, 'key', this._isBuffer)) return callback(err) if (!this._isBuffer(key)) key = String(key) // coerce value to string in node, don't touch it in browser // (indexeddb can store any JS type) if (value != null && !this._isBuffer(value) && !process.browser) value = String(value) if (typeof options != 'object') options = {} if (typeof this._put == 'function') return this._put(key, value, options, callback) process.nextTick(callback) } AbstractLevelDOWN.prototype.del = function (key, options, callback) { var err if (typeof options == 'function') callback = options if (typeof callback != 'function') throw new Error('del() requires a callback argument') if (err = this._checkKey(key, 'key', this._isBuffer)) return callback(err) if (!this._isBuffer(key)) key = String(key) if (typeof options != 'object') options = {} if (typeof this._del == 'function') return this._del(key, options, callback) process.nextTick(callback) } AbstractLevelDOWN.prototype.batch = function (array, options, callback) { if (!arguments.length) return this._chainedBatch() if (typeof options == 'function') callback = options if (typeof array == 'function') callback = array if (typeof callback != 'function') throw new Error('batch(array) requires a callback argument') if (!Array.isArray(array)) return callback(new Error('batch(array) requires an array argument')) if (!options || typeof options != 'object') options = {} var i = 0 , l = array.length , e , err for (; i < l; i++) { e = array[i] if (typeof e != 'object') continue if (err = this._checkKey(e.type, 'type', this._isBuffer)) return callback(err) if (err = this._checkKey(e.key, 'key', this._isBuffer)) return callback(err) } if (typeof this._batch == 'function') return this._batch(array, options, callback) process.nextTick(callback) } //TODO: remove from here, not a necessary primitive AbstractLevelDOWN.prototype.approximateSize = function (start, end, callback) { if ( start == null || end == null || typeof start == 'function' || typeof end == 'function') { throw new Error('approximateSize() requires valid `start`, `end` and `callback` arguments') } if (typeof callback != 'function') throw new Error('approximateSize() requires a callback argument') if (!this._isBuffer(start)) start = String(start) if (!this._isBuffer(end)) end = String(end) if (typeof this._approximateSize == 'function') return this._approximateSize(start, end, callback) process.nextTick(function () { callback(null, 0) }) } AbstractLevelDOWN.prototype._setupIteratorOptions = function (options) { var self = this options = xtend(options) ;[ 'start', 'end', 'gt', 'gte', 'lt', 'lte' ].forEach(function (o) { if (options[o] && self._isBuffer(options[o]) && options[o].length === 0) delete options[o] }) options.reverse = !!options.reverse options.keys = options.keys != false options.values = options.values != false options.limit = 'limit' in options ? options.limit : -1 options.keyAsBuffer = options.keyAsBuffer != false options.valueAsBuffer = options.valueAsBuffer != false return options } AbstractLevelDOWN.prototype.iterator = function (options) { if (typeof options != 'object') options = {} options = this._setupIteratorOptions(options) if (typeof this._iterator == 'function') return this._iterator(options) return new AbstractIterator(this) } AbstractLevelDOWN.prototype._chainedBatch = function () { return new AbstractChainedBatch(this) } AbstractLevelDOWN.prototype._isBuffer = function (obj) { return Buffer.isBuffer(obj) } AbstractLevelDOWN.prototype._checkKey = function (obj, type) { if (obj === null || obj === undefined) return new Error(type + ' cannot be `null` or `undefined`') if (this._isBuffer(obj)) { if (obj.length === 0) return new Error(type + ' cannot be an empty Buffer') } else if (String(obj) === '') return new Error(type + ' cannot be an empty String') } module.exports = AbstractLevelDOWN node-abstract-leveldown-2.4.1/abstract/000077500000000000000000000000001260647433400200555ustar00rootroot00000000000000node-abstract-leveldown-2.4.1/abstract/approximate-size-test.js000066400000000000000000000071631260647433400247000ustar00rootroot00000000000000var db module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test argument-less approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db) , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } , 'no-arg approximateSize() throws' ) t.end() }) test('test callback-less, 1-arg, approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db, 'foo') , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } , 'callback-less, 1-arg approximateSize() throws' ) t.end() }) test('test callback-less, 2-arg, approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db, 'foo', 'bar') , { name: 'Error', message: 'approximateSize() requires a callback argument' } , 'callback-less, 2-arg approximateSize() throws' ) t.end() }) test('test callback-less, 3-arg, approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db, function () {}) , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } , 'callback-only approximateSize() throws' ) t.end() }) test('test callback-only approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db, function () {}) , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } , 'callback-only approximateSize() throws' ) t.end() }) test('test 1-arg + callback approximateSize() throws', function (t) { t.throws( db.approximateSize.bind(db, 'foo', function () {}) , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } , '1-arg + callback approximateSize() throws' ) t.end() }) } module.exports.approximateSize = function (test) { test('test approximateSize()', function (t) { var data = Array.apply(null, Array(10000)).map(function () { return 'aaaaaaaaaa' }).join('') db.batch( Array.apply(null, Array(10)).map(function (x, i) { return { type: 'put', key: 'foo' + i, value: data } }) , function (err) { t.error(err) // cycle open/close to ensure a pack to .sst db.close(function (err) { t.error(err) db.open(function (err) { t.error(err) db.approximateSize('!', '~', function (err, size) { t.error(err) t.equal(typeof size, 'number') t.ok( size > 40000 // account for snappy compression // original would be ~100000 , 'size reports a reasonable amount (' + size + ')' ) db.close(function (err) { t.error(err) t.end() }) }) }) }) } ) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.approximateSize(test) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/batch-test.js000066400000000000000000000130321260647433400224500ustar00rootroot00000000000000var db , verifyNotFoundError = require('./util').verifyNotFoundError , isTypedArray = require('./util').isTypedArray module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test callback-less, 2-arg, batch() throws', function (t) { t.throws(db.batch.bind(db, 'foo', {}), 'callback-less, 2-arg batch() throws') t.end() }) test('test batch() with missing `value`', function (t) { db.batch([{ type: 'put', key: 'foo1' }], function (err) { t.error(err) t.end() }) }) test('test batch() with null `value`', function (t) { db.batch([{ type: 'put', key: 'foo1', value: null }], function (err) { t.error(err) t.end() }) }) test('test batch() with missing `key`', function (t) { db.batch([{ type: 'put', value: 'foo1' }], function (err) { t.ok(err, 'got error') t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') t.end() }) }) test('test batch() with null `key`', function (t) { db.batch([{ type: 'put', key: null, value: 'foo1' }], function (err) { t.ok(err, 'got error') t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') t.end() }) }) test('test batch() with missing `key` and `value`', function (t) { db.batch([{ type: 'put' }], function (err) { t.ok(err, 'got error') t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') t.end() }) }) test('test batch() with missing array', function (t) { db.batch(function (err) { t.ok(err, 'got error') t.equal(err.message, 'batch(array) requires an array argument', 'correct error message') t.end() }) }) test('test batch() with undefined array', function (t) { db.batch(void 0, function (err) { t.ok(err, 'got error') t.equal(err.message, 'batch(array) requires an array argument', 'correct error message') t.end() }) }) test('test batch() with null array', function (t) { db.batch(null, function (err) { t.ok(err, 'got error') t.equal(err.message, 'batch(array) requires an array argument', 'correct error message') t.end() }) }) test('test batch() with null options', function (t) { db.batch([], null, function (err) { t.error(err) t.end() }) }) } module.exports.batch = function (test) { test('test batch() with empty array', function (t) { db.batch([], function (err) { t.error(err) t.end() }) }) test('test simple batch()', function (t) { db.batch([{ type: 'put', key: 'foo', value: 'bar' }], function (err) { t.error(err) db.get('foo', function (err, value) { t.error(err) var result if (isTypedArray(value)) { result = String.fromCharCode.apply(null, new Uint16Array(value)) } else { t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) result = value.toString() } t.equal(result, 'bar') t.end() }) }) }) test('test multiple batch()', function (t) { db.batch([ { type: 'put', key: 'foobatch1', value: 'bar1' } , { type: 'put', key: 'foobatch2', value: 'bar2' } , { type: 'put', key: 'foobatch3', value: 'bar3' } , { type: 'del', key: 'foobatch2' } ], function (err) { t.error(err) var r = 0 , done = function () { if (++r == 3) t.end() } db.get('foobatch1', function (err, value) { t.error(err) var result if (isTypedArray(value)) { result = String.fromCharCode.apply(null, new Uint16Array(value)) } else { t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) result = value.toString() } t.equal(result, 'bar1') done() }) db.get('foobatch2', function (err, value) { t.ok(err, 'entry not found') t.ok(typeof value == 'undefined', 'value is undefined') t.ok(verifyNotFoundError(err), 'NotFound error') done() }) db.get('foobatch3', function (err, value) { t.error(err) var result if (isTypedArray(value)) { result = String.fromCharCode.apply(null, new Uint16Array(value)) } else { t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) result = value.toString() } t.equal(result, 'bar3') done() }) }) }) } module.exports.atomic = function (test) { test('test multiple batch()', function (t) { t.plan(3) db.batch([ { type: 'put', key: 'foobah1', value: 'bar1' } , { type: 'put', value: 'bar2' } , { type: 'put', key: 'foobah3', value: 'bar3' } ], function (err) { t.ok(err, 'should error') db.get('foobah1', function (err) { t.ok(err, 'should not be found') }) db.get('foobah3', function (err) { t.ok(err, 'should not be found') }) }) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.batch(test) module.exports.atomic(test) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/chained-batch-test.js000066400000000000000000000131131260647433400240410ustar00rootroot00000000000000var db module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test batch#put() with missing `value`', function (t) { db.batch().put('foo1') t.end() }) test('test batch#put() with null `value`', function (t) { db.batch().put('foo1', null) t.end() }) test('test batch#put() with missing `key`', function (t) { try { db.batch().put(undefined, 'foo1') } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#put() with null `key`', function (t) { try { db.batch().put(null, 'foo1') } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#put() with missing `key` and `value`', function (t) { try { db.batch().put() } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#del() with missing `key`', function (t) { try { db.batch().del() } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#del() with null `key`', function (t) { try { db.batch().del(null) } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#del() with null `key`', function (t) { try { db.batch().del(null) } catch (err) { t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#clear() doesn\'t throw', function (t) { db.batch().clear() t.end() }) test('test batch#write() with no callback', function (t) { try { db.batch().write() } catch (err) { t.equal(err.message, 'write() requires a callback argument', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#put() after write()', function (t) { var batch = db.batch().put('foo', 'bar') batch.write(function () {}) try { batch.put('boom', 'bang') } catch (err) { t.equal(err.message, 'write() already called on this batch', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#del() after write()', function (t) { var batch = db.batch().put('foo', 'bar') batch.write(function () {}) try { batch.del('foo') } catch (err) { t.equal(err.message, 'write() already called on this batch', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#clear() after write()', function (t) { var batch = db.batch().put('foo', 'bar') batch.write(function () {}) try { batch.clear() } catch (err) { t.equal(err.message, 'write() already called on this batch', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) test('test batch#write() after write()', function (t) { var batch = db.batch().put('foo', 'bar') batch.write(function () {}) try { batch.write(function (err) {}) } catch (err) { t.equal(err.message, 'write() already called on this batch', 'correct error message') return t.end() } t.fail('should have thrown') t.end() }) } module.exports.batch = function (test, testCommon) { test('test basic batch', function (t) { db.batch( [ { type: 'put', key: 'one', value: '1' } , { type: 'put', key: 'two', value: '2' } , { type: 'put', key: 'three', value: '3' } ] , function (err) { t.error(err) db.batch() .put('1', 'one') .del('2', 'two') .put('3', 'three') .clear() .put('one', 'I') .put('two', 'II') .del('three') .put('foo', 'bar') .write(function (err) { t.error(err) testCommon.collectEntries( db.iterator({ keyAsBuffer: false, valueAsBuffer: false }) , function (err, data) { t.error(err) t.equal(data.length, 3, 'correct number of entries') var expected = [ { key: 'foo', value: 'bar' } , { key: 'one', value: 'I' } , { key: 'two', value: 'II' } ] t.deepEqual(data, expected) t.end() } ) }) } ) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.batch(test, testCommon) module.exports.tearDown(test, testCommon) }node-abstract-leveldown-2.4.1/abstract/close-test.js000066400000000000000000000011641260647433400224770ustar00rootroot00000000000000module.exports.close = function (leveldown, test, testCommon) { test('test close()', function (t) { var db = leveldown(testCommon.location()) db.open(function (err) { t.error(err) t.throws( db.close.bind(db) , { name: 'Error', message: 'close() requires a callback argument' } , 'no-arg close() throws' ) t.throws( db.close.bind(db, 'foo') , { name: 'Error', message: 'close() requires a callback argument' } , 'non-callback close() throws' ) db.close(function (err) { t.error(err) t.end() }) }) }) }node-abstract-leveldown-2.4.1/abstract/del-test.js000066400000000000000000000040371260647433400221400ustar00rootroot00000000000000var db , verifyNotFoundError = require('./util').verifyNotFoundError , isTypedArray = require('./util').isTypedArray module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test argument-less del() throws', function (t) { t.throws( db.del.bind(db) , { name: 'Error', message: 'del() requires a callback argument' } , 'no-arg del() throws' ) t.end() }) test('test callback-less, 1-arg, del() throws', function (t) { t.throws( db.del.bind(db, 'foo') , { name: 'Error', message: 'del() requires a callback argument' } , 'callback-less, 1-arg del() throws' ) t.end() }) test('test callback-less, 3-arg, del() throws', function (t) { t.throws( db.del.bind(db, 'foo', {}) , { name: 'Error', message: 'del() requires a callback argument' } , 'callback-less, 2-arg del() throws' ) t.end() }) } module.exports.del = function (test) { test('test simple del()', function (t) { db.put('foo', 'bar', function (err) { t.error(err) db.del('foo', function (err) { t.error(err) db.get('foo', function (err) { t.ok(err, 'entry propertly deleted') t.ok(typeof value == 'undefined', 'value is undefined') t.ok(verifyNotFoundError(err), 'NotFound error') t.end() }) }) }) }) test('test del on non-existent key', function (t) { db.del('blargh', function (err) { t.error(err) t.end() }) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.del(test) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/get-test.js000066400000000000000000000073411260647433400221540ustar00rootroot00000000000000var db , verifyNotFoundError = require('./util').verifyNotFoundError , isTypedArray = require('./util').isTypedArray module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test argument-less get() throws', function (t) { t.throws( db.get.bind(db) , { name: 'Error', message: 'get() requires a callback argument' } , 'no-arg get() throws' ) t.end() }) test('test callback-less, 1-arg, get() throws', function (t) { t.throws( db.get.bind(db, 'foo') , { name: 'Error', message: 'get() requires a callback argument' } , 'callback-less, 1-arg get() throws' ) t.end() }) test('test callback-less, 3-arg, get() throws', function (t) { t.throws( db.get.bind(db, 'foo', {}) , { name: 'Error', message: 'get() requires a callback argument' } , 'callback-less, 2-arg get() throws' ) t.end() }) } module.exports.get = function (test) { test('test simple get()', function (t) { db.put('foo', 'bar', function (err) { t.error(err) db.get('foo', function (err, value) { t.error(err) t.ok(typeof value !== 'string', 'should not be string by default') var result if (isTypedArray(value)) { result = String.fromCharCode.apply(null, new Uint16Array(value)) } else { t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) try { result = value.toString() } catch (e) { t.error(e, 'should not throw when converting value to a string') } } t.equal(result, 'bar') db.get('foo', {}, function (err, value) { // same but with {} t.error(err) t.ok(typeof value !== 'string', 'should not be string by default') var result if (isTypedArray(value)) { result = String.fromCharCode.apply(null, new Uint16Array(value)) } else { t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) try { result = value.toString() } catch (e) { t.error(e, 'should not throw when converting value to a string') } } t.equal(result, 'bar') db.get('foo', { asBuffer: false }, function (err, value) { t.error(err) t.ok(typeof value === 'string', 'should be string if not buffer') t.equal(value, 'bar') t.end() }) }) }) }) }) test('test simultaniously get()', function (t) { db.put('hello', 'world', function (err) { t.error(err) var r = 0 , done = function () { if (++r == 20) t.end() } , i = 0 , j = 0 for (; i < 10; ++i) db.get('hello', function(err, value) { t.error(err) t.equal(value.toString(), 'world') done() }) for (; j < 10; ++j) db.get('not found', function(err, value) { t.ok(err, 'should error') t.ok(verifyNotFoundError(err), 'should have correct error message') t.ok(typeof value == 'undefined', 'value is undefined') done() }) }) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.get(test) module.exports.tearDown(test, testCommon) }node-abstract-leveldown-2.4.1/abstract/iterator-test.js000066400000000000000000000416331260647433400232300ustar00rootroot00000000000000var db , sourceData = (function () { var d = [] , i = 0 , k for (; i < 100; i++) { k = (i < 10 ? '0' : '') + i d.push({ type : 'put' , key : k , value : Math.random() }) } return d }()) , transformSource = function (d) { return { key: d.key, value: String(d.value) } } module.exports.sourceData = sourceData module.exports.transformSource = transformSource module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test argument-less iterator#next() throws', function (t) { var iterator = db.iterator() t.throws( iterator.next.bind(iterator) , { name: 'Error', message: 'next() requires a callback argument' } , 'no-arg iterator#next() throws' ) iterator.end(t.end.bind(t)) }) test('test argument-less iterator#end() after next() throws', function (t) { var iterator = db.iterator() iterator.next(function () { t.throws( iterator.end.bind(iterator) , { name: 'Error', message: 'end() requires a callback argument' } , 'no-arg iterator#end() throws' ) iterator.end(t.end.bind(t)) }) }) test('test argument-less iterator#end() throws', function (t) { var iterator = db.iterator() t.throws( iterator.end.bind(iterator) , { name: 'Error', message: 'end() requires a callback argument' } , 'no-arg iterator#end() throws' ) iterator.end(t.end.bind(t)) }) } module.exports.sequence = function (test) { test('test twice iterator#end() callback with error', function (t) { var iterator = db.iterator() iterator.end(function (err) { t.error(err) iterator.end(function(err2) { t.ok(err2, 'returned error') t.equal(err2.name, 'Error', 'correct error') t.equal(err2.message, 'end() already called on iterator') t.end() }) }) }) test('test iterator#next after iterator#end() callback with error', function (t) { var iterator = db.iterator() iterator.end(function (err) { t.error(err) iterator.next(function(err2) { t.ok(err2, 'returned error') t.equal(err2.name, 'Error', 'correct error') t.equal(err2.message, 'cannot call next() after end()', 'correct message') t.end() }) }) }) test('test twice iterator#next() throws', function (t) { var iterator = db.iterator() iterator.next(function (err) { t.error(err) iterator.end(function (err) { t.error(err) t.end() }) }) iterator.next(function(err) { t.ok(err, 'returned error') t.equal(err.name, 'Error', 'correct error') t.equal(err.message, 'cannot call next() before previous next() has completed') }) }) } module.exports.iterator = function (leveldown, test, testCommon, collectEntries) { test('test simple iterator()', function (t) { var data = [ { type: 'put', key: 'foobatch1', value: 'bar1' } , { type: 'put', key: 'foobatch2', value: 'bar2' } , { type: 'put', key: 'foobatch3', value: 'bar3' } ] , idx = 0 db.batch(data, function (err) { t.error(err) var iterator = db.iterator() , fn = function (err, key, value) { t.error(err) if (key && value) { t.ok(Buffer.isBuffer(key), 'key argument is a Buffer') t.ok(Buffer.isBuffer(value), 'value argument is a Buffer') t.equal(key.toString(), data[idx].key, 'correct key') t.equal(value.toString(), data[idx].value, 'correct value') process.nextTick(next) idx++ } else { // end t.ok(typeof err === 'undefined', 'err argument is undefined') t.ok(typeof key === 'undefined', 'key argument is undefined') t.ok(typeof value === 'undefined', 'value argument is undefined') t.equal(idx, data.length, 'correct number of entries') iterator.end(function () { t.end() }) } } , next = function () { iterator.next(fn) } next() }) }) /** the following tests are mirroring the same series of tests in * LevelUP read-stream-test.js */ test('setUp #2', function (t) { db.close(function () { db = leveldown(testCommon.location()) db.open(function () { db.batch(sourceData, t.end.bind(t)) }) }) }) test('test full data collection', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, reverse: true }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.slice().reverse().map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=0', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '00' }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=50', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=50 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice().reverse().slice(49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key (49.5)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key (49999)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49999' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice().reverse().slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end=50', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key (50.5)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key (50555)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50555' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 49, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) // end='0', starting key is actually '00' so it should avoid it test('test iterator with end=0', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '0' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with start=30 and end=70', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '30', end: '70' }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice(30, 71).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=30 and end=70 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '70', end: '30', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20 }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20 and start=20', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '20', limit: 20 }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(20, 40).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20 and start=20 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '79', limit: 20, reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice().reverse().slice(20, 40).map(transformSource) t.deepEqual(data, expected) t.end() }) }) // the default limit value from levelup is -1 test('test iterator with limit=-1 should iterate over whole database', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: -1}), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=0 should not iterate over anything', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 0 }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with end after limit', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, end: '50' }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end before limit', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 50, end: '19' }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start after database end', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with start after database end and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.slice().reverse().map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start and end after database and and reverse=true', function (t) { collectEntries(db.iterator({ start: '9b', end: '9a', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) function testIteratorCollectsFullDatabase (name, iteratorOptions) { iteratorOptions.keyAsBuffer = false iteratorOptions.valueAsBuffer = false test(name, function (t) { collectEntries(db.iterator(iteratorOptions), function (err, data) { t.error(err) t.equal(data.length, 100, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) } if (!process.browser) { // Can't use buffers as query keys in indexeddb (I think :P) testIteratorCollectsFullDatabase( 'test iterator with start as empty buffer' , { start: new Buffer(0) } ) testIteratorCollectsFullDatabase( 'test iterator with end as empty buffer' , { end: new Buffer(0) } ) } testIteratorCollectsFullDatabase( 'test iterator with start as empty string' , { start: '' } ) testIteratorCollectsFullDatabase( 'test iterator with start as null' , { start: null } ) testIteratorCollectsFullDatabase( 'test iterator with end as empty string' , { end: '' } ) testIteratorCollectsFullDatabase( 'test iterator with end as null' , { end: null } ) } module.exports.snapshot = function (leveldown, test, testCommon) { test('setUp #3', function (t) { db.close(function () { db = leveldown(testCommon.location()) db.open(function () { db.put('foobatch1', 'bar1', t.end.bind(t)) }) }) }) test('iterator create snapshot correctly', function (t) { var iterator = db.iterator() db.del('foobatch1', function () { iterator.next(function (err, key, value) { t.error(err) t.ok(key, 'got a key') t.equal(key.toString(), 'foobatch1', 'correct key') t.equal(value.toString(), 'bar1', 'correct value') iterator.end(t.end.bind(t)) }) }) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.sequence(test) module.exports.iterator(leveldown, test, testCommon, testCommon.collectEntries) module.exports.snapshot(leveldown, test, testCommon) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/leveldown-test.js000066400000000000000000000014511260647433400233700ustar00rootroot00000000000000module.exports.args = function (leveldown, test) { test('test database creation no-arg throws', function (t) { t.throws( leveldown , { name: 'Error', message: 'constructor requires at least a location argument' } , 'no-arg leveldown() throws' ) t.end() }) test('test database creation non-string location throws', function (t) { t.throws( function () { leveldown({}) } , { name: 'Error', message: 'constructor requires a location string argument' } , 'non-string location leveldown() throws' ) t.end() }) test('test database open no-arg throws', function (t) { var db = leveldown('foo') t.ok(db, 'database object returned') t.ok(typeof db.open === 'function', 'open() function exists') t.end() }) }node-abstract-leveldown-2.4.1/abstract/open-test.js000066400000000000000000000061121260647433400223310ustar00rootroot00000000000000module.exports.setUp = function (test, testCommon) { test('setUp', testCommon.setUp) } module.exports.args = function (leveldown, test, testCommon) { test('test database open no-arg throws', function (t) { var db = leveldown(testCommon.location()) t.throws( db.open.bind(db) , { name: 'Error', message: 'open() requires a callback argument' } , 'no-arg open() throws' ) t.end() }) test('test callback-less, 1-arg, open() throws', function (t) { var db = leveldown(testCommon.location()) t.throws( db.open.bind(db, {}) , { name: 'Error', message: 'open() requires a callback argument' } , 'callback-less, 1-arg open() throws' ) t.end() }) } module.exports.open = function (leveldown, test, testCommon) { test('test database open, no options', function (t) { var db = leveldown(testCommon.location()) // default createIfMissing=true, errorIfExists=false db.open(function (err) { t.error(err) db.close(function () { 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.error(err) db.close(function () { t.end() }) }) }) test('test database open, close and open', function (t) { var db = leveldown(testCommon.location()) db.open(function (err) { t.error(err) db.close(function (err) { t.error(err) db.open(function (err) { t.error(err) db.close(function () { t.end() }) }) }) }) }) } module.exports.openAdvanced = function (leveldown, test, testCommon) { test('test database open createIfMissing:false', function (t) { var db = leveldown(testCommon.location()) db.open({ createIfMissing: false }, function (err) { t.ok(err, 'error') t.ok(/does not exist/.test(err.message), 'error is about dir not existing') t.end() }) }) test('test database open errorIfExists:true', function (t) { var location = testCommon.location() , db = leveldown(location) // make a valid database first, then close and dispose db.open({}, function (err) { t.error(err) db.close(function (err) { t.error(err) // open again with 'errorIfExists' db = leveldown(location) db.open({ createIfMissing: false, errorIfExists: true }, function (err) { t.ok(err, 'error') t.ok(/exists/.test(err.message), 'error is about already existing') 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.open(leveldown, test, testCommon) module.exports.openAdvanced(leveldown, test, testCommon) module.exports.tearDown(test, testCommon) }node-abstract-leveldown-2.4.1/abstract/put-get-del-test.js000066400000000000000000000130701260647433400235200ustar00rootroot00000000000000/**** SETUP & UTILITY STUFF ****/ var db , testBuffer , test , verifyNotFoundError = require('./util').verifyNotFoundError function makeGetDelErrorTests (type, key, expectedError) { test('test get() with ' + type + ' causes error', function (t) { db.get(key, function (err) { t.ok(err, 'has error') t.ok(err instanceof Error) t.ok(err.message.match(expectedError), 'correct error message') t.end() }) }) test('test del() with ' + type + ' causes error', function (t) { db.del(key, function (err) { t.ok(err, 'has error') t.ok(err instanceof Error) t.ok(err.message.match(expectedError), 'correct error message') t.end() }) }) } function makePutErrorTest (type, key, value, expectedError) { test('test put() with ' + type + ' causes error', function (t) { db.put(key, value, function (err) { t.ok(err, 'has error') t.ok(err instanceof Error) t.ok(err.message.match(expectedError), 'correct error message') t.end() }) }) } function makePutGetDelSuccessfulTest (type, key, value, expectedResult) { var hasExpectedResult = arguments.length == 4 test('test put()/get()/del() with ' + type, function (t) { db.put(key, value, function (err) { t.error(err) db.get(key, function (err, _value) { t.error(err, 'no error, has key/value for `' + type + '`') t.ok(Buffer.isBuffer(_value), 'is a Buffer') var result = _value if (hasExpectedResult) { t.equal(result.toString(), expectedResult) } else { if (result != null) result = _value.toString() if (value != null) value = value.toString() t.equals(result, value) } db.del(key, function (err) { t.error(err, 'no error, deleted key/value for `' + type + '`') db.get(key, function (err, value) { t.ok(err, 'entry propertly deleted') t.ok(verifyNotFoundError(err), 'should have correct error message') t.equal(typeof value, 'undefined', 'value is undefined') t.end() }) }) }) }) }) } function makeErrorKeyTest (type, key, expectedError) { makeGetDelErrorTests(type, key, expectedError) makePutErrorTest(type, key, 'foo', expectedError) } /**** SETUP ENVIRONMENT ****/ module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } /**** TEST ERROR KEYS ****/ module.exports.errorKeys = function (testFunc, BufferType) { if (!BufferType) BufferType = Buffer test = testFunc makeErrorKeyTest('null key', null, /key cannot be `null` or `undefined`/) makeErrorKeyTest('undefined key', undefined, /key cannot be `null` or `undefined`/) makeErrorKeyTest('empty String key', '', /key cannot be an empty String/) makeErrorKeyTest('empty Buffer key', new BufferType(0), /key cannot be an empty \w*Buffer/) makeErrorKeyTest('empty Array key', [], /key cannot be an empty String/) } /**** TEST NON-ERROR KEYS ****/ module.exports.nonErrorKeys = function (testFunc) { // valid falsey keys test = testFunc makePutGetDelSuccessfulTest('`false` key', false, 'foo false') makePutGetDelSuccessfulTest('`0` key', 0, 'foo 0') makePutGetDelSuccessfulTest('`NaN` key', NaN, 'foo NaN') // standard String key makePutGetDelSuccessfulTest( 'long String key' , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!' , 'foo' ) if (!process.browser) { // Buffer key makePutGetDelSuccessfulTest('Buffer key', testBuffer, 'foo') } // non-empty Array as a value makePutGetDelSuccessfulTest('Array value', 'foo', [1,2,3,4]) } /**** TEST ERROR VALUES ****/ module.exports.errorValues = function () { } module.exports.nonErrorValues = function (testFunc, BufferType) { if (!BufferType) BufferType = Buffer // valid falsey values test = testFunc makePutGetDelSuccessfulTest('`false` value', 'foo false', false) makePutGetDelSuccessfulTest('`0` value', 'foo 0', 0) makePutGetDelSuccessfulTest('`NaN` value', 'foo NaN', NaN) // all of the following result in an empty-string value: makePutGetDelSuccessfulTest('`null` value', 'foo null', null, '') makePutGetDelSuccessfulTest('`undefined` value', 'foo undefined', undefined, '') makePutGetDelSuccessfulTest('empty String value', 'foo', '', '') makePutGetDelSuccessfulTest('empty Buffer value', 'foo', new BufferType(0), '') makePutGetDelSuccessfulTest('empty Array value', 'foo', [], '') // standard String value makePutGetDelSuccessfulTest( 'long String value' , 'foo' , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!' ) // standard Buffer value makePutGetDelSuccessfulTest('Buffer value', 'foo', testBuffer) // non-empty Array as a key makePutGetDelSuccessfulTest('Array key', [1,2,3,4], 'foo') } /**** CLEANUP ENVIRONMENT ****/ module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, testFunc, testCommon, buffer, BufferType) { testBuffer = buffer test = testFunc module.exports.setUp(leveldown, test, testCommon) module.exports.errorKeys(test, BufferType) module.exports.nonErrorKeys(test) module.exports.errorValues(test, BufferType) module.exports.nonErrorValues(test, BufferType) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/put-test.js000066400000000000000000000057501260647433400222070ustar00rootroot00000000000000var db , verifyNotFoundError = require('./util').verifyNotFoundError , isTypedArray = require('./util').isTypedArray module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.args = function (test) { test('test argument-less put() throws', function (t) { t.throws( db.put.bind(db) , { name: 'Error', message: 'put() requires a callback argument' } , 'no-arg put() throws' ) t.end() }) test('test callback-less, 1-arg, put() throws', function (t) { t.throws( db.put.bind(db, 'foo') , { name: 'Error', message: 'put() requires a callback argument' } , 'callback-less, 1-arg put() throws' ) t.end() }) test('test callback-less, 2-arg, put() throws', function (t) { t.throws( db.put.bind(db, 'foo', 'bar') , { name: 'Error', message: 'put() requires a callback argument' } , 'callback-less, 2-arg put() throws' ) t.end() }) test('test callback-less, 3-arg, put() throws', function (t) { t.throws( db.put.bind(db, 'foo', 'bar', {}) , { name: 'Error', message: 'put() requires a callback argument' } , 'callback-less, 3-arg put() throws' ) t.end() }) } module.exports.put = function (test) { test('test simple put()', function (t) { db.put('foo', 'bar', function (err) { t.error(err) db.get('foo', function (err, value) { t.error(err) var result = value.toString() if (isTypedArray(value)) result = String.fromCharCode.apply(null, new Uint16Array(value)) t.equal(result, 'bar') t.end() }) }) }) if (process.browser) { test('test object value put()', function (t) { db.put('dood', {pete: 'sampras'}, function (err) { t.error(err) db.get('dood', { asBuffer: false }, function (err, value) { t.error(err) t.equal(JSON.stringify(value), JSON.stringify({pete: 'sampras'})) t.end() }) }) }) } } module.exports.sync = function (test) { test('sync put', function (t) { db.put('foo', 'bar', { sync: true }, function (err) { t.error(err) db.get('foo', function (err, value) { t.error(err) t.equal(value.toString(), 'bar') t.end() }) }) }) test('sync put just before close', function (t) { t.plan(2) db.put('foo', 'bar', { sync: true }, function (err) { t.error(err) }) db.close(function (err) { t.error(err) }) }) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.args(test) module.exports.put(test) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/ranges-test.js000066400000000000000000000377771260647433400226740ustar00rootroot00000000000000var db , sourceData = require('./iterator-test').sourceData , transformSource = require('./iterator-test').transformSource module.exports.setUp = function (leveldown, test, testCommon) { test('setUp common', testCommon.setUp) test('setUp db', function (t) { db = leveldown(testCommon.location()) db.open(t.end.bind(t)) }) } module.exports.iterator = function (leveldown, test, testCommon, collectEntries) { test('test simple iterator()', function (t) { var data = [ { type: 'put', key: 'foobatch1', value: 'bar1' } , { type: 'put', key: 'foobatch2', value: 'bar2' } , { type: 'put', key: 'foobatch3', value: 'bar3' } ] , idx = 0 db.batch(data, function (err) { t.error(err) var iterator = db.iterator() , fn = function (err, key, value) { t.error(err) if (key && value) { t.equal(key.toString(), data[idx].key, 'correct key') t.equal(value.toString(), data[idx].value, 'correct value') process.nextTick(next) idx++ } else { // end t.ok(typeof err === 'undefined', 'err argument is undefined') t.ok(typeof key === 'undefined', 'key argument is undefined') t.ok(typeof value === 'undefined', 'value argument is undefined') t.equal(idx, data.length, 'correct number of entries') iterator.end(function () { t.end() }) } } , next = function () { iterator.next(fn) } next() }) }) /** the following tests are mirroring the same series of tests in * LevelUP read-stream-test.js */ test('setUp #2', function (t) { db.close(function () { db = leveldown(testCommon.location()) db.open(function () { db.batch(sourceData, t.end.bind(t)) }) }) }) test('test full data collection', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, reverse: true }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.slice().reverse().map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with gte=0', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '00' }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with gte=50', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '50' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with lte=50 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice().reverse().slice(49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key (49.5)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '49.5' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key (49999)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '49999' }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '49.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice().reverse().slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '49.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice().reverse().slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '50', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 50, 'correct number of entries') var expected = sourceData.slice().reverse().slice(50).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end=50', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key (50.5)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50.5' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key (50555)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50555' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key (50555)', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '50555' }), function (err, data) { t.error(err) t.equal(data.length, 51, 'correct number of entries') var expected = sourceData.slice(0, 51).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end being a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '50.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 49, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with gt a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '50.5', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 49, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with gt a midway key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '50', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 49, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with gt 50 key and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '50', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 49, 'correct number of entries') var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) t.deepEqual(data, expected) t.end() }) }) // end='0', starting key is actually '00' so it should avoid it test('test iterator with end=0', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '0' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) // end='0', starting key is actually '00' so it should avoid it test('test iterator with end<0', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '0' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with start=30 and end=70', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '30', lte: '70' }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice(30, 71).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=30 and end=70', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '29', lt: '71' }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice(30, 71).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=30 and end=70 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '70', gte: '30', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start=30 and end=70 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '71', gt: '29', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 41, 'correct number of entries') var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20 and start=20', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '20', limit: 20 }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(20, 40).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with limit=20 and start=79 and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '79', limit: 20, reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice().reverse().slice(20, 40).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end after limit', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, lte: '50' }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with end before limit', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 50, lte: '19' }), function (err, data) { t.error(err) t.equal(data.length, 20, 'correct number of entries') var expected = sourceData.slice(0, 20).map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start after database end', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '9a' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with start after database end', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '9a' }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with start after database end and reverse=true', function (t) { collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '9a', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, sourceData.length, 'correct number of entries') var expected = sourceData.slice().reverse().map(transformSource) t.deepEqual(data, expected) t.end() }) }) test('test iterator with start and end after database and and reverse=true', function (t) { collectEntries(db.iterator({ lte: '9b', gte: '9a', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) test('test iterator with lt and gt after database and and reverse=true', function (t) { collectEntries(db.iterator({ lt: '9b', gt: '9a', reverse: true }), function (err, data) { t.error(err) t.equal(data.length, 0, 'correct number of entries') t.end() }) }) function testIteratorCollectsFullDatabase (name, iteratorOptions) { iteratorOptions.keyAsBuffer = false iteratorOptions.valueAsBuffer = false test(name, function (t) { collectEntries(db.iterator(iteratorOptions), function (err, data) { t.error(err) t.equal(data.length, 100, 'correct number of entries') var expected = sourceData.map(transformSource) t.deepEqual(data, expected) t.end() }) }) } if (!process.browser) { // Can't use buffers as query keys in indexeddb (I think :P) testIteratorCollectsFullDatabase( 'test iterator with start as empty buffer' , { start: new Buffer(0) } ) testIteratorCollectsFullDatabase( 'test iterator with end as empty buffer' , { end: new Buffer(0) } ) } testIteratorCollectsFullDatabase( 'test iterator with start as empty string' , { gte: '' } ) testIteratorCollectsFullDatabase( 'test iterator with start as null' , { gte: null } ) testIteratorCollectsFullDatabase( 'test iterator with end as empty string' , { lte: '' } ) testIteratorCollectsFullDatabase( 'test iterator with end as null' , { lte: null } ) } module.exports.tearDown = function (test, testCommon) { test('tearDown', function (t) { db.close(testCommon.tearDown.bind(null, t)) }) } module.exports.all = function (leveldown, test, testCommon) { module.exports.setUp(leveldown, test, testCommon) module.exports.iterator(leveldown, test, testCommon, testCommon.collectEntries) module.exports.tearDown(test, testCommon) } node-abstract-leveldown-2.4.1/abstract/util.js000066400000000000000000000005641260647433400213750ustar00rootroot00000000000000var nfre = /NotFound/i module.exports.verifyNotFoundError = function verifyNotFoundError (err) { return nfre.test(err.message) || nfre.test(err.name) } module.exports.isTypedArray = function isTypedArray (value) { return (typeof ArrayBuffer != 'undefined' && value instanceof ArrayBuffer) || (typeof Uint8Array != 'undefined' && value instanceof Uint8Array) } node-abstract-leveldown-2.4.1/index.js000066400000000000000000000003711260647433400177200ustar00rootroot00000000000000exports.AbstractLevelDOWN = require('./abstract-leveldown') exports.AbstractIterator = require('./abstract-iterator') exports.AbstractChainedBatch = require('./abstract-chained-batch') exports.isLevelDOWN = require('./is-leveldown') node-abstract-leveldown-2.4.1/is-leveldown.js000066400000000000000000000006651260647433400212270ustar00rootroot00000000000000var AbstractLevelDOWN = require('./abstract-leveldown') function isLevelDOWN (db) { if (!db || typeof db !== 'object') return false return Object.keys(AbstractLevelDOWN.prototype).filter(function (name) { // TODO remove approximateSize check when method is gone return name[0] != '_' && name != 'approximateSize' }).every(function (name) { return typeof db[name] == 'function' }) } module.exports = isLevelDOWN node-abstract-leveldown-2.4.1/package.json000066400000000000000000000032041260647433400205370ustar00rootroot00000000000000{ "name": "abstract-leveldown", "description": "An abstract prototype matching the LevelDOWN API", "version": "2.4.1", "contributors": [ "Rod Vagg (https://github.com/rvagg)", "John Chesley (https://github.com/chesles/)", "Jake Verbaten (https://github.com/raynos)", "Dominic Tarr (https://github.com/dominictarr)", "Max Ogden (https://github.com/maxogden)", "Lars-Magnus Skog (https://github.com/ralphtheninja)", "David Björklund (https://github.com/kesla)", "Julian Gruber (https://github.com/juliangruber)", "Paolo Fragomeni (https://github.com/hij1nx)", "Anton Whalley (https://github.com/No9)", "Matteo Collina (https://github.com/mcollina)", "Pedro Teixeira (https://github.com/pgte)", "James Halliday (https://github.com/substack)", "Thomas Watson Steen (https://github.com/watson)" ], "repository": { "type": "git", "url": "https://github.com/level/abstract-leveldown.git" }, "homepage": "https://github.com/level/abstract-leveldown", "keywords": [ "leveldb", "leveldown", "levelup" ], "main": "index.js", "dependencies": { "xtend": "~4.0.0" }, "devDependencies": { "rimraf": "~2.3.2", "sinon": "~1.14.1", "tape": "~4.0.0" }, "browser": { "rimraf": false }, "scripts": { "test": "node test.js" }, "license": "MIT" } node-abstract-leveldown-2.4.1/test.js000066400000000000000000000523061260647433400175750ustar00rootroot00000000000000var test = require('tape') , sinon = require('sinon') , util = require('util') , testCommon = require('./testCommon') , AbstractLevelDOWN = require('./').AbstractLevelDOWN , AbstractIterator = require('./').AbstractIterator , AbstractChainedBatch = require('./').AbstractChainedBatch , isLevelDOWN = require('./').isLevelDOWN function factory (location) { return new AbstractLevelDOWN(location) } /*** compatibility with basic LevelDOWN API ***/ require('./abstract/leveldown-test').args(factory, test, testCommon) require('./abstract/open-test').args(factory, test, testCommon) require('./abstract/del-test').setUp(factory, test, testCommon) require('./abstract/del-test').args(test) require('./abstract/get-test').setUp(factory, test, testCommon) require('./abstract/get-test').args(test) require('./abstract/put-test').setUp(factory, test, testCommon) require('./abstract/put-test').args(test) require('./abstract/put-get-del-test').setUp(factory, test, testCommon) require('./abstract/put-get-del-test').errorKeys(test) //require('./abstract/put-get-del-test').nonErrorKeys(test, testCommon) require('./abstract/put-get-del-test').errorValues(test) //require('./abstract/test/put-get-del-test').nonErrorKeys(test, testCommon) require('./abstract/put-get-del-test').tearDown(test, testCommon) require('./abstract/approximate-size-test').setUp(factory, test, testCommon) require('./abstract/approximate-size-test').args(test) require('./abstract/batch-test').setUp(factory, test, testCommon) require('./abstract/batch-test').args(test) require('./abstract/chained-batch-test').setUp(factory, test, testCommon) require('./abstract/chained-batch-test').args(test) require('./abstract/close-test').close(factory, test, testCommon) require('./abstract/iterator-test').setUp(factory, test, testCommon) require('./abstract/iterator-test').args(test) require('./abstract/iterator-test').sequence(test) /*** extensibility ***/ test('test core extensibility', function (t) { function Test (location) { AbstractLevelDOWN.call(this, location) t.equal(this.location, location, 'location set on `this`') t.end() } util.inherits(Test, AbstractLevelDOWN) ;new Test('foobar') }) test('test open() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { createIfMissing: true, errorIfExists: false } , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._open = spy test = new Test('foobar') test.open(expectedCb) t.equal(spy.callCount, 1, 'got _open() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _open() was correct') t.equal(spy.getCall(0).args.length, 2, 'got two arguments') t.deepEqual(spy.getCall(0).args[0], expectedOptions, 'got default options argument') test.open({ options: 1 }, expectedCb) expectedOptions.options = 1 t.equal(spy.callCount, 2, 'got _open() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _open() was correct') t.equal(spy.getCall(1).args.length, 2, 'got two arguments') t.deepEqual(spy.getCall(1).args[0], expectedOptions, 'got expected options argument') t.end() }) test('test close() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._close = spy test = new Test('foobar') test.close(expectedCb) t.equal(spy.callCount, 1, 'got _close() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _close() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one arguments') t.end() }) test('test get() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { asBuffer: true } , expectedKey = 'a key' , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._get = spy test = new Test('foobar') test.get(expectedKey, expectedCb) t.equal(spy.callCount, 1, 'got _get() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _get() was correct') t.equal(spy.getCall(0).args.length, 3, 'got three arguments') t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') t.deepEqual(spy.getCall(0).args[1], expectedOptions, 'got default options argument') t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument') test.get(expectedKey, { options: 1 }, expectedCb) expectedOptions.options = 1 t.equal(spy.callCount, 2, 'got _get() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _get() was correct') t.equal(spy.getCall(1).args.length, 3, 'got three arguments') t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument') t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') t.equal(spy.getCall(1).args[2], expectedCb, 'got expected cb argument') t.end() }) test('test del() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { options: 1 } , expectedKey = 'a key' , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._del = spy test = new Test('foobar') test.del(expectedKey, expectedCb) t.equal(spy.callCount, 1, 'got _del() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _del() was correct') t.equal(spy.getCall(0).args.length, 3, 'got three arguments') t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') t.deepEqual(spy.getCall(0).args[1], {}, 'got blank options argument') t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument') test.del(expectedKey, expectedOptions, expectedCb) t.equal(spy.callCount, 2, 'got _del() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _del() was correct') t.equal(spy.getCall(1).args.length, 3, 'got three arguments') t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument') t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') t.equal(spy.getCall(1).args[2], expectedCb, 'got expected cb argument') t.end() }) test('test put() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { options: 1 } , expectedKey = 'a key' , expectedValue = 'a value' , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._put = spy test = new Test('foobar') test.put(expectedKey, expectedValue, expectedCb) t.equal(spy.callCount, 1, 'got _put() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _put() was correct') t.equal(spy.getCall(0).args.length, 4, 'got four arguments') t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') t.equal(spy.getCall(0).args[1], expectedValue, 'got expected value argument') t.deepEqual(spy.getCall(0).args[2], {}, 'got blank options argument') t.equal(spy.getCall(0).args[3], expectedCb, 'got expected cb argument') test.put(expectedKey, expectedValue, expectedOptions, expectedCb) t.equal(spy.callCount, 2, 'got _put() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _put() was correct') t.equal(spy.getCall(1).args.length, 4, 'got four arguments') t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument') t.equal(spy.getCall(1).args[1], expectedValue, 'got expected value argument') t.deepEqual(spy.getCall(1).args[2], expectedOptions, 'got blank options argument') t.equal(spy.getCall(1).args[3], expectedCb, 'got expected cb argument') t.end() }) test('test approximateSize() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedStart = 'a start' , expectedEnd = 'an end' , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._approximateSize = spy test = new Test('foobar') test.approximateSize(expectedStart, expectedEnd, expectedCb) t.equal(spy.callCount, 1, 'got _approximateSize() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _approximateSize() was correct') t.equal(spy.getCall(0).args.length, 3, 'got three arguments') t.equal(spy.getCall(0).args[0], expectedStart, 'got expected start argument') t.equal(spy.getCall(0).args[1], expectedEnd, 'got expected end argument') t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument') t.end() }) test('test batch() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { options: 1 } , expectedArray = [ 1, 2 ] , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._batch = spy test = new Test('foobar') test.batch(expectedArray, expectedCb) t.equal(spy.callCount, 1, 'got _batch() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _batch() was correct') t.equal(spy.getCall(0).args.length, 3, 'got three arguments') t.equal(spy.getCall(0).args[0], expectedArray, 'got expected array argument') t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument') t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument') test.batch(expectedArray, expectedOptions, expectedCb) t.equal(spy.callCount, 2, 'got _batch() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _batch() was correct') t.equal(spy.getCall(1).args.length, 3, 'got three arguments') t.equal(spy.getCall(1).args[0], expectedArray, 'got expected array argument') t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument') test.batch(expectedArray, null, expectedCb) t.equal(spy.callCount, 3, 'got _batch() call') t.equal(spy.getCall(2).thisValue, test, '`this` on _batch() was correct') t.equal(spy.getCall(2).args.length, 3, 'got three arguments') t.equal(spy.getCall(2).args[0], expectedArray, 'got expected array argument') t.ok(spy.getCall(2).args[1], 'options should not be null') t.equal(spy.getCall(2).args[2], expectedCb, 'got expected callback argument') t.end() }) test('test chained batch() (array) extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , expectedOptions = { options: 1 } , expectedArray = [ 1, 2 ] , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._batch = spy test = new Test('foobar') test.batch().put('foo', 'bar').del('bang').write(expectedCb) t.equal(spy.callCount, 1, 'got _batch() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _batch() was correct') t.equal(spy.getCall(0).args.length, 3, 'got three arguments') t.equal(spy.getCall(0).args[0].length, 2, 'got expected array argument') t.deepEqual(spy.getCall(0).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]') t.deepEqual(spy.getCall(0).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]') t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument') t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument') test.batch().put('foo', 'bar').del('bang').write(expectedOptions, expectedCb) t.equal(spy.callCount, 2, 'got _batch() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _batch() was correct') t.equal(spy.getCall(1).args.length, 3, 'got three arguments') t.equal(spy.getCall(1).args[0].length, 2, 'got expected array argument') t.deepEqual(spy.getCall(1).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]') t.deepEqual(spy.getCall(1).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]') t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument') t.end() }) test('test chained batch() (custom _chainedBatch) extensibility', function (t) { var spy = sinon.spy() , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._chainedBatch = spy test = new Test('foobar') test.batch() t.equal(spy.callCount, 1, 'got _chainedBatch() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _chainedBatch() was correct') test.batch() t.equal(spy.callCount, 2, 'got _chainedBatch() call') t.equal(spy.getCall(1).thisValue, test, '`this` on _chainedBatch() was correct') t.end() }) test('test AbstractChainedBatch extensibility', function (t) { function Test (db) { AbstractChainedBatch.call(this, db) t.equal(this._db, db, 'db set on `this`') t.end() } util.inherits(Test, AbstractChainedBatch) new Test('foobar') }) test('test write() extensibility', function (t) { var spy = sinon.spy() , spycb = sinon.spy() , test function Test (db) { AbstractChainedBatch.call(this, db) } util.inherits(Test, AbstractChainedBatch) Test.prototype._write = spy test = new Test('foobar') test.write(spycb) t.equal(spy.callCount, 1, 'got _write() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _write() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one argument') // awkward here cause of nextTick & an internal wrapped cb t.equal(typeof spy.getCall(0).args[0], 'function', 'got a callback function') t.equal(spycb.callCount, 0, 'spycb not called') spy.getCall(0).args[0]() t.equal(spycb.callCount, 1, 'spycb called, i.e. was our cb argument') t.end() }) test('test put() extensibility', function (t) { var spy = sinon.spy() , expectedKey = 'key' , expectedValue = 'value' , returnValue , test function Test (db) { AbstractChainedBatch.call(this, db) } util.inherits(Test, AbstractChainedBatch) Test.prototype._put = spy test = new Test(factory('foobar')) returnValue = test.put(expectedKey, expectedValue) t.equal(spy.callCount, 1, 'got _put call') t.equal(spy.getCall(0).thisValue, test, '`this` on _put() was correct') t.equal(spy.getCall(0).args.length, 2, 'got two arguments') t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') t.equal(spy.getCall(0).args[1], expectedValue, 'got expected value argument') t.equal(returnValue, test, 'get expected return value') t.end() }) test('test del() extensibility', function (t) { var spy = sinon.spy() , expectedKey = 'key' , returnValue , test function Test (db) { AbstractChainedBatch.call(this, db) } util.inherits(Test, AbstractChainedBatch) Test.prototype._del = spy test = new Test(factory('foobar')) returnValue = test.del(expectedKey) t.equal(spy.callCount, 1, 'got _del call') t.equal(spy.getCall(0).thisValue, test, '`this` on _del() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one argument') t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') t.equal(returnValue, test, 'get expected return value') t.end() }) test('test clear() extensibility', function (t) { var spy = sinon.spy() , returnValue , test function Test (db) { AbstractChainedBatch.call(this, db) } util.inherits(Test, AbstractChainedBatch) Test.prototype._clear = spy test = new Test(factory('foobar')) returnValue = test.clear() t.equal(spy.callCount, 1, 'got _clear call') t.equal(spy.getCall(0).thisValue, test, '`this` on _clear() was correct') t.equal(spy.getCall(0).args.length, 0, 'got zero arguments') t.equal(returnValue, test, 'get expected return value') t.end() }) test('test iterator() extensibility', function (t) { var spy = sinon.spy() , expectedOptions = { options: 1, reverse: false, keys: true, values: true, limit: -1, keyAsBuffer: true, valueAsBuffer: true } , test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._iterator = spy test = new Test('foobar') test.iterator({ options: 1 }) t.equal(spy.callCount, 1, 'got _close() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _close() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one arguments') t.deepEqual(spy.getCall(0).args[0], expectedOptions, 'got expected options argument') t.end() }) test('test AbstractIterator extensibility', function (t) { function Test (db) { AbstractIterator.call(this, db) t.equal(this.db, db, 'db set on `this`') t.end() } util.inherits(Test, AbstractIterator) ;new Test('foobar') }) test('test next() extensibility', function (t) { var spy = sinon.spy() , spycb = sinon.spy() , test function Test (db) { AbstractIterator.call(this, db) } util.inherits(Test, AbstractIterator) Test.prototype._next = spy test = new Test('foobar') test.next(spycb) t.equal(spy.callCount, 1, 'got _next() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _next() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one arguments') // awkward here cause of nextTick & an internal wrapped cb t.equal(typeof spy.getCall(0).args[0], 'function', 'got a callback function') t.equal(spycb.callCount, 0, 'spycb not called') spy.getCall(0).args[0]() t.equal(spycb.callCount, 1, 'spycb called, i.e. was our cb argument') t.end() }) test('test end() extensibility', function (t) { var spy = sinon.spy() , expectedCb = function () {} , test function Test (db) { AbstractIterator.call(this, db) } util.inherits(Test, AbstractIterator) Test.prototype._end = spy test = new Test('foobar') test.end(expectedCb) t.equal(spy.callCount, 1, 'got _end() call') t.equal(spy.getCall(0).thisValue, test, '`this` on _end() was correct') t.equal(spy.getCall(0).args.length, 1, 'got one arguments') t.equal(spy.getCall(0).args[0], expectedCb, 'got expected cb argument') t.end() }) test('isLevelDOWN', function (t) { t.notOk(isLevelDOWN(), 'is not a leveldown') t.notOk(isLevelDOWN(''), 'is not a leveldown') t.notOk(isLevelDOWN({}), 'is not a leveldown') t.notOk(isLevelDOWN({ put: function () {} }), 'is not a leveldown') t.ok(isLevelDOWN(new AbstractLevelDOWN('location')), 'IS a leveldown') t.ok(isLevelDOWN({ open: function () {}, close: function () {}, get: function () {}, put: function () {}, del: function () {}, batch: function () {}, iterator: function () {} }), 'IS a leveldown') t.ok(isLevelDOWN({ open: function () {}, close: function () {}, get: function () {}, put: function () {}, del: function () {}, batch: function () {}, approximateSize: function () {}, iterator: function () {} }), 'IS also a leveldown') t.end() }) test('.status', function (t) { t.test('empty prototype', function (t) { var test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) test = new Test('foobar') t.equal(test.status, 'new') test.open(function (err) { t.error(err) t.equal(test.status, 'open') test.close(function (err) { t.error(err) t.equal(test.status, 'closed') t.end() }) }) }) t.test('open error', function (t) { var test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._open = function (options, cb) { cb(new Error) } test = new Test('foobar') test.open(function (err) { t.ok(err) t.equal(test.status, 'new') t.end() }) }) t.test('close error', function (t) { var test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._close = function (cb) { cb(new Error) } test = new Test('foobar') test.open(function () { test.close(function (err) { t.ok(err) t.equal(test.status, 'open') t.end() }) }) }) t.test('open', function (t) { var test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._open = function (options, cb) { process.nextTick(cb) } test = new Test('foobar') test.open(function (err) { t.error(err) t.equal(test.status, 'open') t.end() }) t.equal(test.status, 'opening') }) t.test('close', function (t) { var test function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) Test.prototype._close = function (cb) { process.nextTick(cb) } test = new Test('foobar') test.open(function (err) { t.error(err) test.close(function (err) { t.error(err) t.equal(test.status, 'closed') t.end() }) t.equal(test.status, 'closing') }) }) }) node-abstract-leveldown-2.4.1/testCommon.js000066400000000000000000000034531260647433400207450ustar00rootroot00000000000000var path = require('path') , fs = !process.browser && require('fs') , rimraf = !process.browser && require('rimraf') var dbidx = 0 , location = function () { return path.join(__dirname, '_leveldown_test_db_' + dbidx++) } , lastLocation = function () { return path.join(__dirname, '_leveldown_test_db_' + dbidx) } , cleanup = function (callback) { if (process.browser) return callback() fs.readdir(__dirname, function (err, list) { if (err) return callback(err) list = list.filter(function (f) { return (/^_leveldown_test_db_/).test(f) }) if (!list.length) return callback() var ret = 0 list.forEach(function (f) { rimraf(path.join(__dirname, f), function (err) { if (++ret == list.length) callback() }) }) }) } , setUp = function (t) { cleanup(function (err) { t.error(err, 'cleanup returned an error') t.end() }) } , tearDown = function (t) { setUp(t) // same cleanup! } , collectEntries = function (iterator, callback) { var data = [] , next = function () { iterator.next(function (err, key, value) { if (err) return callback(err) if (!arguments.length) { return iterator.end(function (err) { callback(err, data) }) } data.push({ key: key, value: value }) setTimeout(next, 0) }) } next() } module.exports = { location : location , cleanup : cleanup , lastLocation : lastLocation , setUp : setUp , tearDown : tearDown , collectEntries : collectEntries }