Skip to content

Commit 926978a

Browse files
authored
Address a few TODO comments in the test suite (#97)
Category: none
1 parent 6684039 commit 926978a

File tree

3 files changed

+110
-100
lines changed

3 files changed

+110
-100
lines changed

test/iterator-seek-test.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ exports.sequence = function (test, testCommon) {
4141

4242
exports.seek = function (test, testCommon) {
4343
const testData = () => [
44+
// Note that 'three' sorts before 'two'
4445
{ type: 'put', key: 'one', value: '1' },
4546
{ type: 'put', key: 'two', value: '2' },
4647
{ type: 'put', key: 'three', value: '3' }
4748
]
4849

50+
const bufferTestData = () => [
51+
// Note that 'b9' sorts before 'c0'
52+
{ type: 'put', key: Buffer.from('80', 'hex'), value: '1', keyEncoding: 'buffer' },
53+
{ type: 'put', key: Buffer.from('c0', 'hex'), value: '2', keyEncoding: 'buffer' },
54+
{ type: 'put', key: Buffer.from('b9', 'hex'), value: '3', keyEncoding: 'buffer' }
55+
]
56+
57+
test('prepare byte-aware tests', function (t) {
58+
const data = bufferTestData()
59+
t.ok(data[0].key.toString() === data[1].key.toString(), 'would be equal when not byte-aware')
60+
t.ok(data[0].key.compare(data[1].key) < 0, 'but less than when byte-aware')
61+
t.end()
62+
})
63+
4964
for (const mode of ['iterator', 'keys', 'values']) {
5065
const mapEntry = mode === 'iterator' ? e => e : mode === 'keys' ? e => e[0] : e => e[1]
5166

@@ -63,15 +78,17 @@ exports.seek = function (test, testCommon) {
6378
})
6479

6580
if (testCommon.supports.encodings.buffer) {
66-
// TODO: make this test meaningful, with bytes outside the utf8 range
6781
test(`${mode}().seek() to buffer target`, async function (t) {
82+
// For this test to be meaningful it must use bytes outside the utf8 range
83+
const data = bufferTestData()
6884
const db = testCommon.factory()
69-
await db.batch(testData())
85+
await db.batch(data)
7086
const it = db[mode]({ keyEncoding: 'buffer' })
7187

72-
it.seek(Buffer.from('two'))
88+
// Seek to second key
89+
it.seek(data[1].key)
7390

74-
t.same(await it.next(), mapEntry([Buffer.from('two'), '2']), 'match')
91+
t.same(await it.next(), mapEntry([data[1].key, '2']), 'match')
7592
t.same(await it.next(), undefined, 'end of iterator')
7693

7794
return db.close()

test/self/deferred-iterator-test.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ for (const mode of ['iterator', 'keys', 'values']) {
5555
})
5656

5757
// NOTE: adapted from deferred-leveldown
58-
test(`deferred ${mode}(): non-deferred operations`, function (t) {
59-
t.plan(4)
58+
test(`deferred ${mode}(): non-deferred operations`, async function (t) {
59+
t.plan(3)
6060

6161
class MockIterator extends RealCtor {
6262
_seek (target) {
@@ -74,17 +74,14 @@ for (const mode of ['iterator', 'keys', 'values']) {
7474
}
7575
})
7676

77-
// TODO: async/await
78-
db.open().then(function () {
79-
it.seek(123)
80-
it.next().then(function (item) {
81-
t.is(item, nextArg)
82-
it.close().then(t.pass.bind(t, 'closed'))
83-
})
84-
})
85-
8677
const it = db[publicMethod]({ gt: 'foo' })
8778
t.ok(it instanceof DeferredCtor)
79+
80+
await db.open()
81+
it.seek(123)
82+
t.is(await it.next(), nextArg)
83+
84+
return it.close()
8885
})
8986

9087
// NOTE: adapted from deferred-leveldown
@@ -127,7 +124,7 @@ for (const mode of ['iterator', 'keys', 'values']) {
127124
})
128125

129126
for (const method of ['next', 'nextv', 'all']) {
130-
test(`deferred ${mode}(): closed upon failed open, verified by ${method}()`, function (t) {
127+
test(`deferred ${mode}(): closed upon failed open, verified by ${method}()`, async function (t) {
131128
t.plan(5)
132129

133130
const db = mockLevel({
@@ -152,11 +149,11 @@ for (const mode of ['iterator', 'keys', 'values']) {
152149
return original.call(this, ...args)
153150
}
154151

155-
verifyClosed(t, it, method, () => {})
152+
return verifyClosed(t, it, method)
156153
})
157154

158-
test(`deferred ${mode}(): deferred and real iterators are closed on db.close(), verified by ${method}()`, function (t) {
159-
t.plan(8)
155+
test(`deferred ${mode}(): deferred and real iterators are closed on db.close(), verified by ${method}()`, async function (t) {
156+
t.plan(7)
160157

161158
class MockIterator extends RealCtor {
162159
async _close () {
@@ -179,17 +176,16 @@ for (const mode of ['iterator', 'keys', 'values']) {
179176
return original.call(this, ...args)
180177
}
181178

182-
// TODO: async/await
183-
db.open().then(() => db.close()).then(function () {
184-
verifyClosed(t, it, method, function () {
185-
db.open().then(function () {
186-
// Should still be closed
187-
verifyClosed(t, it, method, function () {
188-
db.close().then(t.pass.bind(t))
189-
})
190-
})
191-
})
192-
})
179+
await db.open()
180+
await db.close()
181+
182+
await verifyClosed(t, it, method)
183+
await db.open()
184+
185+
// Should still be closed
186+
await verifyClosed(t, it, method)
187+
188+
return db.close()
193189
})
194190
}
195191

@@ -298,18 +294,21 @@ for (const mode of ['iterator', 'keys', 'values']) {
298294
})
299295
})
300296

301-
// TODO: async/await
302-
const verifyClosed = function (t, it, method, cb) {
297+
const verifyClosed = async function (t, it, method) {
303298
const requiredArgs = method === 'nextv' ? [10] : []
304299

305-
it[method](...requiredArgs).then(t.fail.bind(t, 'should not succeed'), function (err) {
306-
t.is(err && err.code, 'LEVEL_ITERATOR_NOT_OPEN', `correct error on first ${method}()`)
300+
try {
301+
await it[method](...requiredArgs)
302+
t.fail('should not succeed')
303+
} catch (err) {
304+
t.is(err.code, 'LEVEL_ITERATOR_NOT_OPEN', `correct error on first ${method}()`)
305+
}
307306

308-
// Should account for userland code that ignores errors
309-
it[method](...requiredArgs).then(t.fail.bind(t, 'should not succeed'), function (err) {
310-
t.is(err && err.code, 'LEVEL_ITERATOR_NOT_OPEN', `correct error on second ${method}()`)
311-
cb()
312-
})
313-
})
307+
try {
308+
await it[method](...requiredArgs)
309+
t.fail('should not succeed')
310+
} catch (err) {
311+
t.is(err.code, 'LEVEL_ITERATOR_NOT_OPEN', `correct error on second ${method}()`)
312+
}
314313
}
315314
}

test/self/sublevel-test.js

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,25 @@ test('sublevel is extensible', function (t) {
6969
})
7070

7171
// NOTE: adapted from subleveldown
72-
test('sublevel prefix and options', function (t) {
73-
// TODO: rename "prefix" to "name" where appropriate. E.g. this test should be
74-
// called 'empty name' rather than 'empty prefix'.
75-
t.test('empty prefix', function (t) {
72+
test('sublevel name and options', function (t) {
73+
t.test('empty name', function (t) {
7674
const sub = new NoopLevel().sublevel('')
7775
t.is(sub.prefix, '!!')
7876
t.same(sub.path(), [''])
7977
t.end()
8078
})
8179

82-
t.test('prefix without options', function (t) {
83-
const sub = new NoopLevel().sublevel('prefix')
84-
t.is(sub.prefix, '!prefix!')
85-
t.same(sub.path(), ['prefix'])
80+
t.test('name without options', function (t) {
81+
const sub = new NoopLevel().sublevel('name')
82+
t.is(sub.prefix, '!name!')
83+
t.same(sub.path(), ['name'])
8684
t.end()
8785
})
8886

89-
t.test('prefix and separator option', function (t) {
90-
const sub = new NoopLevel().sublevel('prefix', { separator: '%' })
91-
t.is(sub.prefix, '%prefix%')
92-
t.same(sub.path(), ['prefix'])
87+
t.test('name and separator option', function (t) {
88+
const sub = new NoopLevel().sublevel('name', { separator: '%' })
89+
t.is(sub.prefix, '%name%')
90+
t.same(sub.path(), ['name'])
9391
t.end()
9492
})
9593

@@ -140,22 +138,22 @@ test('sublevel prefix and options', function (t) {
140138
t.end()
141139
})
142140

143-
t.test('separator is trimmed from prefix', function (t) {
144-
const sub1 = new NoopLevel().sublevel('!prefix')
145-
t.is(sub1.prefix, '!prefix!')
146-
t.same(sub1.path(), ['prefix'])
141+
t.test('separator is trimmed from name', function (t) {
142+
const sub1 = new NoopLevel().sublevel('!name')
143+
t.is(sub1.prefix, '!name!')
144+
t.same(sub1.path(), ['name'])
147145

148-
const sub2 = new NoopLevel().sublevel('prefix!')
149-
t.is(sub2.prefix, '!prefix!')
150-
t.same(sub2.path(), ['prefix'])
146+
const sub2 = new NoopLevel().sublevel('name!')
147+
t.is(sub2.prefix, '!name!')
148+
t.same(sub2.path(), ['name'])
151149

152-
const sub3 = new NoopLevel().sublevel('!!prefix!!')
153-
t.is(sub3.prefix, '!prefix!')
154-
t.same(sub3.path(), ['prefix'])
150+
const sub3 = new NoopLevel().sublevel('!!name!!')
151+
t.is(sub3.prefix, '!name!')
152+
t.same(sub3.path(), ['name'])
155153

156-
const sub4 = new NoopLevel().sublevel('@prefix@', { separator: '@' })
157-
t.is(sub4.prefix, '@prefix@')
158-
t.same(sub4.path(), ['prefix'])
154+
const sub4 = new NoopLevel().sublevel('@name@', { separator: '@' })
155+
t.is(sub4.prefix, '@name@')
156+
t.same(sub4.path(), ['name'])
159157

160158
const sub5 = new NoopLevel().sublevel(['!!!a', 'b!!!'])
161159
t.is(sub5.prefix, '!a!!b!')
@@ -615,7 +613,7 @@ test('sublevel operations are prefixed', function (t) {
615613

616614
test('sublevel encodings', function (t) {
617615
// NOTE: adapted from subleveldown
618-
t.test('different sublevels can have different encodings', function (t) {
616+
t.test('different sublevels can have different encodings', async function (t) {
619617
t.plan(6)
620618

621619
const puts = []
@@ -636,41 +634,37 @@ test('sublevel encodings', function (t) {
636634
const sub1 = db.sublevel('test1', { valueEncoding: 'json' })
637635
const sub2 = db.sublevel('test2', { keyEncoding: 'buffer', valueEncoding: 'buffer' })
638636

639-
// TODO: async/await
640-
sub1.put('foo', { some: 'json' }).then(function () {
641-
t.same(puts, [{
642-
key: '!test1!foo',
643-
value: '{"some":"json"}',
644-
keyEncoding: 'utf8',
645-
valueEncoding: 'utf8'
646-
}])
647-
648-
sub1.get('foo').then(function (value) {
649-
t.same(value, { some: 'json' })
650-
t.same(gets.shift(), {
651-
key: '!test1!foo',
652-
keyEncoding: 'utf8',
653-
valueEncoding: 'utf8'
654-
})
637+
await sub1.put('foo', { some: 'json' })
655638

656-
sub2.put(Buffer.from([1, 2]), Buffer.from([3])).then(function () {
657-
t.same(puts, [{
658-
key: Buffer.from('!test2!\x01\x02'),
659-
value: Buffer.from([3]),
660-
keyEncoding: 'buffer',
661-
valueEncoding: 'buffer'
662-
}])
663-
664-
sub2.get(Buffer.from([1, 2])).then(function (value) {
665-
t.same(value, Buffer.from([3]))
666-
t.same(gets.shift(), {
667-
key: Buffer.from('!test2!\x01\x02'),
668-
keyEncoding: 'buffer',
669-
valueEncoding: 'buffer'
670-
})
671-
})
672-
})
673-
})
639+
t.same(puts, [{
640+
key: '!test1!foo',
641+
value: '{"some":"json"}',
642+
keyEncoding: 'utf8',
643+
valueEncoding: 'utf8'
644+
}])
645+
646+
t.same(await sub1.get('foo'), { some: 'json' })
647+
t.same(gets.shift(), {
648+
key: '!test1!foo',
649+
keyEncoding: 'utf8',
650+
valueEncoding: 'utf8'
651+
})
652+
653+
await sub2.put(Buffer.from([1, 2]), Buffer.from([3]))
654+
655+
t.same(puts, [{
656+
key: Buffer.from('!test2!\x01\x02'),
657+
value: Buffer.from([3]),
658+
keyEncoding: 'buffer',
659+
valueEncoding: 'buffer'
660+
}])
661+
662+
t.same(await sub2.get(Buffer.from([1, 2])), Buffer.from([3]))
663+
664+
t.same(gets.shift(), {
665+
key: Buffer.from('!test2!\x01\x02'),
666+
keyEncoding: 'buffer',
667+
valueEncoding: 'buffer'
674668
})
675669
})
676670

0 commit comments

Comments
 (0)