Skip to content

Commit 3c54d51

Browse files
committed
Test encodings and snapshots
1 parent f931e20 commit 3c54d51

8 files changed

+147
-16
lines changed

test/encoding-buffer-test.js

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,10 @@ exports.all = function (test, testCommon) {
218218
const db = testCommon.factory({ keyEncoding })
219219
await db.open()
220220

221+
// These are equal when compared as strings but not when compared as buffers
221222
const one = Buffer.from('80', 'hex')
222223
const two = Buffer.from('c0', 'hex')
223224

224-
t.ok(two.toString() === one.toString(), 'would be equal when not byte-aware')
225-
t.ok(two.compare(one) > 0, 'but greater when byte-aware')
226-
227225
await db.put(one, 'one')
228226
t.is(await db.get(one), 'one', 'value one ok')
229227

@@ -232,6 +230,65 @@ exports.all = function (test, testCommon) {
232230

233231
return db.close()
234232
})
233+
234+
if (testCommon.supports.getSync) {
235+
test(`storage is byte-aware (${keyEncoding} encoding) (sync)`, async function (t) {
236+
const db = testCommon.factory({ keyEncoding })
237+
await db.open()
238+
239+
// These are equal when compared as strings but not when compared as buffers
240+
const one = Buffer.from('80', 'hex')
241+
const two = Buffer.from('c0', 'hex')
242+
243+
await db.put(one, 'one')
244+
t.is(db.getSync(one), 'one', 'value one ok')
245+
246+
await db.put(two, 'two')
247+
t.is(db.getSync(one), 'one', 'value one did not change')
248+
249+
return db.close()
250+
})
251+
}
252+
253+
test(`respects buffer offset and length (${keyEncoding} encoding)`, async function (t) {
254+
const db = testCommon.factory({ keyEncoding })
255+
await db.open()
256+
257+
const a = Buffer.from('000102', 'hex')
258+
const b = a.subarray(1) // 0102
259+
const c = a.subarray(0, 1) // 00
260+
261+
await db.put(a, 'a')
262+
await db.put(b, 'b')
263+
await db.put(c, 'c')
264+
265+
t.is(await db.get(a), 'a', 'value a ok')
266+
t.is(await db.get(b), 'b', 'value b ok')
267+
t.is(await db.get(c), 'c', 'value c ok')
268+
269+
return db.close()
270+
})
271+
272+
if (testCommon.supports.getSync) {
273+
test(`respects buffer offset (${keyEncoding} encoding) (sync)`, async function (t) {
274+
const db = testCommon.factory({ keyEncoding })
275+
await db.open()
276+
277+
const a = Buffer.from('000102', 'hex')
278+
const b = a.subarray(1) // 0102
279+
const c = a.subarray(0, 1) // 00
280+
281+
await db.put(a, 'a')
282+
await db.put(b, 'b')
283+
await db.put(c, 'c')
284+
285+
t.is(db.getSync(a), 'a', 'value a ok')
286+
t.is(db.getSync(b), 'b', 'value b ok')
287+
t.is(db.getSync(c), 'c', 'value c ok')
288+
289+
return db.close()
290+
})
291+
}
235292
}
236293
}
237294

test/encoding-custom-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ exports.all = function (test, testCommon) {
8181
t.same(await db.get(entry.key), entry.value)
8282
}
8383

84+
if (testCommon.supports.getSync) {
85+
for (const entry of entries) {
86+
t.same(db.getSync(entry.key), entry.value)
87+
}
88+
}
89+
8490
return db.close()
8591
}
8692
}

test/encoding-decode-error-test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ exports.all = function (test, testCommon) {
1212
})
1313

