Skip to content

Commit 9ea8770

Browse files
authored
Generalize prewrite test for memory-level (#90)
Ref: Level/memory-level#10 Category: fix
1 parent 847aac2 commit 9ea8770

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

test/hooks/prewrite.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ module.exports = function (test, testCommon) {
769769
const db = testCommon.factory()
770770
await db.open()
771771

772+
const textDecoder = new TextDecoder()
772773
const books = db.sublevel('books', { valueEncoding: 'json' })
773774
const index = db.sublevel('authors', {
774775
// Use JSON, which normally doesn't make sense for keys but
@@ -779,7 +780,14 @@ module.exports = function (test, testCommon) {
779780
db.on('write', (ops) => {
780781
// Check that data is written to correct sublevels, specifically
781782
// !authors!Hesse~12 rather than !books!!authors!Hesse~12.
782-
t.same(ops.map(x => x.key), ['!books!12', '!authors!"Hesse~12"'])
783+
t.same(ops.map(x => decode(x.key)), ['!books!12', '!authors!"Hesse~12"'])
784+
785+
// It's unfortunate DX but because the write is made via the sublevel, the
786+
// format of keys depends on the supported encodings of db. For example on
787+
// a MemoryLevel({ storeEncoding: 'buffer' }) the key will be a buffer.
788+
function decode (key) {
789+
return db.keyEncoding('utf8').format === 'utf8' ? key : textDecoder.decode(key)
790+
}
783791
})
784792

785793
books.on('write', (ops) => {

0 commit comments

Comments
 (0)