-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
166 lines (147 loc) · 4.87 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
const JSONdb = require('.');
const assert = require('chai').assert;
const fs = require('fs');
const iterableTestsCount = 100;
/**
* Generates a file path and name based on current UNIX timestamp.
* @returns {string}
*/
function genFileName() {
return '/tmp/' + Date.now().toString() + '.json';
}
/**
* Makes sure that a unique filename is generated.
*/
(function genFilePath() {
while (true) {
try {
global.filePath = genFileName();
fs.statSync(global.filePath);
} catch (err) {
break;
}
}
})();
/**
* Returns a new instance of JSONdb.
* @returns {JSONdb}
*/
function createInstance() {
return new JSONdb(global.filePath);
}
describe('Privilege', function() {
it('Tests are not being run as root', function() {
let isRoot = process.getuid && process.getuid() === 0;
if (isRoot) {
assert.fail(false, isRoot, 'Please do not run tests with root privileges!');
}
});
});
describe('Consistency', function() {
beforeEach('Database cleanup', function() {
createInstance().deleteAll();
});
it('Create a new JSONdb instance and test `instanceOf`', function() {
const db = createInstance();
assert.instanceOf(db, JSONdb);
});
it('Check error handling for paths with no access', function() {
assert.throws(function() {
const db = new JSONdb('/' + Date.now().toString() + '.json', { syncOnWrite: true });
db.set('foo', 'bar');
});
});
it('Check that a non-exhistent key returns `undefined`', function() {
const db = createInstance();
assert.typeOf(db.get(Date.now()), 'undefined', 'Unexpected type of initial read');
});
});
describe('Mechanics', function() {
beforeEach('Database cleanup', function() {
createInstance().deleteAll();
});
it('Check that values can change', function() {
const db = createInstance();
for (let i = 0; i < iterableTestsCount; i++) {
let change = { testVal: db.get('foo') };
let changeFn = function() {
db.set('foo', Math.random());
change.testVal = db.get('foo');
};
assert.changes(changeFn, change, 'testVal', 'Values do not change');
}
});
it('Check that values can change (deterministic)', function() {
const db = createInstance();
for (let i = 0; i < iterableTestsCount; i++) {
db.set('foo', new Date().toISOString());
let firstVal = db.get('foo');
db.set('foo', new Date().toUTCString());
let secondVal = db.get('foo');
assert.notEqual(firstVal, secondVal, 'Values do not change');
}
});
it('Check that keys can be deleted', function() {
const db = createInstance();
for (let i = 0; i < iterableTestsCount; i++) {
db.set('foo', Date.now());
let firstVal = db.get('foo');
db.delete('foo');
let secondVal = db.get('foo');
assert.notEqual(firstVal, secondVal, 'Values do not change');
assert.isUndefined(secondVal, 'Key was not deleted');
}
});
it('Check that keys existence can be verified', function() {
const db = createInstance();
for (let i = 0; i < iterableTestsCount; i++) {
db.set('foo', Date.now());
assert.isTrue(db.has('foo'), 'Key existence is erroneous (returned False instead of True)');
db.delete('foo');
assert.isFalse(db.has('foo'), 'Key existence is erroneous (returned True instead of False)');
}
});
it('Verify sync to disk', function() {
const db = createInstance();
db.set('foo', Date.now());
assert.doesNotThrow(db.sync, Error, 'Cannot save to disk');
});
it('Check that the copy of the underlying structure is coherent', function() {
const db = createInstance();
const reference = {};
for (let i = 0; i < iterableTestsCount; i++) {
const now = Date.now();
db.set('foo', now);
reference.foo = now;
db.set('bar', now + 1000);
reference.bar = now + 1000;
assert.equal(JSON.stringify(db.JSON()), JSON.stringify(reference), 'Returned copy does not match');
}
});
it('Check that the underlying structure can be replaced', function() {
const db = createInstance();
const replacement = {};
const now = Date.now();
db.set('foo', now);
db.set('bar', now + 1000);
replacement.foo = now - 1000;
replacement.bar = now - 2000;
assert.notEqual(JSON.stringify(db.JSON()), JSON.stringify(replacement), 'Replacement is equal');
db.JSON(replacement);
assert.equal(JSON.stringify(db.JSON()), JSON.stringify(replacement), 'Replaced structure is not equal');
});
});
describe('Persistency', function() {
let db = createInstance();
db.set('foo', Date.now());
let oldVal = db.get('foo');
db = createInstance();
assert.equal(db.get('foo'), oldVal, 'Reloaded value differs from last written');
});
describe('Cleanup', function() {
it('Temporary file removal', function() {
assert.doesNotThrow(function() {
fs.unlinkSync(global.filePath);
}, Error, 'Unable to cleanup');
})
});