forked from Level/packager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
104 lines (91 loc) · 2.91 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
const fs = require('fs')
, path = require('path')
var location = path.join(__dirname, 'level-test-' + process.pid + '.db')
module.exports = function (test, level, options) {
options = options || {}
test('test db open and use, level(location, cb)', function (t) {
level(location, function (err, db) {
t.notOk(err, 'no error')
db.put('test1', 'success', function (err) {
t.notOk(err, 'no error')
db.close(t.end.bind(t))
})
})
})
test('test db open and use, level(location, options, cb)', function (t) {
level(location, { createIfMissing: false, errorIfExists: false }, function (err, db) {
t.notOk(err, 'no error')
db.put('test2', 'success', function (err) {
t.notOk(err, 'no error')
db.close(t.end.bind(t))
})
})
})
if (!options.skipErrorIfExistsTest) {
// should use existing options object
test('test db open and use, level(location, options, cb) force error', function (t) {
level(location, { errorIfExists: true }, function (err, db) {
t.ok(err, 'got error opening existing db')
t.notOk(db, 'no db')
t.end()
})
})
}
test('test db open and use, db=level(location)', function (t) {
var db = level(location)
db.put('test3', 'success', function (err) {
t.notOk(err, 'no error')
db.close(t.end.bind(t))
})
})
test('test db values', function (t) {
var c = 0
, db = level(location)
, setup = options.nonPersistent
? function (callback) {
db.batch([
{ type: 'put', key: 'test1', value: 'success' }
, { type: 'put', key: 'test2', value: 'success' }
, { type: 'put', key: 'test3', value: 'success' }
], callback)
}
: function (callback) { callback() }
function read (err, value) {
t.notOk(err, 'no error')
t.equal(value, 'success')
if (++c == 3)
db.close(t.end.bind(t))
}
setup(function (err) {
t.notOk(err, 'no error')
db.get('test1', read)
db.get('test2', read)
db.get('test3', read)
})
})
if (!options.skipRepairTest) {
test('test repair', function (t) {
t.plan(1)
level.repair(location, function (err) {
t.notOk(err, 'no error')
})
})
}
if (!options.skipDestroyTest) {
test('test destroy', function (t) {
t.plan(4)
t.ok(fs.statSync(location).isDirectory(), 'sanity check, directory exists')
t.ok(fs.existsSync(path.join(location, 'LOG')), 'sanity check, log exists')
level.destroy(location, function (err) {
t.notOk(err, 'no error')
t.notOk(fs.existsSync(path.join(location, 'LOG')), 'db gone (mostly)')
})
})
}
}
if (!module.parent) {
const test = require('tape')
, packager = require('./')
, leveldown = require('leveldown')
module.exports(test, packager(leveldown))
}