|
| 1 | +/* global indexedDB */ |
| 2 | + |
| 3 | +'use strict' |
| 4 | + |
| 5 | +var ta = require('./util/create-typed-array') |
| 6 | +var support = require('../util/support') |
| 7 | + |
| 8 | +var types = [ |
| 9 | + { type: 'number', value: -20 }, |
| 10 | + { type: '+Infinity', value: Infinity }, |
| 11 | + { type: '-Infinity', value: -Infinity }, |
| 12 | + { type: 'string', value: 'test' }, |
| 13 | + { type: 'Date', ctor: true, value: new Date() }, |
| 14 | + { type: 'Array', ctor: true, allowFailure: true, value: [0, '1'] }, |
| 15 | + { type: 'ArrayBuffer', ctor: true, allowFailure: true, value: ta(Buffer).buffer }, |
| 16 | + { type: 'Int8Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 17 | + { type: 'Uint8Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 18 | + { type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 19 | + { type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 20 | + { type: 'Uint16Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 21 | + { type: 'Int32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 22 | + { type: 'Uint32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 23 | + { type: 'Float32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, |
| 24 | + { type: 'Float64Array', ctor: true, allowFailure: true, createValue: ta, view: true } |
| 25 | +] |
| 26 | + |
| 27 | +// TODO: test types that are not supported by IndexedDB Second Edition |
| 28 | +// - Date NaN (should be rejected by IndexedDB) |
| 29 | +// - empty array (should be rejected by abstract-leveldown) |
| 30 | +// - array containing null (should be rejected by IndexedDB) |
| 31 | +// - cyclical array (not sure) |
| 32 | +// - Array(10) (not sure) |
| 33 | +// var illegalTypes = [] |
| 34 | + |
| 35 | +// TODO: test types that are not supported by IndexedDB Second Edition, but get |
| 36 | +// stringified for abstract-leveldown compatibility. |
| 37 | +// - NaN |
| 38 | +// - boolean |
| 39 | +// var stringifiedTypes = [] |
| 40 | + |
| 41 | +module.exports = function (leveljs, test, testCommon) { |
| 42 | + var db |
| 43 | + |
| 44 | + test('setUp', testCommon.setUp) |
| 45 | + test('open', function (t) { |
| 46 | + db = leveljs(testCommon.location()) |
| 47 | + db.open(t.end.bind(t)) |
| 48 | + }) |
| 49 | + |
| 50 | + types.forEach(function (item) { |
| 51 | + var testName = item.name || item.type |
| 52 | + |
| 53 | + test('key type: ' + testName, function (t) { |
| 54 | + var Constructor = item.ctor ? global[item.type] : null |
| 55 | + var skip = item.allowFailure ? 'pass' : 'fail' |
| 56 | + var input = item.value |
| 57 | + |
| 58 | + if (item.ctor && !Constructor) { |
| 59 | + t[skip]('constructor is undefined in this environment') |
| 60 | + return t.end() |
| 61 | + } |
| 62 | + |
| 63 | + if (item.createValue) { |
| 64 | + try { |
| 65 | + input = item.createValue(Constructor) |
| 66 | + } catch (err) { |
| 67 | + t[skip]('constructor is not spec-compliant in this environment') |
| 68 | + return t.end() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (!support.test(input)(indexedDB)) { |
| 73 | + t[skip]('type is not supported in this environment') |
| 74 | + return t.end() |
| 75 | + } |
| 76 | + |
| 77 | + db.put(input, testName, function (err) { |
| 78 | + t.ifError(err, 'no put error') |
| 79 | + |
| 80 | + db.get(input, { asBuffer: false }, function (err, value) { |
| 81 | + t.ifError(err, 'no get error') |
| 82 | + t.same(value, testName, 'correct value') |
| 83 | + |
| 84 | + var it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false }) |
| 85 | + |
| 86 | + testCommon.collectEntries(it, function (err, entries) { |
| 87 | + t.ifError(err, 'no iterator error') |
| 88 | + t.is(entries.length, 1, '1 entry') |
| 89 | + |
| 90 | + var key = entries[0].key |
| 91 | + var value = entries[0].value |
| 92 | + |
| 93 | + if (Constructor) { |
| 94 | + var type = item.view ? 'ArrayBuffer' : item.type |
| 95 | + var expected = '[object ' + type + ']' |
| 96 | + var actual = Object.prototype.toString.call(key) |
| 97 | + |
| 98 | + if (actual === expected) { |
| 99 | + t.is(actual, expected, 'prototype') |
| 100 | + } else { |
| 101 | + t[skip]('(de)serializing is not supported by this environment') |
| 102 | + return t.end() |
| 103 | + } |
| 104 | + |
| 105 | + if (item.view) { |
| 106 | + t.ok(key instanceof ArrayBuffer, 'key is instanceof ArrayBuffer') |
| 107 | + t.same(Buffer.from(new Constructor(key)), ta(Buffer), 'correct octets') |
| 108 | + } else { |
| 109 | + t.ok(key instanceof Constructor, 'key is instanceof ' + type) |
| 110 | + t.same(key, input, 'correct key') |
| 111 | + } |
| 112 | + } else { |
| 113 | + t.is(key, input, 'correct key') |
| 114 | + } |
| 115 | + |
| 116 | + t.same(value, testName, 'correct value') |
| 117 | + |
| 118 | + db.del(input, function (err) { |
| 119 | + t.ifError(err, 'no del error') |
| 120 | + t.end() |
| 121 | + }) |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + test('close', function (t) { db.close(t.end.bind(t)) }) |
| 129 | + test('tearDown', testCommon.tearDown) |
| 130 | +} |
0 commit comments