1 |
|
2 |
|
3 |
|
4 | var db
|
5 | , testBuffer
|
6 | , test
|
7 | , verifyNotFoundError = require('./util').verifyNotFoundError
|
8 |
|
9 | function makeGetDelErrorTests (type, key, expectedError) {
|
10 | test('test get() with ' + type + ' causes error', function (t) {
|
11 | db.get(key, function (err) {
|
12 | t.ok(err, 'has error')
|
13 | t.ok(err instanceof Error)
|
14 | t.ok(err.message.match(expectedError), 'correct error message')
|
15 | t.end()
|
16 | })
|
17 | })
|
18 |
|
19 | test('test del() with ' + type + ' causes error', function (t) {
|
20 | db.del(key, function (err) {
|
21 | t.ok(err, 'has error')
|
22 | t.ok(err instanceof Error)
|
23 | t.ok(err.message.match(expectedError), 'correct error message')
|
24 | t.end()
|
25 | })
|
26 | })
|
27 | }
|
28 |
|
29 | function makePutErrorTest (type, key, value, expectedError) {
|
30 | test('test put() with ' + type + ' causes error', function (t) {
|
31 | db.put(key, value, function (err) {
|
32 | t.ok(err, 'has error')
|
33 | t.ok(err instanceof Error)
|
34 | t.ok(err.message.match(expectedError), 'correct error message')
|
35 | t.end()
|
36 | })
|
37 | })
|
38 | }
|
39 |
|
40 | function makePutGetDelSuccessfulTest (type, key, value, expectedResult) {
|
41 | var hasExpectedResult = arguments.length == 4
|
42 | test('test put()/get()/del() with ' + type, function (t) {
|
43 | db.put(key, value, function (err) {
|
44 | t.error(err)
|
45 | db.get(key, function (err, _value) {
|
46 | t.error(err, 'no error, has key/value for `' + type + '`')
|
47 |
|
48 | var result = _value
|
49 | if (hasExpectedResult) {
|
50 | t.ok(result === expectedResult, 'got `' + expectedResult + '`')
|
51 | } else {
|
52 | if (result != null)
|
53 | result = _value.toString()
|
54 | if (value != null)
|
55 | value = value.toString()
|
56 | t.equals(result, value)
|
57 | }
|
58 | db.del(key, function (err) {
|
59 | t.error(err, 'no error, deleted key/value for `' + type + '`')
|
60 | db.get(key, function (err, value) {
|
61 | t.ok(err, 'entry propertly deleted')
|
62 | t.ok(verifyNotFoundError(err), 'should have correct error message')
|
63 | t.ok(typeof value == 'undefined', 'value is undefined')
|
64 | t.end()
|
65 | })
|
66 | })
|
67 | })
|
68 | })
|
69 | })
|
70 | }
|
71 |
|
72 | function makeErrorKeyTest (type, key, expectedError) {
|
73 | makeGetDelErrorTests(type, key, expectedError)
|
74 | makePutErrorTest(type, key, 'foo', expectedError)
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | module.exports.setUp = function (NoSqlDatabase, test, testCommon) {
|
80 | test('setUp common', testCommon.setUp)
|
81 | test('setUp db', function (t) {
|
82 | db = NoSqlDatabase(testCommon.location())
|
83 | db.open(t.end.bind(t))
|
84 | })
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | module.exports.errorKeys = function (testFunc, BufferType) {
|
90 | if (!BufferType)
|
91 | BufferType = Buffer
|
92 | test = testFunc
|
93 | makeErrorKeyTest('null key', null, /key cannot be `null` or `undefined`/)
|
94 | makeErrorKeyTest('undefined key', undefined, /key cannot be `null` or `undefined`/)
|
95 | makeErrorKeyTest('empty String key', '', /key cannot be an empty String/)
|
96 | makeErrorKeyTest('empty Buffer key', new BufferType(0), /key cannot be an empty \w*Buffer/)
|
97 | makeErrorKeyTest('empty Array key', [], /key cannot be an empty String/)
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | module.exports.nonErrorKeys = function (testFunc) {
|
103 |
|
104 | test = testFunc
|
105 | makePutGetDelSuccessfulTest('`false` key', false, 'foo false')
|
106 | makePutGetDelSuccessfulTest('`0` key', 0, 'foo 0')
|
107 | makePutGetDelSuccessfulTest('`NaN` key', NaN, 'foo NaN')
|
108 |
|
109 |
|
110 | makePutGetDelSuccessfulTest(
|
111 | 'long String key'
|
112 | , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!'
|
113 | , 'foo'
|
114 | )
|
115 |
|
116 | if (!process.browser) {
|
117 |
|
118 | makePutGetDelSuccessfulTest('Buffer key', testBuffer, 'foo')
|
119 | }
|
120 |
|
121 |
|
122 | makePutGetDelSuccessfulTest('Array value', 'foo', [1,2,3,4])
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | module.exports.errorValues = function () {
|
128 | }
|
129 |
|
130 | module.exports.nonErrorValues = function (testFunc, BufferType) {
|
131 | if (!BufferType) BufferType = Buffer
|
132 |
|
133 | test = testFunc
|
134 | makePutGetDelSuccessfulTest('`false` value', 'foo false', false)
|
135 | makePutGetDelSuccessfulTest('`0` value', 'foo 0', 0)
|
136 | makePutGetDelSuccessfulTest('`NaN` value', 'foo NaN', NaN)
|
137 |
|
138 |
|
139 |
|
140 | makePutGetDelSuccessfulTest('`null` value', 'foo null', null, '')
|
141 | makePutGetDelSuccessfulTest('`undefined` value', 'foo undefined', undefined, '')
|
142 | makePutGetDelSuccessfulTest('empty String value', 'foo', '', '')
|
143 | makePutGetDelSuccessfulTest('empty Buffer value', 'foo', new BufferType(0), '')
|
144 | makePutGetDelSuccessfulTest('empty Array value', 'foo', [], '')
|
145 |
|
146 |
|
147 | makePutGetDelSuccessfulTest(
|
148 | 'long String value'
|
149 | , 'foo'
|
150 | , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!'
|
151 | )
|
152 |
|
153 |
|
154 | makePutGetDelSuccessfulTest('Buffer value', 'foo', testBuffer)
|
155 |
|
156 |
|
157 | makePutGetDelSuccessfulTest('Array key', [1,2,3,4], 'foo')
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 | module.exports.tearDown = function (test, testCommon) {
|
163 | test('tearDown', function (t) {
|
164 | db.close(testCommon.tearDown.bind(null, t))
|
165 | })
|
166 | }
|
167 |
|
168 | module.exports.all = function (NoSqlDatabase, testFunc, testCommon, buffer, BufferType) {
|
169 | testBuffer = buffer
|
170 | test = testFunc
|
171 | module.exports.setUp(NoSqlDatabase, test, testCommon)
|
172 | module.exports.errorKeys(test, BufferType)
|
173 | module.exports.nonErrorKeys(test)
|
174 | module.exports.errorValues(test, BufferType)
|
175 | module.exports.nonErrorKeys(test)
|
176 | module.exports.tearDown(test, testCommon)
|
177 | }
|