Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit 9d93fda

Browse files
authored
Binary tests (#146)
* test that Buffer key returns as ArrayBuffer with keyAsBuffer=false * test that Buffer value returns as Uint8Array with asBuffer=false * test combinations of Buffer/ArrayBuffer/Uint8Array values and asBuffer true/false * remove obsolete comment
1 parent 9d5ec5c commit 9d93fda

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

test/custom-test.js

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function (leveljs, test, testCommon) {
4747
})
4848
})
4949

50-
test('buffer value', function (t) {
50+
test('put Buffer value, get Buffer value', function (t) {
5151
var level = leveljs(testCommon.location())
5252
level.open(function (err) {
5353
t.notOk(err, 'no error')
@@ -63,6 +63,88 @@ module.exports = function (leveljs, test, testCommon) {
6363
})
6464
})
6565

66+
test('put Buffer value, get Uint8Array value', function (t) {
67+
var level = leveljs(testCommon.location())
68+
level.open(function (err) {
69+
t.notOk(err, 'no error')
70+
level.put('key', Buffer.from('00ff', 'hex'), function (err) {
71+
t.notOk(err, 'no error')
72+
level.get('key', { asBuffer: false }, function (err, value) {
73+
t.notOk(err, 'no error')
74+
t.notOk(Buffer.isBuffer(value), 'is not a buffer')
75+
t.ok(value instanceof Uint8Array, 'is a Uint8Array')
76+
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
77+
level.close(t.end.bind(t))
78+
})
79+
})
80+
})
81+
})
82+
83+
test('put Uint8Array value, get Buffer value', function (t) {
84+
var level = leveljs(testCommon.location())
85+
level.open(function (err) {
86+
t.notOk(err, 'no error')
87+
level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) {
88+
t.notOk(err, 'no error')
89+
level.get('key', function (err, value) {
90+
t.notOk(err, 'no error')
91+
t.ok(Buffer.isBuffer(value), 'is buffer')
92+
t.same(value, Buffer.from('00ff', 'hex'))
93+
level.close(t.end.bind(t))
94+
})
95+
})
96+
})
97+
})
98+
99+
test('put Uint8Array value, get Uint8Array value', function (t) {
100+
var level = leveljs(testCommon.location())
101+
level.open(function (err) {
102+
t.notOk(err, 'no error')
103+
level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) {
104+
t.notOk(err, 'no error')
105+
level.get('key', { asBuffer: false }, function (err, value) {
106+
t.notOk(err, 'no error')
107+
t.notOk(Buffer.isBuffer(value), 'is not a buffer')
108+
t.ok(value instanceof Uint8Array, 'is a Uint8Array')
109+
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
110+
level.close(t.end.bind(t))
111+
})
112+
})
113+
})
114+
})
115+
116+
test('put ArrayBuffer value, get Buffer value', function (t) {
117+
var level = leveljs(testCommon.location())
118+
level.open(function (err) {
119+
t.notOk(err, 'no error')
120+
level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) {
121+
t.notOk(err, 'no error')
122+
level.get('key', function (err, value) {
123+
t.notOk(err, 'no error')
124+
t.ok(Buffer.isBuffer(value), 'is buffer')
125+
t.same(value, Buffer.from('00ff', 'hex'))
126+
level.close(t.end.bind(t))
127+
})
128+
})
129+
})
130+
})
131+
132+
test('put ArrayBuffer value, get ArrayBuffer value', function (t) {
133+
var level = leveljs(testCommon.location())
134+
level.open(function (err) {
135+
t.notOk(err, 'no error')
136+
level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) {
137+
t.notOk(err, 'no error')
138+
level.get('key', { asBuffer: false }, function (err, value) {
139+
t.notOk(err, 'no error')
140+
t.ok(value instanceof ArrayBuffer, 'is a ArrayBuffer')
141+
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
142+
level.close(t.end.bind(t))
143+
})
144+
})
145+
})
146+
})
147+
66148
// This should be covered by abstract-leveldown tests, but that's
67149
// prevented by process.browser checks (Level/abstract-leveldown#121).
68150
// This test is adapted from memdown.

test/key-type-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var types = [
1616
{ type: 'ArrayBuffer', ctor: true, allowFailure: true, key: ta(Buffer).buffer },
1717
{ type: 'Int8Array', ctor: true, allowFailure: true, createKey: ta, view: true },
1818
{ type: 'Uint8Array', ctor: true, allowFailure: true, createKey: ta, view: true },
19+
{ name: 'Buffer', type: 'Uint8Array', ctor: true, allowFailure: true, key: ta(Buffer), view: true },
1920
{ type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createKey: ta, view: true },
2021
{ type: 'Int16Array', ctor: true, allowFailure: true, createKey: ta, view: true },
2122
{ type: 'Uint16Array', ctor: true, allowFailure: true, createKey: ta, view: true },

test/structured-clone-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var types = [
4242

4343
// Don't allow failure as this is the primary type for binary (Buffer) data
4444
{ type: 'Uint8Array', ctor: true, createValue: ta },
45+
{ name: 'Buffer', type: 'Uint8Array', ctor: true, value: ta(Buffer) },
4546

4647
{ type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta },
4748
{ type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta },

util/mixed-to-buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ var toBuffer = require('typedarray-to-buffer')
44

55
module.exports = function (value) {
66
if (value instanceof Uint8Array) return toBuffer(value)
7-
else if (value instanceof ArrayBuffer) return Buffer.from(value) // For keys.
7+
else if (value instanceof ArrayBuffer) return Buffer.from(value)
88
else return Buffer.from(String(value))
99
}

0 commit comments

Comments
 (0)