diff options
Diffstat (limited to 'src/database.test.ts')
-rw-r--r-- | src/database.test.ts | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/database.test.ts b/src/database.test.ts index ff4b6d8..9f583b9 100644 --- a/src/database.test.ts +++ b/src/database.test.ts @@ -1,11 +1,27 @@ import { Database } from "./database"; +import { NaiveErrorCode } from "./error.model"; + +const generateString = (sizeBytes: number) => + Array(Math.ceil(sizeBytes)).fill("a"); + +const maxDbSizeMB = 0.1; describe("Database module", () => { let db: Database; + beforeAll(async () => { - db = new Database(); + db = new Database({ + logger: console.log, + maxDbSizeMB, + cachePath: `${__dirname}/db.json` + }); await db.init(); }); + + beforeEach(async () => { + await db.flush(); + }); + // Next 2 tests simply look // for expections on operations that // should be harmless @@ -14,12 +30,15 @@ describe("Database module", () => { 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 = { @@ -29,6 +48,7 @@ describe("Database module", () => { 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" @@ -36,13 +56,13 @@ describe("Database module", () => { 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` }); @@ -51,4 +71,15 @@ describe("Database module", () => { expect(s).toBeNull(); expect(before).toEqual(db.toString()); }); + + test("it should throw an error if max size is exceeded", async () => { + try { + await db.write("/big/data", { + bigObj: generateString(maxDbSizeMB * 1024 ** 2) + }); + throw new Error("Did not throw exception"); + } catch (e) { + expect(e.code).toBe(NaiveErrorCode.OUT_OF_SPACE); + } + }); }); |