UNPKG

5.71 kBJavaScriptView Raw
1var db
2
3module.exports.setUp = function (leveldown, test, testCommon) {
4 test('setUp common', testCommon.setUp)
5 test('setUp db', function (t) {
6 db = leveldown(testCommon.location())
7 db.open(t.end.bind(t))
8 })
9}
10
11module.exports.args = function (test) {
12 test('test batch#put() with missing `value`', function (t) {
13 db.batch().put('foo1')
14 t.end()
15 })
16
17 test('test batch#put() with null `value`', function (t) {
18 db.batch().put('foo1', null)
19 t.end()
20 })
21
22 test('test batch#put() with missing `key`', function (t) {
23 try {
24 db.batch().put(undefined, 'foo1')
25 } catch (err) {
26 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
27 return t.end()
28 }
29 t.fail('should have thrown')
30 t.end()
31 })
32
33 test('test batch#put() with null `key`', function (t) {
34 try {
35 db.batch().put(null, 'foo1')
36 } catch (err) {
37 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
38 return t.end()
39 }
40 t.fail('should have thrown')
41 t.end()
42 })
43
44 test('test batch#put() with missing `key` and `value`', function (t) {
45 try {
46 db.batch().put()
47 } catch (err) {
48 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
49 return t.end()
50 }
51 t.fail('should have thrown')
52 t.end()
53 })
54
55 test('test batch#del() with missing `key`', function (t) {
56 try {
57 db.batch().del()
58 } catch (err) {
59 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
60 return t.end()
61 }
62 t.fail('should have thrown')
63 t.end()
64 })
65
66 test('test batch#del() with null `key`', function (t) {
67 try {
68 db.batch().del(null)
69 } catch (err) {
70 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
71 return t.end()
72 }
73 t.fail('should have thrown')
74 t.end()
75 })
76
77 test('test batch#del() with null `key`', function (t) {
78 try {
79 db.batch().del(null)
80 } catch (err) {
81 t.equal(err.message, 'key cannot be `null` or `undefined`', 'correct error message')
82 return t.end()
83 }
84 t.fail('should have thrown')
85 t.end()
86 })
87
88 test('test batch#clear() doesn\'t throw', function (t) {
89 db.batch().clear()
90 t.end()
91 })
92
93 test('test batch#write() with no callback', function (t) {
94 try {
95 db.batch().write()
96 } catch (err) {
97 t.equal(err.message, 'write() requires a callback argument', 'correct error message')
98 return t.end()
99 }
100 t.fail('should have thrown')
101 t.end()
102 })
103
104 test('test batch#put() after write()', function (t) {
105 var batch = db.batch().put('foo', 'bar')
106 batch.write(function () {})
107 try {
108 batch.put('boom', 'bang')
109 } catch (err) {
110 t.equal(err.message, 'write() already called on this batch', 'correct error message')
111 return t.end()
112 }
113 t.fail('should have thrown')
114 t.end()
115 })
116
117 test('test batch#del() after write()', function (t) {
118 var batch = db.batch().put('foo', 'bar')
119 batch.write(function () {})
120 try {
121 batch.del('foo')
122 } catch (err) {
123 t.equal(err.message, 'write() already called on this batch', 'correct error message')
124 return t.end()
125 }
126 t.fail('should have thrown')
127 t.end()
128 })
129
130 test('test batch#clear() after write()', function (t) {
131 var batch = db.batch().put('foo', 'bar')
132 batch.write(function () {})
133 try {
134 batch.clear()
135 } catch (err) {
136 t.equal(err.message, 'write() already called on this batch', 'correct error message')
137 return t.end()
138 }
139 t.fail('should have thrown')
140 t.end()
141 })
142
143 test('test batch#write() after write()', function (t) {
144 var batch = db.batch().put('foo', 'bar')
145 batch.write(function () {})
146 try {
147 batch.write(function (err) {})
148 } catch (err) {
149 t.equal(err.message, 'write() already called on this batch', 'correct error message')
150 return t.end()
151 }
152 t.fail('should have thrown')
153 t.end()
154 })
155}
156
157module.exports.batch = function (test, testCommon) {
158 test('test basic batch', function (t) {
159 db.batch(
160 [
161 { type: 'put', key: 'one', value: '1' }
162 , { type: 'put', key: 'two', value: '2' }
163 , { type: 'put', key: 'three', value: '3' }
164 ]
165 , function (err) {
166 t.error(err)
167
168 db.batch()
169 .put('1', 'one')
170 .del('2', 'two')
171 .put('3', 'three')
172 .clear()
173 .put('one', 'I')
174 .put('two', 'II')
175 .del('three')
176 .put('foo', 'bar')
177 .write(function (err) {
178 t.error(err)
179 testCommon.collectEntries(
180 db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
181 , function (err, data) {
182 t.error(err)
183 t.equal(data.length, 3, 'correct number of entries')
184 var expected = [
185 { key: 'foo', value: 'bar' }
186 , { key: 'one', value: 'I' }
187 , { key: 'two', value: 'II' }
188 ]
189 t.deepEqual(data, expected)
190 t.end()
191 }
192 )
193 })
194 }
195 )
196 })
197}
198
199module.exports.tearDown = function (test, testCommon) {
200 test('tearDown', function (t) {
201 db.close(testCommon.tearDown.bind(null, t))
202 })
203}
204
205module.exports.all = function (leveldown, test, testCommon) {
206 module.exports.setUp(leveldown, test, testCommon)
207 module.exports.args(test)
208 module.exports.batch(test, testCommon)
209 module.exports.tearDown(test, testCommon)
210}
\No newline at end of file