From 05c47705d734d2c6270621900dc6c417f5f21495 Mon Sep 17 00:00:00 2001 From: Christian Paul <christianp@matrix.org> Date: Fri, 8 Jan 2021 13:10:41 +0100 Subject: [PATCH] Add tests for countEntries --- test/db/test_roomstore.ts | 53 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/test/db/test_roomstore.ts b/test/db/test_roomstore.ts index f9b0b0f..9263391 100644 --- a/test/db/test_roomstore.ts +++ b/test/db/test_roomstore.ts @@ -24,7 +24,7 @@ import { RemoteStoreRoom, MatrixStoreRoom } from "../../src/db/roomstore"; let store: DiscordStore; describe("RoomStore", () => { - before(async () => { + beforeEach(async () => { store = new DiscordStore(":memory:"); await store.init(); }); @@ -189,4 +189,55 @@ describe("RoomStore", () => { expect(entries).to.be.empty; }); }); + describe("countEntries", () => { + it("returns 0 when no entry has been upserted", async () => { + expect(await store.roomStore.countEntries()).to.equal(0); + }); + it("returns 1 when one entry has been upserted", async () => { + await store.roomStore.upsertEntry({ + id: "test", + matrix: new MatrixStoreRoom("test_m"), + remote: new RemoteStoreRoom("test_r", { discord_guild: "find", discord_channel: "this" }), + }); + expect(await store.roomStore.countEntries()).to.equal(1); + }); + it("returns 2 when two entries have been upserted", async () => { + await store.roomStore.upsertEntry({ + id: "test1", + matrix: new MatrixStoreRoom("test1_m"), + remote: new RemoteStoreRoom("test1_r", { discord_guild: "find", discord_channel: "this" }), + }); + await store.roomStore.upsertEntry({ + id: "test2", + matrix: new MatrixStoreRoom("test2_m"), + remote: new RemoteStoreRoom("test2_r", { discord_guild: "find", discord_channel: "this" }), + }); + expect(await store.roomStore.countEntries()).to.equal(2); + }); + it("does not count entries with no matrix_id", async () => { + await store.roomStore.upsertEntry({ + id: "test", + matrix: null, + remote: new RemoteStoreRoom("test_r", { discord_guild: "find", discord_channel: "this" }), + }); + expect(await store.roomStore.countEntries()).to.equal(0); + }); + it("does not count entries with no remote_id", async () => { + await store.roomStore.upsertEntry({ + id: "test", + matrix: new MatrixStoreRoom("test_m"), + remote: null, + }); + expect(await store.roomStore.countEntries()).to.equal(0); + }); + it("returns 0 when one entry has been upserted and removed", async () => { + await store.roomStore.upsertEntry({ + id: "test", + matrix: new MatrixStoreRoom("test_m"), + remote: new RemoteStoreRoom("test_r", { discord_guild: "find", discord_channel: "this" }), + }); + await store.roomStore.removeEntriesByRemoteRoomId("test_r"); + expect(await store.roomStore.countEntries()).to.equal(0); + }); + }); }); -- GitLab