This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathb-table.unit.js
304 lines (263 loc) · 8.75 KB
/
b-table.unit.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
var Sbucket = require('../lib/s-bucket');
var Btable = require('../lib/b-table');
var expect = require('chai').expect;
var sinon = require('sinon');
var EventEmitter = require('events').EventEmitter;
var utils = require('../lib/utils');
var proxyquire = require('proxyquire');
describe('Btable', function() {
describe('@constructor', function() {
var _open;
before(function() {
_open = sinon.stub(Btable.prototype, '_open');
});
it('should merge options with defaults', function() {
var bTable = new Btable('some/path', { someOption: true });
expect(bTable._options.someOption).to.equal(true);
expect(bTable._options.referenceId).to.equal(null);
});
it('should coerce the table path to add extension', function() {
var bTable = new Btable('some/path');
expect(bTable._tablePath).to.equal('some/path.kfs');
});
it('should create a reference id', function() {
var bTable = new Btable('');
expect(Buffer.isBuffer(bTable._rid)).to.equal(true);
});
after(function() {
_open.restore();
});
});
describe('#_open', function() {
var _initBtableDirectory;
var _validateTablePath;
var StubbedBtable = proxyquire('../lib/b-table', {
fs: {
readFileSync: sinon.stub().returns(
utils.createReferenceId().toString('hex')
)
}
});
before(function() {
_initBtableDirectory = sinon.stub(
StubbedBtable.prototype,
'_initBtableDirectory'
);
_validateTablePath = sinon.stub(
StubbedBtable.prototype,
'_validateTablePath'
);
});
it('should initialize the db if it does not exist', function() {
/* jshint unused:false */
var _fileDoesExist = sinon.stub(utils, 'fileDoesExist').returns(false);
var bTable = new StubbedBtable('');
_fileDoesExist.restore();
setImmediate(function() {
expect(_initBtableDirectory.called).to.equal(true);
});
});
it('should validate the db if it does exist', function() {
/* jshint unused:false */
var _fileDoesExist = sinon.stub(utils, 'fileDoesExist').returns(true);
var bTable = new StubbedBtable('');
_fileDoesExist.restore();
setImmediate(function() {
expect(_validateTablePath.called).to.equal(true);
});
});
it('should throw an error if validating table path fails', function() {
/* jshint unused:false */
_validateTablePath.restore();
_validateTablePath = sinon.stub(
StubbedBtable.prototype,
'_validateTablePath'
).throws(new Error('Failed'));
var _fileDoesExist = sinon.stub(utils, 'fileDoesExist').returns(true);
expect(function() {
var bTable = new StubbedBtable('');
}).to.throw(Error, 'Failed');
_fileDoesExist.restore();
});
after(function() {
_initBtableDirectory.restore();
_validateTablePath.restore();
});
});
describe('#_initBtableDirectory', function() {
it('should throw error if mkdirp fails', function() {
var StubbedBtable = proxyquire('../lib/b-table', {
mkdirp: {
sync: sinon.stub().throws(new Error('Failed'))
}
});
expect(function() {
StubbedBtable.prototype._initBtableDirectory.call({
_tablePath: 'some/path.kfs'
});
}).to.throw(Error, 'Failed');
});
it('should write the reference id file and callback', function() {
var _mkdirp = sinon.stub();
var _writeFileSync = sinon.stub();
var StubbedBtable = proxyquire('../lib/b-table', {
mkdirp: {
sync: _mkdirp
},
fs: {
writeFileSync: _writeFileSync
}
});
StubbedBtable.prototype._initBtableDirectory.call({
_tablePath: 'some/path.kfs'
});
expect(_mkdirp.called).to.equal(true);
expect(_writeFileSync.called).to.equal(true);
});
});
describe('#_validateTablePath', function() {
it('should throw error if not a directory', function() {
var StubbedBtable = proxyquire('../lib/b-table', {
fs: {
statSync: sinon.stub().returns({
isDirectory: sinon.stub().returns(false)
})
}
});
expect(function() {
StubbedBtable.prototype._validateTablePath.call({
_tablePath: 'some/path.kfs'
});
}).to.throw(Error, 'Table path is not a directory');
});
it('should throw error if not valid table', function() {
var StubbedBtable = proxyquire('../lib/b-table', {
fs: {
statSync: sinon.stub().returns({
isDirectory: sinon.stub().returns(true)
}),
readdirSync: sinon.stub().returns([])
}
});
expect(function() {
StubbedBtable.prototype._validateTablePath.call({
_tablePath: 'some/path.kfs'
});
}).to.throw(Error, 'Table path is not a valid KFS instance');
});
it('should not throw if valid table', function() {
var StubbedBtable = proxyquire('../lib/b-table', {
fs: {
statSync: sinon.stub().returns({
isDirectory: sinon.stub().returns(true)
}),
readdirSync: sinon.stub().returns([Btable.RID_FILENAME])
}
});
expect(function() {
StubbedBtable.prototype._validateTablePath.call({
_tablePath: 'some/path.kfs'
});
}).to.not.throw(Error);
});
});
describe('#_getSbucketIndexForKey', function() {
it('should return the correct xor value', function() {
expect(
Btable.prototype._getSbucketIndexForKey.call({
_rid: Buffer('00', 'hex')
}, '00')
).to.equal(96);
});
});
describe('#_getSbucketAtIndex', function() {
it('should return the existing sBucket', function() {
var _sBucket = new EventEmitter();
var sBucket = Btable.prototype._getSbucketAtIndex.call({
_sBuckets: { 0: _sBucket }
}, 0);
expect(sBucket).to.equal(_sBucket);
});
it('should create a new sBucket at the index', function() {
var StubbedBtable = proxyquire('../lib/b-table', {
'./s-bucket': EventEmitter
});
var sBucket = StubbedBtable.prototype._getSbucketAtIndex.call({
_sBuckets: {},
_tablePath: 'some/path.kfs',
_options: { sBucketOpts: {} }
}, 0);
expect(sBucket).to.be.instanceOf(EventEmitter);
});
it('should close the s-bucket on idle', function(done) {
var StubbedBtable = proxyquire('../lib/b-table', {
'./s-bucket': EventEmitter
});
var sBucket = StubbedBtable.prototype._getSbucketAtIndex.call({
_sBuckets: {},
_tablePath: 'some/path.kfs',
_options: { sBucketOpts: {} }
}, 0);
var _close = sinon.stub(sBucket, 'close');
sBucket.emit('idle');
setImmediate(() => {
expect(_close.called).to.equal(true);
done();
});
});
});
describe('#_getSbucketForKey', function() {
it('should callback with the sBucket if opened', function(done) {
var sBucket = new EventEmitter();
sBucket.readyState = Sbucket.OPENED;
var _getSbucketAtIndex = sinon.stub(
Btable.prototype,
'_getSbucketAtIndex'
).returns(sBucket);
var _open = sinon.stub(Btable.prototype, '_open');
var bTable = new Btable('');
bTable._getSbucketForKey('0f0f', function(err, s) {
_getSbucketAtIndex.restore();
_open.restore();
expect(s).to.equal(sBucket);
done();
});
});
it('should callback with error if opening sBucket fails', function(done) {
var sBucket = new EventEmitter();
sBucket.readyState = Sbucket.CLOSED;
sBucket.open = sinon.stub().callsArgWith(0, new Error('Failed'));
var _getSbucketAtIndex = sinon.stub(
Btable.prototype,
'_getSbucketAtIndex'
).returns(sBucket);
var _open = sinon.stub(Btable.prototype, '_open');
var bTable = new Btable('');
bTable._getSbucketForKey('0f0f', function(err) {
_getSbucketAtIndex.restore();
_open.restore();
expect(err.message).to.equal('Failed');
done();
});
});
it('should callback when sBucket is opened', function(done) {
var sBucket = new EventEmitter();
sBucket.readyState = Sbucket.CLOSED;
sBucket.open = sinon.stub().callsArgWith(0);
var _getSbucketAtIndex = sinon.stub(
Btable.prototype,
'_getSbucketAtIndex'
).returns(sBucket);
var _open = sinon.stub(Btable.prototype, '_open');
var bTable = new Btable('');
bTable._getSbucketForKey('0f0f', function(err, s) {
_getSbucketAtIndex.restore();
_open.restore();
expect(err).to.equal(null);
expect(s).to.equal(sBucket);
done();
});
});
});
});