Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate seek tests to abstract-leveldown #508

Merged
merged 3 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"main": "leveldown.js",
"dependencies": {
"abstract-leveldown": "github:Level/abstract-leveldown#0f42686",
"abstract-leveldown": "github:Level/abstract-leveldown#b610da0",
"bindings": "~1.3.0",
"fast-future": "~1.0.2",
"nan": "~2.11.0",
Expand All @@ -24,9 +24,7 @@
"coveralls": "^3.0.2",
"delayed": "~1.0.1",
"du": "~0.1.0",
"iota-array": "~1.0.0",
"level-concat-iterator": "^2.0.0",
"lexicographic-integer": "~1.1.0",
"mkfiletree": "~1.0.1",
"monotonic-timestamp": "~0.0.8",
"nyc": "^12.0.2",
Expand Down
206 changes: 2 additions & 204 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const make = require('./make')
const iota = require('iota-array')
const lexi = require('lexicographic-integer')
const util = require('util')

// This test isn't included in abstract-leveldown because
// the empty-check is currently performed by leveldown.
make('iterator#seek throws if target is empty', function (db, t, done) {
var targets = ['', Buffer.alloc(0), []]
var pending = targets.length
Expand All @@ -27,71 +26,6 @@ make('iterator#seek throws if target is empty', function (db, t, done) {
}
})

make('iterator is seekable', function (db, t, done) {
var ite = db.iterator()
ite.seek('two')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key.toString(), 'two', 'key matches')
t.same(value.toString(), '2', 'value matches')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key, undefined, 'end of iterator')
t.same(value, undefined, 'end of iterator')
ite.end(done)
})
})
})

make('iterator is seekable with buffer', function (db, t, done) {
var ite = db.iterator()
ite.seek(Buffer.from('two'))
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key.toString(), 'two', 'key matches')
t.equal(value.toString(), '2', 'value matches')
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
t.equal(key, undefined, 'end of iterator')
t.equal(value, undefined, 'end of iterator')
ite.end(done)
})
})
})

make('reverse seek in the middle', function (db, t, done) {
var ite = db.iterator({ reverse: true, limit: 1 })
ite.seek('three!')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key.toString(), 'three', 'key matches')
t.same(value.toString(), '3', 'value matches')
ite.end(done)
})
})

make('iterator invalid seek', function (db, t, done) {
var ite = db.iterator()
ite.seek('zzz')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key, undefined, 'end of iterator')
t.same(value, undefined, 'end of iterator')
ite.end(done)
})
})

make('reverse seek from invalid range', function (db, t, done) {
var ite = db.iterator({ reverse: true })
ite.seek('zzz')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key.toString(), 'two')
t.same(value.toString(), '2')
ite.end(done)
})
})

make('iterator optimized for seek', function (db, t, done) {
var batch = db.batch()
batch.put('a', 1)
Expand Down Expand Up @@ -130,24 +64,6 @@ make('iterator optimized for seek', function (db, t, done) {
})
})

make('iterator seek before next has completed', function (db, t, done) {
var ite = db.iterator()
var error

ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
ite.end(done)
})

try {
ite.seek('two')
} catch (err) {
error = err.message
}

t.is(error, 'cannot call seek() before next() has completed', 'got error')
})

make('close db with open iterator', function (db, t, done) {
var ite = db.iterator()
var cnt = 0
Expand All @@ -165,121 +81,3 @@ make('close db with open iterator', function (db, t, done) {
done(false)
})
})

make('iterator seek after end', function (db, t, done) {
var ite = db.iterator()
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
ite.end(function (err) {
t.error(err, 'no error from end()')
var error

try {
ite.seek('two')
} catch (err) {
error = err.message
}

t.is(error, 'cannot call seek() after end()', 'got error')
done()
})
})
})

make('iterator seek respects range', function (db, t, done) {
db.batch(pairs(10), function (err) {
t.error(err, 'no error from batch()')

var pending = 0

expect({ gt: '5' }, '4', undefined)
expect({ gt: '5' }, '5', undefined)
expect({ gt: '5' }, '6', '6')

expect({ gte: '5' }, '4', undefined)
expect({ gte: '5' }, '5', '5')
expect({ gte: '5' }, '6', '6')

expect({ start: '5' }, '4', undefined)
expect({ start: '5' }, '5', '5')
expect({ start: '5' }, '6', '6')

expect({ lt: '5' }, '4', '4')
expect({ lt: '5' }, '5', undefined)
expect({ lt: '5' }, '6', undefined)

expect({ lte: '5' }, '4', '4')
expect({ lte: '5' }, '5', '5')
expect({ lte: '5' }, '6', undefined)

expect({ end: '5' }, '4', '4')
expect({ end: '5' }, '5', '5')
expect({ end: '5' }, '6', undefined)

expect({ lt: '5', reverse: true }, '4', '4')
expect({ lt: '5', reverse: true }, '5', undefined)
expect({ lt: '5', reverse: true }, '6', undefined)

expect({ lte: '5', reverse: true }, '4', '4')
expect({ lte: '5', reverse: true }, '5', '5')
expect({ lte: '5', reverse: true }, '6', undefined)

expect({ start: '5', reverse: true }, '4', '4')
expect({ start: '5', reverse: true }, '5', '5')
expect({ start: '5', reverse: true }, '6', undefined)

expect({ gt: '5', reverse: true }, '4', undefined)
expect({ gt: '5', reverse: true }, '5', undefined)
expect({ gt: '5', reverse: true }, '6', '6')

expect({ gte: '5', reverse: true }, '4', undefined)
expect({ gte: '5', reverse: true }, '5', '5')
expect({ gte: '5', reverse: true }, '6', '6')

expect({ end: '5', reverse: true }, '4', undefined)
expect({ end: '5', reverse: true }, '5', '5')
expect({ end: '5', reverse: true }, '6', '6')

expect({ gt: '7', lt: '8' }, '7', undefined)
expect({ gte: '7', lt: '8' }, '7', '7')
expect({ gte: '7', lt: '8' }, '8', undefined)
expect({ gt: '7', lte: '8' }, '8', '8')

function expect (range, target, expected) {
pending++
var ite = db.iterator(range)

ite.seek(target)
ite.next(function (err, key, value) {
t.error(err, 'no error from next()')

var tpl = 'seek(%s) on %s yields %s'
var msg = util.format(tpl, target, util.inspect(range), expected)

if (expected === undefined) {
t.equal(value, undefined, msg)
} else {
t.equal(value.toString(), expected, msg)
}

ite.end(function (err) {
t.error(err, 'no error from end()')
if (!--pending) done()
})
})
}
})
})

function pairs (length, opts) {
opts = opts || {}
return iota(length).filter(not(opts.not)).map(function (k) {
var key = opts.lex ? lexi.pack(k, 'hex') : '' + k
return { type: 'put', key: key, value: '' + k }
})
}

function not (n) {
if (typeof n === 'function') return function (k) { return !n(k) }
return function (k) { return k !== n }
}