[Pkg-javascript-commits] [node-leveldown] 232/492: added uber test suite for iterator()
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:02 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit 265bed5f95af0e485c70d5ecb8e516b7e95737b5
Author: Rod Vagg <rod at vagg.org>
Date: Sat Feb 23 22:46:09 2013 +1100
added uber test suite for iterator()
copied the logic from LevelUP read-stream-test.js
---
src/database.cc | 4 +-
src/iterator.cc | 7 +-
test/iterator-test.js | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 261 insertions(+), 4 deletions(-)
diff --git a/src/database.cc b/src/database.cc
index 872a1e6..86835e8 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -403,8 +403,8 @@ v8::Handle<v8::Value> Database::Iterator (const v8::Arguments& args) {
v8::HandleScope scope;
v8::Local<v8::Object> optionsObj;
- if (args.Length() > 0 && args[1]->IsObject()) {
- optionsObj = v8::Local<v8::Object>::Cast(args[1]) ;
+ if (args.Length() > 0 && args[0]->IsObject()) {
+ optionsObj = v8::Local<v8::Object>::Cast(args[0]);
}
return scope.Close(Iterator::NewInstance(args.This(), optionsObj));
diff --git a/src/iterator.cc b/src/iterator.cc
index e132367..838d3b6 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -48,6 +48,8 @@ Iterator::Iterator (
Iterator::~Iterator () {
delete options;
startPtr.Dispose();
+ if (start != NULL)
+ delete start;
if (end != NULL)
delete end;
};
@@ -223,7 +225,7 @@ v8::Handle<v8::Value> Iterator::New (const v8::Arguments& args) {
v8::Local<v8::Object> optionsObj;
- if (args.Length() > 0 && args[1]->IsObject()) {
+ if (args.Length() > 1 && args[1]->IsObject()) {
optionsObj = v8::Local<v8::Object>::Cast(args[1]);
if (optionsObj->Has(option_start)
@@ -231,7 +233,8 @@ v8::Handle<v8::Value> Iterator::New (const v8::Arguments& args) {
|| optionsObj->Get(option_start)->IsString())) {
startBuffer = v8::Local<v8::Value>::New(optionsObj->Get(option_start));
- STRING_OR_BUFFER_TO_SLICE(start, startBuffer, Start)
+ STRING_OR_BUFFER_TO_SLICE(_start, startBuffer, Start)
+ start = new leveldb::Slice(_start.data(), _start.size());
}
if (optionsObj->Has(option_end)
diff --git a/test/iterator-test.js b/test/iterator-test.js
index 87925e4..33fa0f9 100644
--- a/test/iterator-test.js
+++ b/test/iterator-test.js
@@ -3,6 +3,38 @@ const test = require('tap').test
, leveldown = require('../')
var 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: d.value } }
+ , 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)
+ })
+ }
+ if (key == +key) key = +key
+ data.push({ key: key, value: value })
+ process.nextTick(next)
+ })
+ }
+ next()
+ }
test('setUp', function (t) {
db = leveldown(testCommon.location())
@@ -87,6 +119,228 @@ test('test simple iterator()', function (t) {
})
})
+/** 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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 end=50', function (t) {
+ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50' }), function (err, data) {
+ t.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ 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, end: '50' }), function (err, data) {
+ t.notOk(err, 'no error')
+ 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.notOk(err, 'no error')
+ t.equal(data.length, 20, 'correct number of entries')
+ var expected = sourceData.slice(0, 20).map(transformSource)
+ t.deepEqual(data, expected)
+ t.end()
+ })
+})
+
test('tearDown', function (t) {
db.close(testCommon.tearDown.bind(null, t))
})
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list