-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.test.js
496 lines (421 loc) · 15.4 KB
/
index.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// @ts-check
const assert = require("assert");
const { readFileSync, unlinkSync, writeFileSync } = require("fs");
const { join } = require("path");
const { FSDB } = require("file-system-db");
describe("FSDB", () => {
/** @type {FSDB} */
let db;
describe("initializing the class", () => {
describe("usage", () => {
const filename = "initialization-db.json";
const filepath = join(__dirname, filename);
afterEach(() => unlinkSync(db.path));
it("should handle no arguments", () => {
db = new FSDB();
const expectedPath = join(__dirname, "database.json");
assert(db instanceof FSDB);
assert.strictEqual(db.path, expectedPath);
assert.strictEqual(readFileSync(expectedPath, "utf8"), "{}");
});
it("should handle a custom, relative path", () => {
db = new FSDB(`./${filename}`);
assert(db instanceof FSDB);
assert.strictEqual(db.path, filepath);
assert.strictEqual(readFileSync(filepath, "utf8"), "{}");
});
it("should handle a custom path, absolute path", () => {
const { homedir } = require("os");
const filepath = join(homedir(), filename);
db = new FSDB(filepath);
assert(db instanceof FSDB);
assert.strictEqual(db.path, filepath);
assert.strictEqual(readFileSync(filepath, "utf8"), "{}");
});
it("should handle referencing an existing database", () => {
db = new FSDB(`./${filename}`);
const content = JSON.stringify({ foo: "bar" });
writeFileSync(filepath, content, "utf8");
assert(db instanceof FSDB);
assert.strictEqual(db.path, filepath);
assert.strictEqual(readFileSync(filepath, "utf8"), content);
});
});
});
describe("performing basic operations", () => {
const exampleData = {
foo: "bar",
bar: 0,
baz: true,
qux: [1, 2, 3],
};
before(() => {
db = new FSDB("set-get-db.json");
// set some values
db.set("quux", exampleData);
Object.entries(exampleData).forEach(([key, value]) => {
db.set(key, value);
});
});
after(() => unlinkSync(db.path));
describe("setting values", () => {
it("should set serializable values in the database", () => {
const data = JSON.parse(readFileSync(db.path, "utf8"));
const expected = { ...exampleData, quux: exampleData };
assert.deepStrictEqual(data, expected);
});
it("should not set non-serializable values in the database", () => {
db.set("non-serializable", () => {});
const data = JSON.parse(readFileSync(db.path, "utf8"));
assert.strictEqual(data["non-serializable"], undefined);
});
it("should set nested values in the database", () => {
db.set("an.example.nested.key", 42);
const data = JSON.parse(readFileSync(db.path, "utf8"));
assert.strictEqual(data.an.example.nested.key, 42);
});
});
describe("getting values", () => {
it("should get top-level values from the database", () => {
assert.deepStrictEqual(db.get("quux"), exampleData);
Object.entries(exampleData).forEach(([key, value]) => {
assert.deepStrictEqual(db.get(key), value);
});
});
it("should get nested values from the database", () => {
Object.entries(exampleData).forEach(([key, value]) => {
assert.deepStrictEqual(db.get(`quux.${key}`), value);
});
});
it("should get nested array values from the database", () => {
assert.deepStrictEqual(db.get("quux.qux.0"), 1);
assert.deepStrictEqual(db.get("quux.qux.1"), 2);
assert.deepStrictEqual(db.get("quux.qux.2"), 3);
});
it("should get `undefined` for non-existent values", () => {
assert.strictEqual(db.get("non-existent"), undefined);
});
});
describe("checking for values", () => {
it("should return `true` for existing values", () => {
assert.strictEqual(db.has("quux"), true);
Object.keys(exampleData).forEach((key) => {
assert.strictEqual(db.has(key), true);
});
});
it("should return `false` for non-existent values", () => {
assert.strictEqual(db.has("non-existent"), false);
});
});
describe("deleting values", () => {
it("should delete top-level values from the database", () => {
db.delete("foo");
assert.strictEqual(db.get("foo"), undefined);
});
it("should delete nested values from the database", () => {
db.delete("quux.foo");
assert.strictEqual(db.get("quux.foo"), undefined);
});
it("should not throw for non-existent values", () => {
assert.doesNotThrow(() => db.delete("non-existent"));
});
});
describe("getting entries", () => {
it("should get all entries from the database", () => {
const entries = db.getAll();
const expected = [
{ key: "bar", value: 0 },
{ key: "baz", value: true },
{ key: "qux", value: [1, 2, 3] },
{
key: "quux",
value: { bar: 0, baz: true, qux: [1, 2, 3] },
},
{ key: "an", value: { example: { nested: { key: 42 } } } },
];
entries.forEach(({ key, value }) => {
assert.deepStrictEqual(
value,
expected.find((entry) => entry.key === key)?.value,
);
});
});
it("should get all entries when `verbose` is `true`", () => {
const entries = db.getAll(true);
const expected = [
{ key: "bar", value: 0 },
{ key: "baz", value: true },
{ key: "qux.0", value: 1 },
{ key: "qux.1", value: 2 },
{ key: "qux.2", value: 3 },
{ key: "quux.bar", value: 0 },
{ key: "quux.baz", value: true },
{ key: "quux.qux.0", value: 1 },
{ key: "quux.qux.1", value: 2 },
{ key: "quux.qux.2", value: 3 },
{ key: "an.example.nested.key", value: 42 },
];
entries.forEach(({ key, value }) => {
assert.strictEqual(
value,
expected.find((entry) => entry.key === key)?.value,
);
});
});
it("should get all entries starting with a given query", () => {
const entries = db.startsWith("qu");
const expected = [
{ key: "qux.0", value: 1 },
{ key: "qux.1", value: 2 },
{ key: "qux.2", value: 3 },
{ key: "quux.bar", value: 0 },
{ key: "quux.baz", value: true },
{ key: "quux.qux.0", value: 1 },
{ key: "quux.qux.1", value: 2 },
{ key: "quux.qux.2", value: 3 },
];
entries.forEach(({ key, value }) => {
assert.deepStrictEqual(
value,
expected.find((entry) => entry.key === key)?.value,
);
});
});
});
describe("backing up content", () => {
it("should create a backup of the database", () => {
const expectedPath = join(__dirname, "../backup.json");
db.backup(expectedPath);
assert.strictEqual(
readFileSync(expectedPath, "utf8"),
readFileSync(db.path, "utf8"),
);
unlinkSync(expectedPath);
});
});
describe("delete all content", () => {
it("should clear the database", () => {
db.deleteAll();
assert.strictEqual(readFileSync(db.path, "utf8"), "{}");
});
});
});
describe("performing array operations", () => {
before(() => {
db = new FSDB("array-db.json");
db.set("arr1", []);
db.set("arr2", [1, 2, 3]);
db.set("arr3", ["a", "b", "c"]);
});
after(() => unlinkSync(db.path));
describe("pushing values", () => {
it("should push values to array", () => {
db.push("arr1", "a");
assert.deepStrictEqual(db.get("arr1"), ["a"]);
db.push("arr2", 4);
assert.deepStrictEqual(db.get("arr2"), [1, 2, 3, 4]);
db.push("arr3", "d");
assert.deepStrictEqual(db.get("arr3"), ["a", "b", "c", "d"]);
});
it("should push multiple values to array", () => {
db.push("arr1", 1, "b", 2, 3);
assert.deepStrictEqual(db.get("arr1"), ["a", 1, "b", 2, 3]);
db.push("arr2", 5, 6, 7);
assert.deepStrictEqual(db.get("arr2"), [1, 2, 3, 4, 5, 6, 7]);
});
});
describe("removing values", () => {
it("should remove values from array", () => {
db.pull("arr1", 3);
assert.deepStrictEqual(db.get("arr1"), ["a", 1, "b", 2]);
db.pull("arr2", 7);
assert.deepStrictEqual(db.get("arr2"), [1, 2, 3, 4, 5, 6]);
});
it("should remove multiple values from array", () => {
db.pull("arr1", 1, 2);
assert.deepStrictEqual(db.get("arr1"), ["a", "b"]);
db.pull("arr2", 4, 5, 6);
assert.deepStrictEqual(db.get("arr2"), [1, 2, 3]);
});
});
});
describe("performing arithmetic operations", () => {
before(() => {
db = new FSDB("arithmetic-db.json");
});
after(() => unlinkSync(db.path));
beforeEach(() => {
db.set("n1", 0);
db.set("n2", 1_000);
db.set("n3", -1_000);
});
describe("adding values", () => {
it("should handle positive numbers", () => {
db.add("n1", 1);
assert.strictEqual(db.get("n1"), 1);
db.add("n1", 50);
assert.strictEqual(db.get("n1"), 51);
db.add("n1", 5.125);
assert.strictEqual(db.get("n1"), 56.125);
db.add("n2", 1);
assert.strictEqual(db.get("n2"), 1_001);
db.add("n2", 500);
assert.strictEqual(db.get("n2"), 1_501);
db.add("n2", 25.5);
assert.strictEqual(db.get("n2"), 1_526.5);
db.add("n3", 1);
assert.strictEqual(db.get("n3"), -999);
db.add("n3", 250);
assert.strictEqual(db.get("n3"), -749);
db.add("n3", 12.75);
assert.strictEqual(db.get("n3"), -736.25);
});
it("should handle negative numbers", () => {
db.add("n1", -1);
assert.strictEqual(db.get("n1"), -1);
db.add("n1", -50);
assert.strictEqual(db.get("n1"), -51);
db.add("n1", -5.125);
assert.strictEqual(db.get("n1"), -56.125);
db.add("n2", -1);
assert.strictEqual(db.get("n2"), 999);
db.add("n2", -500);
assert.strictEqual(db.get("n2"), 499);
db.add("n2", -25.5);
assert.strictEqual(db.get("n2"), 473.5);
db.add("n3", -1);
assert.strictEqual(db.get("n3"), -1_001);
db.add("n3", -250);
assert.strictEqual(db.get("n3"), -1_251);
db.add("n3", -12.75);
assert.strictEqual(db.get("n3"), -1_263.75);
});
});
describe("subtracting values", () => {
it("should handle positive numbers", () => {
db.subtract("n1", 1);
assert.strictEqual(db.get("n1"), -1);
db.subtract("n1", 50);
assert.strictEqual(db.get("n1"), -51);
db.subtract("n1", 5.125);
assert.strictEqual(db.get("n1"), -56.125);
db.subtract("n2", 1);
assert.strictEqual(db.get("n2"), 999);
db.subtract("n2", 500);
assert.strictEqual(db.get("n2"), 499);
db.subtract("n2", 25.5);
assert.strictEqual(db.get("n2"), 473.5);
db.subtract("n3", 1);
assert.strictEqual(db.get("n3"), -1_001);
db.subtract("n3", 250);
assert.strictEqual(db.get("n3"), -1_251);
db.subtract("n3", 12.75);
assert.strictEqual(db.get("n3"), -1_263.75);
});
it("should handle negative numbers", () => {
db.subtract("n1", -1);
assert.strictEqual(db.get("n1"), 1);
db.subtract("n1", -50);
assert.strictEqual(db.get("n1"), 51);
db.subtract("n1", -5.125);
assert.strictEqual(db.get("n1"), 56.125);
db.subtract("n2", -1);
assert.strictEqual(db.get("n2"), 1_001);
db.subtract("n2", -500);
assert.strictEqual(db.get("n2"), 1_501);
db.subtract("n2", -25.5);
assert.strictEqual(db.get("n2"), 1_526.5);
db.subtract("n3", -1);
assert.strictEqual(db.get("n3"), -999);
db.subtract("n3", -250);
assert.strictEqual(db.get("n3"), -749);
db.subtract("n3", -12.75);
assert.strictEqual(db.get("n3"), -736.25);
});
});
describe("multiplying values", () => {
it("should handle positive numbers", () => {
db.multiply("n1", 1);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n1", 50);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n1", 5.125);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n2", 1);
assert.strictEqual(db.get("n2"), 1_000);
db.multiply("n2", 500);
assert.strictEqual(db.get("n2"), 500_000);
db.multiply("n2", 25.5);
assert.strictEqual(db.get("n2"), 12_750_000);
db.multiply("n3", 1);
assert.strictEqual(db.get("n3"), -1_000);
db.multiply("n3", 250);
assert.strictEqual(db.get("n3"), -250_000);
db.multiply("n3", 12.75);
assert.strictEqual(db.get("n3"), -3_187_500);
});
it("should handle negative numbers", () => {
db.multiply("n1", -1);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n1", -50);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n1", -5.125);
assert.strictEqual(db.get("n1"), 0);
db.multiply("n2", -1);
assert.strictEqual(db.get("n2"), -1_000);
db.multiply("n2", -500);
assert.strictEqual(db.get("n2"), 500_000);
db.multiply("n2", -25.5);
assert.strictEqual(db.get("n2"), -12_750_000);
db.multiply("n3", -1);
assert.strictEqual(db.get("n3"), 1_000);
db.multiply("n3", -250);
assert.strictEqual(db.get("n3"), -250_000);
db.multiply("n3", -12.75);
assert.strictEqual(db.get("n3"), 3_187_500);
});
});
describe("dividing values", () => {
it("should handle positive numbers", () => {
db.divide("n1", 1);
assert.strictEqual(db.get("n1"), 0);
db.divide("n1", 50);
assert.strictEqual(db.get("n1"), 0);
db.divide("n1", 5.125);
assert.strictEqual(db.get("n1"), 0);
db.divide("n2", 1);
assert.strictEqual(db.get("n2"), 1_000);
db.divide("n2", 500);
assert.strictEqual(db.get("n2"), 2);
db.divide("n2", 0.5);
assert.strictEqual(db.get("n2"), 4);
db.divide("n3", 1);
assert.strictEqual(db.get("n3"), -1_000);
db.divide("n3", 250);
assert.strictEqual(db.get("n3"), -4);
db.divide("n3", 0.5);
assert.strictEqual(db.get("n3"), -8);
});
it("should handle negative numbers", () => {
db.divide("n1", -1);
assert.strictEqual(db.get("n1"), 0);
db.divide("n1", -50);
assert.strictEqual(db.get("n1"), 0);
db.divide("n1", -5.125);
assert.strictEqual(db.get("n1"), 0);
db.divide("n2", -1);
assert.strictEqual(db.get("n2"), -1_000);
db.divide("n2", -500);
assert.strictEqual(db.get("n2"), 2);
db.divide("n2", -0.5);
assert.strictEqual(db.get("n2"), -4);
db.divide("n3", -1);
assert.strictEqual(db.get("n3"), 1_000);
db.divide("n3", -250);
assert.strictEqual(db.get("n3"), -4);
db.divide("n3", -0.5);
assert.strictEqual(db.get("n3"), 8);
});
});
});
});