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

Binary tests #146

Merged
merged 4 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion test/custom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function (leveljs, test, testCommon) {
})
})

test('buffer value', function (t) {
test('put Buffer value, get Buffer value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
Expand All @@ -63,6 +63,88 @@ module.exports = function (leveljs, test, testCommon) {
})
})

test('put Buffer value, get Uint8Array value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
level.put('key', Buffer.from('00ff', 'hex'), function (err) {
t.notOk(err, 'no error')
level.get('key', { asBuffer: false }, function (err, value) {
t.notOk(err, 'no error')
t.notOk(Buffer.isBuffer(value), 'is not a buffer')
t.ok(value instanceof Uint8Array, 'is a Uint8Array')
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
level.close(t.end.bind(t))
})
})
})
})

test('put Uint8Array value, get Buffer value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) {
t.notOk(err, 'no error')
level.get('key', function (err, value) {
t.notOk(err, 'no error')
t.ok(Buffer.isBuffer(value), 'is buffer')
t.same(value, Buffer.from('00ff', 'hex'))
level.close(t.end.bind(t))
})
})
})
})

test('put Uint8Array value, get Uint8Array value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) {
t.notOk(err, 'no error')
level.get('key', { asBuffer: false }, function (err, value) {
t.notOk(err, 'no error')
t.notOk(Buffer.isBuffer(value), 'is not a buffer')
t.ok(value instanceof Uint8Array, 'is a Uint8Array')
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
level.close(t.end.bind(t))
})
})
})
})

test('put ArrayBuffer value, get Buffer value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) {
t.notOk(err, 'no error')
level.get('key', function (err, value) {
t.notOk(err, 'no error')
t.ok(Buffer.isBuffer(value), 'is buffer')
t.same(value, Buffer.from('00ff', 'hex'))
level.close(t.end.bind(t))
})
})
})
})

test('put ArrayBuffer value, get ArrayBuffer value', function (t) {
var level = leveljs(testCommon.location())
level.open(function (err) {
t.notOk(err, 'no error')
level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) {
t.notOk(err, 'no error')
level.get('key', { asBuffer: false }, function (err, value) {
t.notOk(err, 'no error')
t.ok(value instanceof ArrayBuffer, 'is a ArrayBuffer')
t.same(Buffer.from(value), Buffer.from('00ff', 'hex'))
level.close(t.end.bind(t))
})
})
})
})

// This should be covered by abstract-leveldown tests, but that's
// prevented by process.browser checks (Level/abstract-leveldown#121).
// This test is adapted from memdown.
Expand Down
1 change: 1 addition & 0 deletions test/key-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var types = [
{ type: 'ArrayBuffer', ctor: true, allowFailure: true, key: ta(Buffer).buffer },
{ type: 'Int8Array', ctor: true, allowFailure: true, createKey: ta, view: true },
{ type: 'Uint8Array', ctor: true, allowFailure: true, createKey: ta, view: true },
{ name: 'Buffer', type: 'Uint8Array', ctor: true, allowFailure: true, key: ta(Buffer), view: true },
{ type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createKey: ta, view: true },
{ type: 'Int16Array', ctor: true, allowFailure: true, createKey: ta, view: true },
{ type: 'Uint16Array', ctor: true, allowFailure: true, createKey: ta, view: true },
Expand Down
1 change: 1 addition & 0 deletions test/structured-clone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var types = [

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

{ type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta },
Expand Down
2 changes: 1 addition & 1 deletion util/mixed-to-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ var toBuffer = require('typedarray-to-buffer')

module.exports = function (value) {
if (value instanceof Uint8Array) return toBuffer(value)
else if (value instanceof ArrayBuffer) return Buffer.from(value) // For keys.
else if (value instanceof ArrayBuffer) return Buffer.from(value)
else return Buffer.from(String(value))
}