-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuDB.test.gs
95 lines (81 loc) · 2.62 KB
/
uDB.test.gs
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
"use strict";
const testStore = "test-store";
function runTests() {
console.log("Test the database:");
test("List empty store", () => {
assertEqual(new uDB(testStore).getAll(), []);
});
test("Insert documents", () => {
const db = new uDB(testStore);
assertEqual(db.getAll().length, 0);
const doc = { foo: "bar" };
const docInserted = db.put(doc);
assertEqual(db.getAll().length, 1);
const docInserted2 = db.put(doc);
assertEqual(db.getAll().length, 2);
assertEqual(docInserted.foo, doc.foo);
assertEqual(docInserted2.foo, doc.foo);
assertNotEqual(docInserted._id, docInserted2._id);
});
test("Update document", () => {
const db = new uDB(testStore);
assertEqual(db.getAll().length, 0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
assertEqual(db.getAll().length, 1);
docInserted.foo = "bazz";
const docInserted2 = db.put(docInserted);
assertEqual(db.getAll().length, 1);
assertEqual(docInserted._id, docInserted2._id);
});
test("Find document", () => {
const db = new uDB(testStore);
assertEqual(db.getAll().length, 0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
assertEqual(db.get(docInserted._id), docInserted);
assertEqual(db.get("no-such-id"), null);
});
test("Delete document", () => {
const db = new uDB(testStore);
assertEqual(db.getAll().length, 0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
assertEqual(db.getAll().length, 1);
assertEqual(db.delete(docInserted._id).length, 0);
});
test("List stores", () => {
const doc = { foo: "bar" };
new uDB(testStore).put(doc);
const stores = uDB.getStores();
assertEqual(stores[0], testStore);
assertEqual(new uDB(stores[0]).getAll()[0].foo, doc.foo);
});
console.log("All test passed. :)");
}
function beforeEach() {
new uDB(testStore).clear();
}
function afterEach() {
new uDB(testStore).clear();
}
function test(name, testFunc) {
console.log(` - ${name}`);
beforeEach();
testFunc();
afterEach();
}
function assertEqual(a, b) {
const jsonA = JSON.stringify(a);
const jsonB = JSON.stringify(b);
if (jsonA != jsonB) {
throw new Error(`Unexpected: ${jsonA} != ${jsonB}`);
}
}
function assertNotEqual(a, b) {
const jsonA = JSON.stringify(a);
const jsonB = JSON.stringify(b);
if (jsonA == jsonB) {
throw new Error(`Unexpected: ${jsonA} == ${jsonB}`);
}
}