1414
// NOTE: adapted from encoding-down
15-
test('decode error is wrapped by get() and getMany()', async function (t) {
16-
t.plan(4)
15+
test('decode error is wrapped by get() and variants', async function (t) {
16+
t.plan(testCommon.supports.getSync ? 6 : 4)
1717

1818
const key = testKey()
1919
const valueEncoding = {
@@ -37,11 +37,20 @@ exports.all = function (test, testCommon) {
3737
t.is(err.code, 'LEVEL_DECODE_ERROR')
3838
t.is(err.cause.message, 'decode error xyz')
3939
}
40+
41+
if (testCommon.supports.getSync) {
42+
try {
43+
db.getSync(key, { valueEncoding })
44+
} catch (err) {
45+
t.is(err.code, 'LEVEL_DECODE_ERROR')
46+
t.is(err.cause.message, 'decode error xyz')
47+
}
48+
}
4049
})
4150

4251
// NOTE: adapted from encoding-down
43-
test('get() and getMany() yield decode error if stored value is invalid', async function (t) {
44-
t.plan(4)
52+
test('get() and variants yield decode error if stored value is invalid', async function (t) {
53+
t.plan(testCommon.supports.getSync ? 6 : 4)
4554

4655
const key = testKey()
4756
await db.put(key, 'this {} is [] not : json', { valueEncoding: 'utf8' })
@@ -59,6 +68,15 @@ exports.all = function (test, testCommon) {
5968
t.is(err.code, 'LEVEL_DECODE_ERROR')
6069
t.is(err.cause.name, 'SyntaxError') // From JSON.parse()
6170
}
71+
72+
if (testCommon.supports.getSync) {
73+
try {
74+
db.getSync(key, { valueEncoding: 'json' })
75+
} catch (err) {
76+
t.is(err.code, 'LEVEL_DECODE_ERROR')
77+
t.is(err.cause.name, 'SyntaxError') // From JSON.parse()
78+
}
79+
}
6280
})
6381

6482
test('decode error teardown', async function (t) {

test/encoding-json-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ exports.all = function (test, testCommon) {
5555
await db.batch(operations)
5656
await Promise.all([...entries.map(testGet), testIterator()])
5757

58+
if (testCommon.supports.getSync) {
59+
for (const entry of entries) {
60+
t.same(db.getSync(entry.key), entry.value)
61+
}
62+
}
63+
5864
return db.close()
5965

6066
async function testGet (entry) {

test/encoding-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ exports.all = function (test, testCommon) {
5959
if (!deferred) await db.open()
6060
await db.put(1, 2)
6161
t.is(await db.get(1), '2')
62+
testCommon.supports.getSync && t.is(db.getSync(1), '2')
6263
return db.close()
6364
})
6465
}
@@ -68,15 +69,25 @@ exports.all = function (test, testCommon) {
6869
const key = testKey()
6970
const data = { thisis: 'json' }
7071
await db.put(key, JSON.stringify(data), { valueEncoding: 'utf8' })
72+
7173
t.same(await db.get(key, { valueEncoding: 'json' }), data, 'got parsed object')
74+
75+
if (testCommon.supports.getSync) {
76+
t.same(db.getSync(key, { valueEncoding: 'json' }), data, 'got parsed object (sync)')
77+
}
7278
})
7379

7480
// NOTE: adapted from encoding-down
7581
test('can decode from json to string', async function (t) {
7682
const data = { thisis: 'json' }
7783
const key = testKey()
7884
await db.put(key, data, { valueEncoding: 'json' })
85+
7986
t.same(await db.get(key, { valueEncoding: 'utf8' }), JSON.stringify(data), 'got unparsed JSON string')
87+
88+
if (testCommon.supports.getSync) {
89+
t.same(db.getSync(key, { valueEncoding: 'utf8' }), JSON.stringify(data), 'got unparsed JSON string (sync)')
90+
}
8091
})
8192

8293
// NOTE: adapted from encoding-down

test/get-sync-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ exports.tearDown = function (test, testCommon) {
5353
})
5454
}
5555

56-
// TODO: test encodings, snapshots
5756
exports.all = function (test, testCommon) {
5857
exports.setUp(test, testCommon)
5958
exports.args(test, testCommon)

test/iterator-explicit-snapshot-test.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ exports.get = function (test, testCommon) {
1818
const { testFresh, testClose } = testFactory(test, testCommon)
1919

2020
testFresh('get() changed entry from snapshot', async function (t, db) {
21-
t.plan(3)
22-
2321
await db.put('abc', 'before')
2422
const snapshot = db.snapshot()
2523
await db.put('abc', 'after')
@@ -28,12 +26,16 @@ exports.get = function (test, testCommon) {
2826
t.is(await db.get('abc', { snapshot }), 'before')
2927
t.is(await db.get('other', { snapshot }), undefined)
3028

29+
if (testCommon.supports.getSync) {
30+
t.is(db.getSync('abc'), 'after')
31+
t.is(db.getSync('abc', { snapshot }), 'before')
32+
t.is(db.getSync('other', { snapshot }), undefined)
33+
}
34+
3135
return snapshot.close()
3236
})
3337

3438
testFresh('get() deleted entry from snapshot', async function (t, db) {
35-
t.plan(3)
36-
3739
await db.put('abc', 'before')
3840
const snapshot = db.snapshot()
3941
await db.del('abc')
@@ -42,27 +44,34 @@ exports.get = function (test, testCommon) {
4244
t.is(await db.get('abc', { snapshot }), 'before')
4345
t.is(await db.get('other', { snapshot }), undefined)
4446

47+
if (testCommon.supports.getSync) {
48+
t.is(db.getSync('abc'), undefined)
49+
t.is(db.getSync('abc', { snapshot }), 'before')
50+
t.is(db.getSync('other', { snapshot }), undefined)
51+
}
52+
4553
return snapshot.close()
4654
})
4755

4856
testFresh('get() non-existent entry from snapshot', async function (t, db) {
49-
t.plan(2)
50-
5157
const snapshot = db.snapshot()
5258
await db.put('abc', 'after')
5359

5460
t.is(await db.get('abc'), 'after')
5561
t.is(await db.get('abc', { snapshot }), undefined)
5662

63+
if (testCommon.supports.getSync) {
64+
t.is(db.getSync('abc'), 'after')
65+
t.is(db.getSync('abc', { snapshot }), undefined)
66+
}
67+
5768
return snapshot.close()
5869
})
5970

6071
testFresh('get() entries from multiple snapshots', async function (t, db) {
6172
const snapshots = []
6273
const iterations = 100
6374

64-
t.plan(iterations)
65-
6675
for (let i = 0; i < iterations; i++) {
6776
await db.put('number', i.toString())
6877
snapshots.push(db.snapshot())
@@ -73,6 +82,10 @@ exports.get = function (test, testCommon) {
7382
const value = i.toString()
7483

7584
t.is(await db.get('number', { snapshot }), value)
85+
86+
if (testCommon.supports.getSync) {
87+
t.is(db.getSync('number', { snapshot }), value)
88+
}
7689
}
7790

7891
return Promise.all(snapshots.map(x => x.close()))
@@ -90,12 +103,22 @@ exports.get = function (test, testCommon) {
90103
// Closing one snapshot should not affect the other
91104
t.is(await db.get('abc', { snapshot: snapshot2 }), 'before')
92105

106+
if (testCommon.supports.getSync) {
107+
t.is(db.getSync('abc', { snapshot: snapshot2 }), 'before')
108+
}
109+
93110
return snapshot2.close()
94111
})
95112

96113
testClose('get()', async function (db, snapshot) {
97114
return db.get('xyz', { snapshot })
98115
})
116+
117+
if (testCommon.supports.getSync) {
118+
testClose('getSync()', async function (db, snapshot) {
119+
return db.getSync('xyz', { snapshot })
120+
})
121+
}
99122
}
100123

101124
exports.getMany = function (test, testCommon) {

test/put-get-del-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ function makeTest (test, type, key, value, expectedValue) {
99

1010
test('put(), get(), del() with ' + type, async function (t) {
1111
await db.put(key, value)
12+
1213
t.is((await db.get(key)).toString(), stringValue)
14+
15+
if (db.supports.getSync) {
16+
t.is(db.getSync(key).toString(), stringValue)
17+
}
18+
1319
await db.del(key)
20+
1421
t.is(await db.get(key), undefined, 'not found')
22+
23+
if (db.supports.getSync) {
24+
t.is(db.getSync(key), undefined, 'not found')
25+
}
1526
})
1627
}
1728

0 commit comments

Comments
 (0)