/
test-api.js
138 lines (105 loc) · 3.16 KB
/
test-api.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
var config = require('./config');
path = require('path'),
fs = require('fs'),
dirty = require(config.LIB_DIRTY),
events = require('events'),
assert = require('assert');
// exists moved from path to fs in node v0.7.1
// https://raw.github.com/joyent/node/v0.7.1/ChangeLog
var exists = (fs.exists) ? fs.exists : path.exists;
function dirtyAPITests(file) {
var mode = (file) ? 'persistent' : 'transient';
describe('dirty api (' + mode + ' mode)', function() {
function cleanup(done) {
exists(file, function(doesExist) {
if (doesExist) {
fs.unlinkSync(file);
}
done();
});
}
before(cleanup);
describe('dirty constructor', function() {
var db = dirty(file);
after(cleanup);
it('is an event emitter', function() {
assert.ok(db instanceof events.EventEmitter);
});
it('is a dirty', function() {
assert.ok(db instanceof dirty);
});
});
describe('events', function() {
afterEach(cleanup);
it('should fire load', function(done) {
var db = dirty(file);
db.on('load', function(length) {
assert.strictEqual(length, 0);
done();
});
});
it('should fire drain after write', function(done) {
var db = dirty(file);
db.on('load', function(length) {
assert.strictEqual(length, 0);
db.set('key', 'value');
db.on('drain', function() {
done();
});
});
});
});
describe('accessors', function(done) {
after(cleanup);
var db;
it('.set should trigger callback', function(done) {
db = dirty(file);
db.set('key', 'value', function(err) {
assert.ok(!err);
done();
});
});
it('.get should return value', function() {
assert.strictEqual(db.get('key'), 'value');
});
it('.path is valid', function() {
assert.strictEqual(db.path, file);
});
it('.forEach runs for all', function() {
var total = 2, count = 0;
db.set('key1', 'value1');
db.set('delete', 'me');
db.rm('delete');
var keys = ['key', 'key1'];
var vals = ['value', 'value1'];
db.forEach(function(key, val) {
assert.strictEqual(key, keys[count]);
assert.strictEqual(val, vals[count]);
count ++;
});
assert.strictEqual(count, total);
});
it('.rm removes key/value pair', function() {
db.set('test', 'test');
assert.strictEqual(db.get('test'), 'test');
db.rm('test');
assert.strictEqual(db.get('test'), undefined);
});
it('will reload file from disk', function(done) {
if (!file) {
console.log('N/A in transient mode');
return done();
}
db = dirty(file);
db.on('load', function(length) {
assert.strictEqual(length, 2);
assert.strictEqual(db.get('key'), 'value');
assert.strictEqual(db.get('key1'), 'value1');
done();
});
});
});
});
}
dirtyAPITests('');
dirtyAPITests(config.TMP_PATH + '/apitest.dirty');