diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2019-01-19 20:58:16 -0800 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2019-01-19 20:58:16 -0800 |
commit | 9d2c5e6879aeb4aac66fa15127786bab6b436e01 (patch) | |
tree | 39230eaf1b083cb1dcf2e3081ae4dd205afcdd0b | |
parent | 8c0a7dcf693fb7300ad98ad5b34e22620883eab6 (diff) |
Test for out of space error
-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); + } + }); }); |