-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.test.js
96 lines (84 loc) · 3.28 KB
/
client.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
"use strict";
const uDBClient = require("./client");
const testStore = "test-store";
const timeoutSeconds = 30; // GAS can be reaaaally slow
const apiUrl = (() => {
let apiUrl = (process.env["API_URL"] || "").trim();
if (apiUrl.length === 0) apiUrl = null;
if (apiUrl == null) {
console.log(
'Environment variable "API_URL" not set (Skipping client tests)'
);
}
return apiUrl;
})();
describe("Test the database and client", () => {
const testAPI = (name, func) => {
if (apiUrl != null) {
test(name, func, timeoutSeconds * 1000);
}
};
if (apiUrl != null) {
beforeEach(async () => {
await new uDBClient(apiUrl).store(testStore).clear();
}, timeoutSeconds * 1000);
afterEach(async () => {
await new uDBClient(apiUrl).store(testStore).clear();
}, timeoutSeconds * 1000);
} else {
// Test suite must contain at least one test.
test("Dummy", () => {});
}
testAPI("List empty store", async () => {
const docs = await new uDBClient(apiUrl).store(testStore).getAll();
expect(docs).toEqual([]);
});
testAPI("Insert documents", async () => {
const db = new uDBClient(apiUrl).store(testStore);
expect((await db.getAll()).length).toBe(0);
const doc = { foo: "bar" };
const docInserted = await db.put(doc);
expect((await db.getAll()).length).toBe(1);
const docInserted2 = await db.put(doc);
expect((await db.getAll()).length).toBe(2);
expect(docInserted.foo).toBe(doc.foo);
expect(docInserted2.foo).toBe(doc.foo);
expect(docInserted._id).not.toEqual(docInserted2._id);
});
testAPI("Update document", async () => {
const db = new uDBClient(apiUrl).store(testStore);
expect((await db.getAll()).length).toBe(0);
const doc = { foo: "bar" };
let docInserted = await db.put(doc);
expect((await db.getAll()).length).toBe(1);
docInserted.foo = "bazz";
const docInserted2 = await db.put(docInserted);
expect((await db.getAll()).length).toBe(1);
expect(docInserted._id).toEqual(docInserted2._id);
});
testAPI("Find document", async () => {
const db = new uDBClient(apiUrl).store(testStore);
expect((await db.getAll()).length).toBe(0);
const doc = { foo: "bar" };
let docInserted = await db.put(doc);
expect(await db.get(docInserted._id)).toEqual(docInserted);
expect(await db.get("no-such-id")).toBe(null);
});
testAPI("Delete document", async () => {
const db = new uDBClient(apiUrl).store(testStore);
expect((await db.getAll()).length).toBe(0);
const doc = { foo: "bar" };
let docInserted = await db.put(doc);
expect((await db.getAll()).length).toBe(1);
expect((await db.delete(docInserted._id)).length).toBe(0);
});
testAPI("List stores", async () => {
const doc = { foo: "bar" };
await new uDBClient(apiUrl).store(testStore).put(doc);
const stores = await new uDBClient(apiUrl).getStores();
expect(stores[0]).toBe(testStore);
expect(
(await new uDBClient(apiUrl).store(stores[0]).getAll())[0].foo
).toBe(doc.foo);
});
});