1 | var db
|
2 |
|
3 | module.exports.setUp = function (NoSqlDatabase, test, testCommon) {
|
4 | test('setUp common', testCommon.setUp)
|
5 | test('setUp db', function (t) {
|
6 | db = NoSqlDatabase(testCommon.location())
|
7 | db.open(t.end.bind(t))
|
8 | })
|
9 | }
|
10 |
|
11 | module.exports.args = function (test) {
|
12 | test('test argument-less approximateSize() throws', function (t) {
|
13 | t.throws(
|
14 | db.approximateSize.bind(db)
|
15 | , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback`(for async) arguments' }
|
16 | , 'no-arg approximateSize() throws'
|
17 | )
|
18 | t.end()
|
19 | })
|
20 |
|
21 | test('test callback-less, 1-arg, approximateSize() throws', function (t) {
|
22 | t.throws(
|
23 | db.approximateSize.bind(db, 'foo')
|
24 | , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback`(for async) arguments' }
|
25 | , 'callback-less, 1-arg approximateSize() throws'
|
26 | )
|
27 | t.end()
|
28 | })
|
29 |
|
30 | test('test callback-less, 3-arg, approximateSize() throws', function (t) {
|
31 | t.throws(
|
32 | db.approximateSize.bind(db, function () {})
|
33 | , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback`(for async) arguments' }
|
34 | , 'callback-only approximateSize() throws'
|
35 | )
|
36 | t.end()
|
37 | })
|
38 |
|
39 | test('test callback-only approximateSize() throws', function (t) {
|
40 | t.throws(
|
41 | db.approximateSize.bind(db, function () {})
|
42 | , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback`(for async) arguments' }
|
43 | , 'callback-only approximateSize() throws'
|
44 | )
|
45 | t.end()
|
46 | })
|
47 |
|
48 | test('test 1-arg + callback approximateSize() throws', function (t) {
|
49 | t.throws(
|
50 | db.approximateSize.bind(db, 'foo', function () {})
|
51 | , { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback`(for async) arguments' }
|
52 | , '1-arg + callback approximateSize() throws'
|
53 | )
|
54 | t.end()
|
55 | })
|
56 | }
|
57 |
|
58 | module.exports.approximateSize = function (test) {
|
59 | test('test approximateSize()', function (t) {
|
60 | var data = Array.apply(null, Array(10000)).map(function () {
|
61 | return 'aaaaaaaaaa'
|
62 | }).join('')
|
63 |
|
64 | db.batch(
|
65 | Array.apply(null, Array(10)).map(function (x, i) {
|
66 | return { type: 'put', key: 'foo' + i, value: data }
|
67 | })
|
68 | , function (err) {
|
69 | t.error(err)
|
70 |
|
71 |
|
72 |
|
73 | db.close(function (err) {
|
74 | t.error(err)
|
75 |
|
76 | db.open(function (err) {
|
77 | t.error(err)
|
78 |
|
79 | db.approximateSize('!', '~', function (err, size) {
|
80 | t.error(err)
|
81 |
|
82 | t.type(size, 'number')
|
83 | t.ok(
|
84 | size > 40000
|
85 |
|
86 | , 'size reports a reasonable amount (' + size + ')'
|
87 | )
|
88 | t.end()
|
89 |
|
90 | })
|
91 | })
|
92 | })
|
93 | }
|
94 | )
|
95 | })
|
96 | }
|
97 |
|
98 | module.exports.sync = function (test) {
|
99 | test('sync', function (t) {
|
100 | if (db._approximateSizeSync) {
|
101 | delete db.__proto__._approximateSize
|
102 | }
|
103 | t.end()
|
104 | })
|
105 | }
|
106 |
|
107 | module.exports.tearDown = function (test, testCommon) {
|
108 | test('tearDown', function (t) {
|
109 | db.close(testCommon.tearDown.bind(null, t))
|
110 | })
|
111 | }
|
112 |
|
113 | module.exports.all = function (NoSqlDatabase, test, testCommon) {
|
114 | module.exports.setUp(NoSqlDatabase, test, testCommon)
|
115 | module.exports.args(test)
|
116 | module.exports.approximateSize(test)
|
117 | if (NoSqlDatabase.prototype._approximateSizeSync) {
|
118 | module.exports.sync(test)
|
119 | module.exports.approximateSize(test)
|
120 | }
|
121 | module.exports.tearDown(test, testCommon)
|
122 | }
|