UNPKG

5.57 kBJavaScriptView Raw
1var db
2 , verifyNotFoundError = require('./util').verifyNotFoundError
3 , isTypedArray = require('./util').isTypedArray
4
5module.exports.setUp = function (NoSqlDatabase, test, testCommon) {
6 test('setUp common', testCommon.setUp)
7 test('setUp db', function (t) {
8 db = NoSqlDatabase(testCommon.location())
9 db.open(t.end.bind(t))
10 })
11}
12
13module.exports.args = function (test) {
14 test('test batch() with missing `value`', function (t) {
15 db.batch([{ type: 'put', key: 'foo1' }], function (err) {
16 t.error(err)
17 t.end()
18 })
19 })
20
21 test('test batch() with null `value`', function (t) {
22 db.batch([{ type: 'put', key: 'foo1', value: null }], function (err) {
23 t.error(err)
24 t.end()
25 })
26 })
27
28 test('test batch() with missing `key`', function (t) {
29 db.batch([{ type: 'put', value: 'foo1' }], function (err) {
30 t.ok(err, 'got error')
31 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
32 t.end()
33 })
34 })
35
36 test('test batch() with null `key`', function (t) {
37 db.batch([{ type: 'put', key: null, value: 'foo1' }], function (err) {
38 t.ok(err, 'got error')
39 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
40 t.end()
41 })
42 })
43
44 test('test batch() with missing `key` and `value`', function (t) {
45 db.batch([{ type: 'put' }], function (err) {
46 t.ok(err, 'got error')
47 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
48 t.end()
49 })
50 })
51
52 test('test batch() with missing array', function (t) {
53 db.batch(function (err) {
54 t.ok(err, 'got error')
55 t.equal(err.message, 'batch(array) requires an array argument', 'correct error message')
56 t.end()
57 })
58 })
59
60 test('test batch() with undefined array', function (t) {
61 db.batch(void 0, function (err) {
62 t.ok(err, 'got error')
63 t.equal(err.message, 'batch(array) requires an array argument', 'correct error message')
64 t.end()
65 })
66 })
67
68 test('test batch() with null array', function (t) {
69 db.batch(null, function (err) {
70 t.ok(err, 'got error')
71 t.equal(err.message, 'batch(array) requires an array argument', 'correct error message')
72 t.end()
73 })
74 })
75
76 test('test batch() with null options', function (t) {
77 db.batch([], null, function (err) {
78 t.error(err)
79 t.end()
80 })
81 })
82}
83
84module.exports.batch = function (test) {
85 test('test batch() with empty array', function (t) {
86 db.batch([], function (err) {
87 t.error(err)
88 t.end()
89 })
90 })
91
92 test('test simple batch()', function (t) {
93 db.batch([{ type: 'put', key: 'foo', value: 'bar' }], function (err) {
94 t.error(err)
95
96 db.get('foo', function (err, result) {
97 t.error(err)
98 /*
99 var result
100 if (isTypedArray(value)) {
101 result = String.fromCharCode.apply(null, new Uint16Array(value))
102 } else {
103 t.ok(typeof Buffer != 'undefined' && value instanceof Buffer)
104 result = value.toString()
105 }*/
106 t.equal(result, 'bar')
107 t.end()
108 })
109 })
110 })
111
112 test('test multiple batch()', function (t) {
113 db.batch([
114 { type: 'put', key: 'foobatch1', value: 'bar1' }
115 , { type: 'put', key: 'foobatch2', value: 'bar2' }
116 , { type: 'put', key: 'foobatch3', value: 'bar3' }
117 , { type: 'del', key: 'foobatch2' }
118 ], function (err) {
119 t.error(err)
120
121 var r = 0
122 , done = function () {
123 if (++r == 3)
124 t.end()
125 }
126
127 db.get('foobatch1', function (err, result) {
128 t.error(err)
129 /*
130 var result
131 if (isTypedArray(value)) {
132 result = String.fromCharCode.apply(null, new Uint16Array(value))
133 } else {
134 t.ok(typeof Buffer != 'undefined' && value instanceof Buffer)
135 result = value.toString()
136 }*/
137 t.equal(result, 'bar1')
138 done()
139 })
140
141 db.get('foobatch2', function (err, value) {
142 t.ok(err, 'entry not found')
143 t.ok(typeof value == 'undefined', 'value is undefined')
144 t.ok(verifyNotFoundError(err), 'NotFound error')
145 done()
146 })
147
148 db.get('foobatch3', function (err, result) {
149 t.error(err)
150 t.equal(result, 'bar3')
151 done()
152 })
153 })
154 })
155}
156module.exports.atomic = function (test) {
157 test('test multiple batch()', function (t) {
158 t.plan(3)
159 db.batch([
160 { type: 'put', key: 'foobah1', value: 'bar1' }
161 , { type: 'put', value: 'bar2' }
162 , { type: 'put', key: 'foobah3', value: 'bar3' }
163 ], function (err) {
164 t.ok(err, 'should error')
165 db.get('foobah1', function (err) {
166 t.ok(err, 'should not be found')
167 })
168 db.get('foobah3', function (err) {
169 t.ok(err, 'should not be found')
170 })
171 })
172 })
173}
174module.exports.tearDown = function (test, testCommon) {
175 test('tearDown', function (t) {
176 db.close(testCommon.tearDown.bind(null, t))
177 })
178}
179
180module.exports.sync = function (test) {
181 test('sync', function (t) {
182 if (db._batchSync) {
183 delete db.__proto__._batch
184 }
185 t.end()
186 })
187}
188
189module.exports.all = function (NoSqlDatabase, test, testCommon) {
190 module.exports.setUp(NoSqlDatabase, test, testCommon)
191 module.exports.args(test)
192 module.exports.batch(test)
193 module.exports.atomic(test)
194 if (NoSqlDatabase.prototype._batchSync) {
195 module.exports.sync(test)
196 module.exports.batch(test)
197 module.exports.atomic(test)
198 }
199 module.exports.tearDown(test, testCommon)
200}