UNPKG

3.35 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}
15
16module.exports.get = function (test) {
17 test('test simple get()', function (t) {
18 db.put('foo', 'bar', function (err) {
19 t.error(err)
20 db.get('foo', function (err, value) {
21 t.error(err)
22 t.ok(typeof value !== 'string', 'should not be string by default')
23
24 var result
25 if (isTypedArray(value)) {
26 result = String.fromCharCode.apply(null, new Uint16Array(value))
27 } else {
28 t.ok(typeof Buffer != 'undefined' && value instanceof Buffer)
29 try {
30 result = value.toString()
31 } catch (e) {
32 t.error(e, 'should not throw when converting value to a string')
33 }
34 }
35
36 t.equal(result, 'bar')
37
38 db.get('foo', {}, function (err, value) { // same but with {}
39 t.error(err)
40 t.ok(typeof value !== 'string', 'should not be string by default')
41
42 var result
43 if (isTypedArray(value)) {
44 result = String.fromCharCode.apply(null, new Uint16Array(value))
45 } else {
46 t.ok(typeof Buffer != 'undefined' && value instanceof Buffer)
47 try {
48 result = value.toString()
49 } catch (e) {
50 t.error(e, 'should not throw when converting value to a string')
51 }
52 }
53
54 t.equal(result, 'bar')
55
56 db.get('foo', { asBuffer: false }, function (err, value) {
57 t.error(err)
58 t.ok(typeof value === 'string', 'should be string if not buffer')
59 t.equal(value, 'bar')
60 t.end()
61 })
62 })
63 })
64 })
65 })
66
67 test('test simultaniously get()', function (t) {
68 db.put('hello', 'world', function (err) {
69 t.error(err)
70 var r = 0
71 , done = function () {
72 if (++r == 20)
73 t.end()
74 }
75 , i = 0
76 , j = 0
77
78 for (; i < 10; ++i)
79 db.get('hello', function(err, value) {
80 t.error(err)
81 t.equal(value.toString(), 'world')
82 done()
83 })
84
85 for (; j < 10; ++j)
86 db.get('not found', function(err, value) {
87 t.ok(err, 'should error')
88 t.ok(verifyNotFoundError(err), 'should have correct error message')
89 t.ok(typeof value == 'undefined', 'value is undefined')
90 done()
91 })
92 })
93 })
94}
95
96module.exports.tearDown = function (test, testCommon) {
97 test('tearDown', function (t) {
98 db.close(testCommon.tearDown.bind(null, t))
99 })
100}
101
102module.exports.sync = function (test) {
103 test('sync', function (t) {
104 if (db._getSync) {
105 delete db.__proto__._get
106 }
107 t.end()
108 })
109}
110
111module.exports.all = function (NoSqlDatabase, test, testCommon) {
112 module.exports.setUp(NoSqlDatabase, test, testCommon)
113 module.exports.args(test)
114 module.exports.get(test)
115 if (NoSqlDatabase.prototype._getSync) {
116 module.exports.sync(test)
117 module.exports.get(test)
118 }
119 module.exports.tearDown(test, testCommon)
120}