summaryrefslogtreecommitdiff
path: root/src/database.test.ts
blob: ff4b6d8b78f37fba554588042ddc1ebb32436c74 (plain)
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
import { Database } from "./database";

describe("Database module", () => {
  let db: Database;
  beforeAll(async () => {
    db = new Database();
    await db.init();
  });
  // Next 2 tests simply look
  // for expections on operations that
  // should be harmless
  test("it should write data", () => {
    return db.write("/hello/world", {
      formula: "hello world"
    });
  });
  test("it should read data", () => {
    return db.read("/hello/world");
  });
  test("it should have a string representation", () => {
    expect(db.toString()).not.toHaveLength(0);
  });
  test("it should write and then read data", async () => {
    const path = "/hello/world";
    const toWrite = {
      secret: "stuff"
    };
    await db.write(path, toWrite);
    const s = await db.read(path);
    expect(s).toBe(toWrite);
  });
  test("it should be empty after a flush", async () => {
    await db.write("/hello/world", {
      my: "object"
    });
    await db.flush();
    expect(db.toString()).toHaveLength(2); // empty object
  });
  test("it should read null on an empty node", async () => {
    await db.flush();
    const s = await db.read("/any/path/should/work");
    expect(s).toBeNull();
  });
  test("it should not change on read", async () => {
    await db.flush();
    await db.write("/my/data/lives/here", {
      whiteHouseDinner: `America's Finest McDouble's`
    });
    const before = db.toString();
    const s = await db.read("/no/data/here/silly");
    expect(s).toBeNull();
    expect(before).toEqual(db.toString());
  });
});