Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Dec 4, 2022
1 parent 3fd7719 commit 39a55e4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugin
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import express from "express";
import supertest from "supertest";
import { vi, describe, it, beforeEach, afterEach, expect } from "vitest";
import * as noteDao from "../../db/note.dao";
import * as bloomFilter from "../../db/bloomFilter.dao";
import EventLogger from "../../logging/EventLogger";
import { deleteNoteController } from "./note.delete.controller";

vi.mock("../../db/note.dao");
vi.mock("../../db/bloomFilter.dao");
vi.mock("../../logging/EventLogger");

const VALID_USER_ID = "f06536e7df6857fc";
Expand All @@ -17,6 +19,7 @@ const MOCK_NOTE_ID = "NOTE_ID";
describe("note.delete.controller", () => {
let mockNoteDao = vi.mocked(noteDao);
let mockEventLogger = vi.mocked(EventLogger);
let mockBloomFilterDao = vi.mocked(bloomFilter);

const test_app = express()
.use(express.json())
Expand All @@ -41,6 +44,10 @@ describe("note.delete.controller", () => {
throw new Error("Note not found");
}
});

mockBloomFilterDao.getFilter.mockImplementation(async () => {
throw new Error("No BloomFilter found");
});
});

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions server/src/lib/expiredNoteFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const DELETED_NOTES_FILTER_NAME = "deletedNotes" as const;
type FilterName =
| typeof EXPIRED_NOTES_FILTER_NAME
| typeof DELETED_NOTES_FILTER_NAME;

export class NoteIdFilter {
_filter: ScalableBloomFilter;
_name: string;
Expand Down
8 changes: 4 additions & 4 deletions server/src/lib/expiredNoteFilter.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Deserialization from database", () => {
mockedDao.getFilter.mockRejectedValue(new Error("No BloomFilter found"));

// test instatiation
const testFilter = await NoteIdFilter.deserializeFromDb();
const testFilter = await NoteIdFilter.deserializeFromDb("expiredNotes");
expect(mockedDao.getFilter).toHaveBeenCalledWith(
"expiredNotes",
ScalableBloomFilter
Expand All @@ -37,7 +37,7 @@ describe("Deserialization from database", () => {
mockedDao.getFilter.mockResolvedValue(bloomFilter);

// test instatiation
const testFilter = await NoteIdFilter.deserializeFromDb();
const testFilter = await NoteIdFilter.deserializeFromDb("expiredNotes");
expect(mockedDao.getFilter).toHaveBeenCalledWith(
"expiredNotes",
ScalableBloomFilter
Expand All @@ -63,7 +63,7 @@ describe("Filter operations and serialization", () => {
});

it("should add multiple noteIds to the filter", async () => {
testFilter = await NoteIdFilter.deserializeFromDb();
testFilter = await NoteIdFilter.deserializeFromDb("expiredNotes");
testFilter.addNoteIds(["test", "test2"]);
expect(testFilter.hasNoteId("test")).toBe(true);
expect(testFilter.hasNoteId("test2")).toBe(true);
Expand All @@ -77,7 +77,7 @@ describe("Filter operations and serialization", () => {
});

it("Should have an error rate <1% for 1000 elements", async () => {
testFilter = await NoteIdFilter.deserializeFromDb();
testFilter = await NoteIdFilter.deserializeFromDb("expiredNotes");
const elements = Array.from({ length: 1000 }, (_, i) => i.toString());
testFilter.addNoteIds(elements);

Expand Down
5 changes: 3 additions & 2 deletions server/src/tasks/deleteExpiredNotes.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vi.mock("../lib/expiredNoteFilter", () => {
const instance = {
addNoteIds: vi.fn(),
};
return { getExpiredNoteFilter: () => instance };
return { getNoteFilter: (name: string) => instance };
});

vi.spyOn(logger, "error");
Expand All @@ -33,12 +33,13 @@ describe("deleteExpiredNotes", () => {
insert_time: new Date(),
expire_time: new Date(),
crypto_version: "v1",
secret_token: "secret_token",
},
]);
mockedDao.deleteNotes.mockResolvedValue(1);

// mock ExpiredNoteFilter
const mockedFilter = vi.mocked(await filter.getNoteFilter());
const mockedFilter = vi.mocked(await filter.getNoteFilter("expiredNotes"));
mockedFilter.addNoteIds.mockResolvedValue();

// test task call
Expand Down

0 comments on commit 39a55e4

Please sign in to comment.