-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (44 loc) · 1.33 KB
/
index.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
var LevelUp = require('level-test')
var xtend = require('xtend')
var rmrf = require('rimraf')
module.exports = function (opts, test) {
if (typeof opts == 'function') {
test = opts
opts = {}
}
throwIfNotFunction(test, 'invalid test function')
return function (desc, _opts, callback) {
if (typeof _opts == 'function') {
callback = _opts
_opts = {}
}
_opts = xtend(opts, _opts)
throwIfNotFunction(callback, 'missing test callback')
test(desc, function (t) {
throwIfNotFunction(t.end, 'missing t.end')
throwIfNotFunction(t.ok, 'missing t.ok')
var dbName = 'level-test-' + Date.now()
var levelup = LevelUp(opts)
levelup(dbName, _opts, function (err, db) {
t.ok(!err, 'no error on open()')
t.ok(db, 'valid db object')
var location = db.location
var end = t.end
t.end = function () {
db.close(function (err) {
t.ok(!err, 'no error on close()')
rmrf(location, function (err) {
t.ok(!err, 'db removed')
end.call(t)
})
})
}
// Now everything is setup to test the db!
callback(t, db, db.createReadStream.bind(db))
})
})
}
}
function throwIfNotFunction(fn, msg) {
if (typeof fn != 'function') throw new Error(msg)
}