Skip to content

Commit

Permalink
moved seek tests to abstract-leveldown
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Oct 19, 2018
1 parent 1fd0bdb commit 23222b5
Showing 1 changed file with 4 additions and 204 deletions.
208 changes: 4 additions & 204 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const make = require('./make')
const iota = require('iota-array')
const lexi = require('lexicographic-integer')
const util = require('util')
// const iota = require('iota-array') // TODO: remove if unused
// const lexi = require('lexicographic-integer') // TODO: remove if unused

// 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 +28,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 +66,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 +83,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 }
}

0 comments on commit 23222b5

Please sign in to comment.