|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { illegalKeys } = require('./util') |
| 4 | +const traits = require('./traits') |
| 5 | + |
| 6 | +let db |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {import('tape')} test |
| 10 | + */ |
| 11 | +exports.setUp = function (test, testCommon) { |
| 12 | + test('hasMany() setup', async function (t) { |
| 13 | + db = testCommon.factory() |
| 14 | + return db.open() |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {import('tape')} test |
| 20 | + */ |
| 21 | +exports.args = function (test, testCommon) { |
| 22 | + test('hasMany() requires an array argument', function (t) { |
| 23 | + t.plan(6) |
| 24 | + |
| 25 | + db.hasMany().catch(function (err) { |
| 26 | + t.is(err && err.name, 'TypeError') |
| 27 | + t.is(err && err.message, "The first argument 'keys' must be an array") |
| 28 | + }) |
| 29 | + |
| 30 | + db.hasMany('foo').catch(function (err) { |
| 31 | + t.is(err && err.name, 'TypeError') |
| 32 | + t.is(err && err.message, "The first argument 'keys' must be an array") |
| 33 | + }) |
| 34 | + |
| 35 | + db.hasMany('foo', {}).catch(function (err) { |
| 36 | + t.is(err && err.name, 'TypeError') |
| 37 | + t.is(err && err.message, "The first argument 'keys' must be an array") |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + test('hasMany() with illegal keys', function (t) { |
| 42 | + t.plan(illegalKeys.length * 4) |
| 43 | + |
| 44 | + for (const { name, key } of illegalKeys) { |
| 45 | + db.hasMany([key]).catch(function (err) { |
| 46 | + t.ok(err instanceof Error, name + ' - is Error') |
| 47 | + t.is(err.code, 'LEVEL_INVALID_KEY', name + ' - correct error code') |
| 48 | + }) |
| 49 | + |
| 50 | + db.hasMany(['valid', key]).catch(function (err) { |
| 51 | + t.ok(err instanceof Error, name + ' - is Error (second key)') |
| 52 | + t.is(err.code, 'LEVEL_INVALID_KEY', name + ' - correct error code (second key)') |
| 53 | + }) |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param {import('tape')} test |
| 60 | + */ |
| 61 | +exports.hasMany = function (test, testCommon) { |
| 62 | + test('simple hasMany()', async function (t) { |
| 63 | + await db.put('foo', 'bar') |
| 64 | + |
| 65 | + t.same(await db.hasMany(['foo']), [true]) |
| 66 | + t.same(await db.hasMany(['foo'], {}), [true]) // same but with {} |
| 67 | + t.same(await db.hasMany(['beep']), [false]) |
| 68 | + |
| 69 | + await db.put('beep', 'boop') |
| 70 | + |
| 71 | + t.same(await db.hasMany(['beep']), [true]) |
| 72 | + t.same(await db.hasMany(['foo', 'beep']), [true, true]) |
| 73 | + t.same(await db.hasMany(['aaa', 'beep']), [false, true]) |
| 74 | + t.same(await db.hasMany(['beep', 'aaa']), [true, false], 'maintains order of input keys') |
| 75 | + }) |
| 76 | + |
| 77 | + test('empty hasMany()', async function (t) { |
| 78 | + t.same(await db.hasMany([]), []) |
| 79 | + |
| 80 | + const encodings = Object.keys(db.supports.encodings) |
| 81 | + .filter(k => db.supports.encodings[k]) |
| 82 | + |
| 83 | + for (const valueEncoding of encodings) { |
| 84 | + t.same(await db.hasMany([], { valueEncoding }), []) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + test('simultaneous hasMany()', async function (t) { |
| 89 | + t.plan(20) |
| 90 | + |
| 91 | + await db.put('hello', 'world') |
| 92 | + const promises = [] |
| 93 | + |
| 94 | + for (let i = 0; i < 10; ++i) { |
| 95 | + promises.push(db.hasMany(['hello']).then(function (values) { |
| 96 | + t.same(values, [true]) |
| 97 | + })) |
| 98 | + } |
| 99 | + |
| 100 | + for (let i = 0; i < 10; ++i) { |
| 101 | + promises.push(db.hasMany(['non-existent']).then(function (values) { |
| 102 | + t.same(values, [false]) |
| 103 | + })) |
| 104 | + } |
| 105 | + |
| 106 | + return Promise.all(promises) |
| 107 | + }) |
| 108 | + |
| 109 | + traits.open('hasMany()', testCommon, async function (t, db) { |
| 110 | + t.same(await db.hasMany(['foo']), [false]) |
| 111 | + }) |
| 112 | + |
| 113 | + traits.closed('hasMany()', testCommon, async function (t, db) { |
| 114 | + return db.hasMany(['foo']) |
| 115 | + }) |
| 116 | + |
| 117 | + // Also test empty array because it has a fast-path |
| 118 | + traits.open('hasMany() with empty array', testCommon, async function (t, db) { |
| 119 | + t.same(await db.hasMany([]), []) |
| 120 | + }) |
| 121 | + |
| 122 | + traits.closed('hasMany() with empty array', testCommon, async function (t, db) { |
| 123 | + return db.hasMany([]) |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * @param {import('tape')} test |
| 129 | + */ |
| 130 | +exports.tearDown = function (test, testCommon) { |
| 131 | + test('hasMany() teardown', async function (t) { |
| 132 | + return db.close() |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * @param {import('tape')} test |
| 138 | + */ |
| 139 | +exports.all = function (test, testCommon) { |
| 140 | + exports.setUp(test, testCommon) |
| 141 | + exports.args(test, testCommon) |
| 142 | + exports.hasMany(test, testCommon) |
| 143 | + exports.tearDown(test, testCommon) |
| 144 | +} |
0 commit comments