From 52049db948bfdbc51675ec9652f2df0999cbfa76 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 26 Jan 2025 16:26:53 +0100 Subject: [PATCH] Test seeking outside of range options Closes a code coverage gap in memory-level. Category: fix --- test/iterator-seek-test.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/iterator-seek-test.js b/test/iterator-seek-test.js index 3bb18cf..e146b30 100644 --- a/test/iterator-seek-test.js +++ b/test/iterator-seek-test.js @@ -293,5 +293,43 @@ exports.seek = function (test, testCommon) { }) } }) + + // Tests the specific case where an iterator can (theoretically) tell that + // a seek() would be out of range by comparing the seek target against + // range options, before performing an actual seek. MemoryLevel works this + // way for example. Also test the same scenario without an explicit seek() + // which should have the same result. + for (const reverse of [false, true]) { + for (const seek of [true, false]) { + const props = `reverse = ${reverse}, seek = ${seek}` + const name = `${mode}() seek outside of range options (${props})` + const key = 'a' + + test(name, async function (t) { + const db = testCommon.factory() + + await db.open() + await db.put(key, '123') + + // Pick ranges that exclude the key + const ranges = [ + { gt: 'x', reverse }, + { gte: 'x', reverse }, + { lt: '0', reverse }, + { lte: '0', reverse } + ] + + // Test each range + for (let i = 0; i < ranges.length; i++) { + const iterator = db[mode](ranges[i]) + if (seek) iterator.seek(key) + t.same(await iterator.next(), undefined, `end of iterator ${i}`) + await iterator.close() + } + + return db.close() + }) + } + } } }