|
1 |
| -const test = require('tape') |
2 |
| - , testCommon = require('abstract-leveldown/testCommon') |
3 |
| - , leveldown = require('../') |
4 |
| - , abstract = require('abstract-leveldown/abstract/approximate-size-test') |
| 1 | +const test = require('tape') |
| 2 | +const leveldown = require('..') |
| 3 | +const testCommon = require('abstract-leveldown/testCommon') |
5 | 4 |
|
6 |
| -abstract.all(leveldown, test, testCommon) |
| 5 | +var db |
| 6 | + |
| 7 | +test('setUp common for approximate size', testCommon.setUp) |
| 8 | + |
| 9 | +test('setUp db', function (t) { |
| 10 | + db = leveldown(testCommon.location()) |
| 11 | + console.log('db', db) |
| 12 | + db.open(t.end.bind(t)) |
| 13 | +}) |
| 14 | + |
| 15 | +test('test argument-less approximateSize() throws', function (t) { |
| 16 | + t.throws( |
| 17 | + db.approximateSize.bind(db) |
| 18 | + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } |
| 19 | + , 'no-arg approximateSize() throws' |
| 20 | + ) |
| 21 | + t.end() |
| 22 | +}) |
| 23 | + |
| 24 | +test('test callback-less, 1-arg, approximateSize() throws', function (t) { |
| 25 | + t.throws( |
| 26 | + db.approximateSize.bind(db, 'foo') |
| 27 | + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } |
| 28 | + , 'callback-less, 1-arg approximateSize() throws' |
| 29 | + ) |
| 30 | + t.end() |
| 31 | +}) |
| 32 | + |
| 33 | +test('test callback-less, 2-arg, approximateSize() throws', function (t) { |
| 34 | + t.throws( |
| 35 | + db.approximateSize.bind(db, 'foo', 'bar') |
| 36 | + , { name: 'Error', message: 'approximateSize() requires a callback argument' } |
| 37 | + , 'callback-less, 2-arg approximateSize() throws' |
| 38 | + ) |
| 39 | + t.end() |
| 40 | +}) |
| 41 | + |
| 42 | +test('test callback-less, 3-arg, approximateSize() throws', function (t) { |
| 43 | + t.throws( |
| 44 | + db.approximateSize.bind(db, function () {}) |
| 45 | + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } |
| 46 | + , 'callback-only approximateSize() throws' |
| 47 | + ) |
| 48 | + t.end() |
| 49 | +}) |
| 50 | + |
| 51 | +test('test callback-only approximateSize() throws', function (t) { |
| 52 | + t.throws( |
| 53 | + db.approximateSize.bind(db, function () {}) |
| 54 | + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } |
| 55 | + , 'callback-only approximateSize() throws' |
| 56 | + ) |
| 57 | + t.end() |
| 58 | +}) |
| 59 | + |
| 60 | +test('test 1-arg + callback approximateSize() throws', function (t) { |
| 61 | + t.throws( |
| 62 | + db.approximateSize.bind(db, 'foo', function () {}) |
| 63 | + , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' } |
| 64 | + , '1-arg + callback approximateSize() throws' |
| 65 | + ) |
| 66 | + t.end() |
| 67 | +}) |
| 68 | + |
| 69 | +test('test custom _serialize*', function (t) { |
| 70 | + t.plan(4) |
| 71 | + var db = leveldown(testCommon.location()) |
| 72 | + db._serializeKey = function (data) { return data } |
| 73 | + db.approximateSize = function (start, end, callback) { |
| 74 | + t.deepEqual(start, { foo: 'bar' }) |
| 75 | + t.deepEqual(end, { beep: 'boop' }) |
| 76 | + process.nextTick(callback) |
| 77 | + } |
| 78 | + db.open(function () { |
| 79 | + db.approximateSize({ foo: 'bar' }, { beep: 'boop' }, function (err) { |
| 80 | + t.error(err) |
| 81 | + db.close(t.error.bind(t)) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +test('test approximateSize()', function (t) { |
| 87 | + var data = Array.apply(null, Array(10000)).map(function () { |
| 88 | + return 'aaaaaaaaaa' |
| 89 | + }).join('') |
| 90 | + |
| 91 | + db.batch(Array.apply(null, Array(10)).map(function (x, i) { |
| 92 | + return { type: 'put', key: 'foo' + i, value: data } |
| 93 | + }), function (err) { |
| 94 | + t.error(err) |
| 95 | + |
| 96 | + // cycle open/close to ensure a pack to .sst |
| 97 | + |
| 98 | + db.close(function (err) { |
| 99 | + t.error(err) |
| 100 | + |
| 101 | + db.open(function (err) { |
| 102 | + t.error(err) |
| 103 | + |
| 104 | + db.approximateSize('!', '~', function (err, size) { |
| 105 | + t.error(err) |
| 106 | + |
| 107 | + t.equal(typeof size, 'number') |
| 108 | + // account for snappy compression, original would be ~100000 |
| 109 | + t.ok(size > 40000, 'size reports a reasonable amount (' + size + ')') |
| 110 | + |
| 111 | + db.close(function (err) { |
| 112 | + t.error(err) |
| 113 | + t.end() |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +test('tearDown', function (t) { |
| 122 | + db.close(testCommon.tearDown.bind(null, t)) |
| 123 | +}) |
0 commit comments