UNPKG

3.4 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
16var destBuffer = new Buffer(8192)
17function testDestBuffer(t, err, len, expected, offset) {
18 t.error(err)
19 t.equal(len, expected.length)
20 if (typeof offset !== 'number') offset = 0
21 result = destBuffer.toString(null, offset, offset+expected.length)
22 t.equal(result, expected)
23 destBuffer.fill(0, offset, offset+expected.length)
24}
25module.exports.getBuffer = function (test) {
26 test('test simple Setup', function (t) {
27 db.put('foo', 'bar', function (err) {
28 t.error(err)
29 t.end()
30 })
31 })
32 test('test simple getBuffer() with default options', function (t) {
33 db.getBuffer('foo', destBuffer, function (err, len) {
34 testDestBuffer(t, err, len, 'bar')
35 t.end()
36 })
37 })
38
39 test('test simple getBuffer() with empty options', function (t) {
40 db.getBuffer('foo', destBuffer, {}, function (err, len) { // same but with {}
41 testDestBuffer(t, err, len, 'bar')
42 t.end()
43 })
44 })
45
46 test('test simple getBuffer() with offset option', function (t) {
47 db.getBuffer('foo', destBuffer, { offset: 3 }, function (err, len) {
48 testDestBuffer(t, err, len, 'bar', 3)
49 t.end()
50 })
51 })
52 test('test simple getBuffer() with no destBuffer', function (t) {
53 db.getBuffer('foo', null, function (err, len) {
54 t.error(err)
55 t.equal(len, 3)
56 t.end()
57 })
58 })
59 test('test simple getBuffer() with value truncated', function (t) {
60 db.getBuffer('foo', destBuffer, {offset: destBuffer.length-1}, function (err, len) {
61 testDestBuffer(t, err, len, 'b', destBuffer.length-1)
62 t.end()
63 })
64 })
65
66 test('test simultaniously get()', function (t) {
67 db.put('hello', 'world', function (err) {
68 t.error(err)
69 var r = 0
70 , done = function () {
71 if (++r == 20)
72 t.end()
73 }
74 , i = 0
75 , j = 0
76
77 for (; i < 10; ++i)
78 db.getBuffer('hello', destBuffer, function(err, len) {
79 testDestBuffer(t, err, len, 'world')
80 done()
81 })
82
83 for (; j < 10; ++j)
84 db.getBuffer('not found', destBuffer, function(err, value) {
85 t.ok(err, 'should error')
86 t.ok(verifyNotFoundError(err), 'should have correct error message')
87 t.ok(typeof value == 'undefined', 'value is undefined')
88 done()
89 })
90 })
91 })
92}
93
94module.exports.tearDown = function (test, testCommon) {
95 test('tearDown', function (t) {
96 db.close(testCommon.tearDown.bind(null, t))
97 })
98}
99
100module.exports.sync = function (test) {
101 test('sync', function (t) {
102 if (db._getBufferSync) {
103 delete db.__proto__._getBuffer
104 }
105 t.end()
106 })
107}
108
109module.exports.all = function (NoSqlDatabase, test, testCommon) {
110 module.exports.setUp(NoSqlDatabase, test, testCommon)
111 module.exports.args(test)
112 module.exports.getBuffer(test)
113 if (NoSqlDatabase.prototype._getBufferSync) {
114 module.exports.sync(test)
115 module.exports.getBuffer(test)
116 }
117 module.exports.tearDown(test, testCommon)
118}