This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
173 lines (141 loc) · 4.15 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const test = require('tap').test
, rimraf = require('rimraf')
, levelup = require('levelup')
, multiply = require('./')
var ltest = function (name, fn) {
test(name, function (t) {
var location = '__multiply-' + Math.random()
, db
t._end = t.end
t.end = function () {
db.close(function (err) {
t.notOk(err, 'no error on close()')
rimraf(location, t._end.bind(t))
})
}
levelup(location, function (err, _db) {
t.notOk(err, 'no error on open()')
db = multiply(_db)
fn(db, t)
})
})
}
// test that the standard API is working as it should
ltest('test singular get()', function (db, t) {
t.throws(db.get.bind(db))
db.get('foo', function (err) {
t.ok(err, 'returned error')
t.equal(err.name, 'NotFoundError', 'correct error type')
t.end()
})
})
ltest('test singular put() & get()', function (db, t) {
t.throws(db.put.bind(db))
db.put('foo', 'bar', function (err) {
t.notOk(err, 'no error')
db.get('foo', function (err, value) {
t.notOk(err, 'no error')
t.equal(value, 'bar', 'got expected value')
t.end()
})
})
})
ltest('test singular del()', function (db, t) {
t.throws(db.del.bind(db))
db.del('foo', function (err) {
t.notOk(err, 'no error')
t.end()
})
})
ltest('test multiple get()', function (db, t) {
db.batch([
{ type: 'put', key: 'foo1', value: 'foovalue1' }
, { type: 'put', key: 'foo2', value: 'foovalue2' }
, { type: 'put', key: 'foo3', value: 'foovalue3' }
, { type: 'put', key: 'foo4', value: 'foovalue4' }
, { type: 'put', key: 'foo5', value: 'foovalue5' }
], function (err) {
t.notOk(err, 'no error')
db.get([ 'foo2', 'foo4', 'foo6' ], function (err, data) {
t.notOk(err, 'no error')
t.deepEqual(data, {
foo2: 'foovalue2'
, foo4: 'foovalue4'
, foo6: null
}, 'correct data')
t.end()
})
})
})
ltest('test multiple put()', function (db, t) {
db.put({
foo1: 'foovalue1'
, foo2: 'foovalue2'
, foo3: 'foovalue3'
, foo4: 'foovalue4'
, foo5: 'foovalue5'
}, function (err) {
t.notOk(err, 'no error')
db.get([ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ], function (err, data) {
t.notOk(err, 'no error')
t.deepEqual(data, {
foo1: 'foovalue1'
, foo2: 'foovalue2'
, foo3: 'foovalue3'
, foo4: 'foovalue4'
, foo5: 'foovalue5'
}, 'correct data')
t.end()
})
})
})
ltest('test multiple del()', function (db, t) {
db.batch([
{ type: 'put', key: 'foo1', value: 'foovalue1' }
, { type: 'put', key: 'foo2', value: 'foovalue2' }
, { type: 'put', key: 'foo3', value: 'foovalue3' }
, { type: 'put', key: 'foo4', value: 'foovalue4' }
, { type: 'put', key: 'foo5', value: 'foovalue5' }
], function (err) {
t.notOk(err, 'no error')
db.del([ 'foo1', 'foo3', 'foo5' ], function (err) {
t.notOk(err, 'no error')
db.get([ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ], function (err, data) {
t.notOk(err, 'no error')
t.deepEqual(data, {
foo1: null
, foo2: 'foovalue2'
, foo3: null
, foo4: 'foovalue4'
, foo5: null
}, 'correct data')
t.end()
})
})
})
})
test('test prefixed methods', function (t) {
var location = '__multiply-' + Math.random()
levelup(location, function (err, db) {
t.notOk(err, 'no error on open()')
var orig = {
get: db.get
, put: db.put
, del: db.del
}
t.notOk(db.multiget, 'multiget() not defined')
t.notOk(db.multiput, 'multiput() not defined')
t.notOk(db.multidel, 'multidel() not defined')
db = multiply(db, 'multi')
t.equal(db.get, orig.get, 'get() not replaced')
t.equal(db.put, orig.put, 'put() not replaced')
t.equal(db.del, orig.del, 'del() not replaced')
t.type(db.multiget, 'function', 'multiget() defined')
t.type(db.multiput, 'function', 'multiput() defined')
t.type(db.multidel, 'function', 'multidel() defined')
db.close(function (err) {
t.notOk(err, 'no error on close()')
rimraf(location, t.end.bind(t))
})
})
})