-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
197 lines (163 loc) · 5.56 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
const test = require('tape')
const encode = require('encoding-down')
const levelup = require('levelup')
const compose = require('.')
const packager = function (down) {
return compose(down, encode, levelup)
}
// Tests copied from level-packager
test('packager - Level constructor has access to levelup errors', function (t) {
function Down () {}
t.ok(packager(Down).errors, '.errors property set on constructor')
t.end()
})
test('packager - Level constructor relays .destroy if it exists', function (t) {
t.plan(2)
function Down () {}
function callback () {}
Down.destroy = function (location, cb) {
t.is(location, 'location', 'location is correct')
t.is(cb, callback, 'cb is set')
}
// Unlike level-packager, compose does not add a fallback callback (Level/packager#86)
packager(Down).destroy('location', callback)
})
test('packager - Level constructor relays .repair if it exists', function (t) {
t.plan(2)
function Down () {}
function callback () {}
Down.repair = function (location, cb) {
t.is(location, 'location', 'location is correct')
t.is(cb, callback, 'cb is set')
}
// Unlike level-packager, compose does not add a fallback callback (Level/packager#86)
packager(Down).repair('location', callback)
})
test('packager - Level constructor with default options', function (t) {
t.plan(3)
function Down (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {}
}
}
const levelup = packager(Down)('location')
// In level-packager, this works because encoding-down mutates the shared
// options object. That level-packager test should be updated.
// t.is(levelup.options.keyEncoding, 'utf8')
// t.is(levelup.options.valueEncoding, 'utf8')
t.is(levelup._db.codec.opts.keyEncoding, 'utf8')
t.is(levelup._db.codec.opts.valueEncoding, 'utf8')
})
test('packager - Level constructor with callback', function (t) {
t.plan(4)
function Down (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {
t.pass('open called')
process.nextTick(cb)
}
}
}
packager(Down)('location', function (err, db) {
t.error(err)
t.ok(db, 'db set in callback')
})
})
test('packager - Level constructor with custom options', function (t) {
t.plan(3)
const Down = function (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {}
}
}
const levelup = packager(Down)('location', {
keyEncoding: 'binary',
valueEncoding: 'binary'
})
t.is(levelup._db.codec.opts.keyEncoding, 'binary')
t.is(levelup._db.codec.opts.valueEncoding, 'binary')
})
// Own tests
// TODO: write more
test('passes defaults only to preceding layer', function (t) {
t.plan(12)
compose(negative, positive, { test: true }, negative)('test-location')
compose([negative, positive, { test: true }, negative])('test-location')
compose().use([negative, positive, { test: true }, negative])('test-location')
compose().use(negative).use(positive, { test: true }).use(negative)('test-location')
function negative (dbOrLocation, options) {
t.same(options, {}, 'did not get defaults')
return dbOrLocation
}
function positive (dbOrLocation, options) {
t.same(options, { test: true }, 'did get defaults')
return dbOrLocation
}
})
test('passes defaults only to preceding layers (in array)', function (t) {
t.plan(4)
compose(negative, [positive, positive], { test: true }, negative)('test-location')
function negative (dbOrLocation, options) {
t.same(options, {}, 'did not get defaults')
return dbOrLocation
}
function positive (dbOrLocation, options) {
t.same(options, { test: true }, 'did get defaults')
return dbOrLocation
}
})
test('merges defaults into preceding layers', function (t) {
t.plan(9)
const objects = { a: { a: 1 }, b: { b: 2 } }
compose().use([b, [ab, ab], objects.a, b], objects.b)('test-location')
compose().use(none).use([a, a], objects.a).use(b, objects.b)('test-location')
t.same(objects, { a: { a: 1 }, b: { b: 2 } }, 'did not mutate original objects')
function a (dbOrLocation, options) {
t.same(options, { a: 1 }, 'only a')
return dbOrLocation
}
function b (dbOrLocation, options) {
t.same(options, { b: 2 }, 'only b')
return dbOrLocation
}
function ab (dbOrLocation, options) {
t.same(options, { a: 1, b: 2 }, 'a and b')
return dbOrLocation
}
function none (dbOrLocation, options) {
t.same(options, {}, 'none')
return dbOrLocation
}
})
test('passes defaults to abstract db.open() if callback is provided', function (t) {
t.plan(20)
compose(lastLayer, { test: true })('test-location', onOpen)
compose([firstLayer, lastLayer], { test: true })('test-location', onOpen)
compose([[firstLayer, lastLayer], { test: false }], { test: true })('test-location', onOpen)
function firstLayer (location, options, callback) {
t.is(callback, undefined, 'only last layer receives callback')
return location
}
function lastLayer (location, options, callback) {
t.is(location, 'test-location', 'got location')
t.same(options, { test: true }, 'got options')
t.is(callback, onOpen, 'got callback')
// Mock abstract-leveldown
return {
status: 'new',
mocked: true,
open: function (options, callback) {
t.same(options, { test: true }, 'got open() options')
process.nextTick(callback)
}
}
}
function onOpen (err, db) {
t.ifError(err, 'no open error')
t.is(db.mocked, true, 'got db')
}
})