Skip to content

Commit

Permalink
feat(CB2-14229): snowball sqs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Searle committed Oct 21, 2024
1 parent 489f47d commit 3220125
Showing 1 changed file with 46 additions and 70 deletions.
116 changes: 46 additions & 70 deletions tests/unit/reportGenFunction.unitTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const mockProcessRecord = jest.fn();

import { reportGen } from "../../src/functions/reportGen";
import { ReportGenerationService } from "../../src/services/ReportGenerationService";
import { SendATFReport } from "../../src/services/SendATFReport";
// import mockConfig from "../util/mockConfig";
import { ERRORS } from "../../src/assets/enum";

const mockProcessRecord = jest.fn();

jest.mock("../../src/services/ReportGenerationService");
jest.mock("../../src/services/SendATFReport");
Expand All @@ -16,91 +16,67 @@ describe("Retro Gen Function", () => {
jest.setTimeout(5000);
return new Promise((r) => setTimeout(r, 0));
});
// const ctx = mockContext();
// mockConfig();

const ctx = {};
context("Receiving an empty event (of various types)", () => {
it("should throw errors (event = {})", async () => {
expect.assertions(1);
try {
await reportGen({}, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Event is empty");
}

describe("When Receiving an invalid event", () => {
it("should throw an error when it is empty)", async () => {
await expect(reportGen({}, ctx as any, () => {
return;
})).rejects.toThrow(ERRORS.EVENT_IS_EMPTY);
});
it("should throw errors (event = null)", async () => {
expect.assertions(1);
try {
await reportGen(null, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Event is empty");
}

it("should throw an error when the event is null)", async () => {
await expect(reportGen(null, ctx as any, () => {
return;
})).rejects.toThrow(ERRORS.EVENT_IS_EMPTY);
});
it("should throw errors (event has no records)", async () => {
expect.assertions(1);
try {
await reportGen({ something: true }, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Event is empty");
}

it("should throw an error when the event has no records", async () => {
await expect(reportGen({something: true}, ctx as any, () => {
return;
})).rejects.toThrow(ERRORS.EVENT_IS_EMPTY);
});
it("should throw errors (event Records is not array)", async () => {
expect.assertions(1);
try {
await reportGen({ Records: true }, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Event is empty");
}

it("should throw an error when the event records is not array", async () => {
await expect(reportGen({Records: true}, ctx as any, () => {
return;
})).rejects.toThrow(ERRORS.EVENT_IS_EMPTY);
});
it("should throw errors (event Records array is empty)", async () => {
expect.assertions(1);
try {
await reportGen({ Records: [] }, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Event is empty");
}

it("should throw an error when the event records array is empty", async () => {
await expect(reportGen({Records: []}, ctx as any, () => {
return;
})).rejects.toThrow(ERRORS.EVENT_IS_EMPTY);
});
});

context("Inner services fail", () => {
describe("Inner services fail", () => {
afterEach(() => {
jest.restoreAllMocks();
});

it("Should throw an error (generateATFReport fails)", async () => {
it("Should add to batchItemFailures (generateATFReport fails)", async () => {
ReportGenerationService.prototype.generateATFReport = jest.fn().mockRejectedValue(new Error("Oh no!"));
mockProcessRecord.mockReturnValueOnce("All good");
expect.assertions(1);
try {
await reportGen({ Records: [{ body: mockPayload }] }, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Oh no!");
}

const result = await reportGen({Records: [JSON.parse(mockPayload)]}, ctx as any, () => {
return;
});
expect(result.batchItemFailures).toHaveLength(1);
expect(result.batchItemFailures[0].itemIdentifier).toBe("1234");
});
it("Should throw an error (bucket upload fails)", async () => {

it("Should add to batchItemFailures (bucket upload fails)", async () => {
ReportGenerationService.prototype.generateATFReport = jest.fn().mockResolvedValue("Looking good");
SendATFReport.prototype.sendATFReport = jest.fn().mockRejectedValue(new Error("Oh dear"));
mockProcessRecord.mockReturnValueOnce("All good");
expect.assertions(1);
try {
await reportGen({ Records: [{ body: mockPayload }] }, ctx as any, () => {
return;
});
} catch (e) {
expect(e.message).toEqual("Oh dear");
}

const result = await reportGen({Records: [JSON.parse(mockPayload)]}, ctx as any, () => {
return;
});
expect(result.batchItemFailures).toHaveLength(1);
expect(result.batchItemFailures[0].itemIdentifier).toBe("1234");
});
});
});

0 comments on commit 3220125

Please sign in to comment.