Skip to content

Commit 6e13097

Browse files
minor refactor upsert unit tests
1 parent 448702b commit 6e13097

1 file changed

Lines changed: 92 additions & 100 deletions

File tree

lambdas/upsert-letter/src/handler/__tests__/upsert-handler.test.ts

Lines changed: 92 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SNSMessage, SQSEvent } from "aws-lambda";
1+
import { SNSMessage, SQSEvent, SQSRecord } from "aws-lambda";
22
import pino from "pino";
33
import { LetterRepository } from "internal/datastore/src";
44
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
@@ -11,7 +11,13 @@ import createUpsertLetterHandler from "../upsert-handler";
1111
import { Deps } from "../../config/deps";
1212
import { EnvVars } from "../../config/env";
1313

14-
function createSqsRecord(msgId: string, body: string) {
14+
function createSQSEvent(records: SQSRecord[]): SQSEvent {
15+
return {
16+
Records: records,
17+
};
18+
}
19+
20+
function createSqsRecord(msgId: string, body: string): SQSRecord {
1521
return {
1622
messageId: msgId,
1723
receiptHandle: "",
@@ -169,18 +175,16 @@ describe("createUpsertLetterHandler", () => {
169175
});
170176

171177
test("processes all records successfully and returns no batch failures", async () => {
172-
const evt: SQSEvent = {
173-
Records: [
174-
createSqsRecord(
175-
"msg1",
176-
JSON.stringify(createNotification(createPreparedV2Event())),
177-
),
178-
createSqsRecord(
179-
"msg2",
180-
JSON.stringify(createNotification(createSupplierStatusChangeEvent())),
181-
),
182-
],
183-
};
178+
const evt: SQSEvent = createSQSEvent([
179+
createSqsRecord(
180+
"msg1",
181+
JSON.stringify(createNotification(createPreparedV2Event())),
182+
),
183+
createSqsRecord(
184+
"msg2",
185+
JSON.stringify(createNotification(createSupplierStatusChangeEvent())),
186+
),
187+
]);
184188

185189
const result = await createUpsertLetterHandler(mockedDeps)(
186190
evt,
@@ -215,26 +219,24 @@ describe("createUpsertLetterHandler", () => {
215219
});
216220

217221
test("processes all v1 records successfully and returns no batch failures", async () => {
218-
const evt: SQSEvent = {
219-
Records: [
220-
createSqsRecord(
221-
"msg1",
222-
JSON.stringify(createNotification(createPreparedV1Event())),
223-
),
224-
createSqsRecord(
225-
"msg2",
226-
JSON.stringify(
227-
createNotification(
228-
createPreparedV1Event({
229-
id: "7b9a03ca-342a-4150-b56b-989109c45614",
230-
domainId: "letter2",
231-
url: "s3://letterDataBucket/letter2.pdf",
232-
}),
233-
),
222+
const evt: SQSEvent = createSQSEvent([
223+
createSqsRecord(
224+
"msg1",
225+
JSON.stringify(createNotification(createPreparedV1Event())),
226+
),
227+
createSqsRecord(
228+
"msg2",
229+
JSON.stringify(
230+
createNotification(
231+
createPreparedV1Event({
232+
id: "7b9a03ca-342a-4150-b56b-989109c45614",
233+
domainId: "letter2",
234+
url: "s3://letterDataBucket/letter2.pdf",
235+
}),
234236
),
235237
),
236-
],
237-
};
238+
),
239+
]);
238240

239241
const result = await createUpsertLetterHandler(mockedDeps)(
240242
evt,
@@ -271,9 +273,9 @@ describe("createUpsertLetterHandler", () => {
271273
});
272274

273275
test("invalid JSON body produces batch failure and logs error", async () => {
274-
const evt: SQSEvent = {
275-
Records: [createSqsRecord("bad-json", "this-is-not-json")],
276-
};
276+
const evt: SQSEvent = createSQSEvent([
277+
createSqsRecord("bad-json", "this-is-not-json"),
278+
]);
277279

278280
const result = await createUpsertLetterHandler(mockedDeps)(
279281
evt,
@@ -293,14 +295,12 @@ describe("createUpsertLetterHandler", () => {
293295
});
294296

295297
test("invalid notification schema produces batch failure and logs error", async () => {
296-
const evt: SQSEvent = {
297-
Records: [
298-
createSqsRecord(
299-
"bad-notification-schema",
300-
JSON.stringify({ not: "unexpected notification shape" }),
301-
),
302-
],
303-
};
298+
const evt: SQSEvent = createSQSEvent([
299+
createSqsRecord(
300+
"bad-notification-schema",
301+
JSON.stringify({ not: "unexpected notification shape" }),
302+
),
303+
]);
304304

305305
const result = await createUpsertLetterHandler(mockedDeps)(
306306
evt,
@@ -322,17 +322,15 @@ describe("createUpsertLetterHandler", () => {
322322
});
323323

324324
test("no event type produces batch failure and logs error", async () => {
325-
const evt: SQSEvent = {
326-
Records: [
327-
createSqsRecord(
328-
"bad-event-type",
329-
JSON.stringify({
330-
Type: "Notification",
331-
Message: JSON.stringify({ no: "type" }),
332-
}),
333-
),
334-
],
335-
};
325+
const evt: SQSEvent = createSQSEvent([
326+
createSqsRecord(
327+
"bad-event-type",
328+
JSON.stringify({
329+
Type: "Notification",
330+
Message: JSON.stringify({ no: "type" }),
331+
}),
332+
),
333+
]);
336334

337335
const result = await createUpsertLetterHandler(mockedDeps)(
338336
evt,
@@ -352,17 +350,15 @@ describe("createUpsertLetterHandler", () => {
352350
});
353351

354352
test("invalid event type produces batch failure and logs error", async () => {
355-
const evt: SQSEvent = {
356-
Records: [
357-
createSqsRecord(
358-
"bad-event-type",
359-
JSON.stringify({
360-
Type: "Notification",
361-
Message: JSON.stringify({ type: "unexpected type" }),
362-
}),
363-
),
364-
],
365-
};
353+
const evt: SQSEvent = createSQSEvent([
354+
createSqsRecord(
355+
"bad-event-type",
356+
JSON.stringify({
357+
Type: "Notification",
358+
Message: JSON.stringify({ type: "unexpected type" }),
359+
}),
360+
),
361+
]);
366362

367363
const result = await createUpsertLetterHandler(mockedDeps)(
368364
evt,
@@ -382,20 +378,18 @@ describe("createUpsertLetterHandler", () => {
382378
});
383379

384380
test("valid event type and invalid schema produces batch failure and logs error", async () => {
385-
const evt: SQSEvent = {
386-
Records: [
387-
createSqsRecord(
388-
"bad-event-schema",
389-
JSON.stringify({
390-
Type: "Notification",
391-
Message: JSON.stringify({
392-
type: "uk.nhs.notify.letter-rendering.letter-request.prepared",
393-
some: "unexpected shape",
394-
}),
381+
const evt: SQSEvent = createSQSEvent([
382+
createSqsRecord(
383+
"bad-event-schema",
384+
JSON.stringify({
385+
Type: "Notification",
386+
Message: JSON.stringify({
387+
type: "uk.nhs.notify.letter-rendering.letter-request.prepared",
388+
some: "unexpected shape",
395389
}),
396-
),
397-
],
398-
};
390+
}),
391+
),
392+
]);
399393

400394
const result = await createUpsertLetterHandler(mockedDeps)(
401395
evt,
@@ -419,32 +413,30 @@ describe("createUpsertLetterHandler", () => {
419413
.mockResolvedValueOnce({})
420414
.mockRejectedValueOnce(new Error("ddb error"));
421415

422-
const evt: SQSEvent = {
423-
Records: [
424-
createSqsRecord(
425-
"ok-msg",
426-
JSON.stringify(
427-
createNotification(
428-
createPreparedV2Event({
429-
id: "7b9a03ca-342a-4150-b56b-989109c45615",
430-
domainId: "ok",
431-
}),
432-
),
416+
const evt: SQSEvent = createSQSEvent([
417+
createSqsRecord(
418+
"ok-msg",
419+
JSON.stringify(
420+
createNotification(
421+
createPreparedV2Event({
422+
id: "7b9a03ca-342a-4150-b56b-989109c45615",
423+
domainId: "ok",
424+
}),
433425
),
434426
),
435-
createSqsRecord(
436-
"fail-msg",
437-
JSON.stringify(
438-
createNotification(
439-
createPreparedV2Event({
440-
id: "7b9a03ca-342a-4150-b56b-989109c45616",
441-
domainId: "fail",
442-
}),
443-
),
427+
),
428+
createSqsRecord(
429+
"fail-msg",
430+
JSON.stringify(
431+
createNotification(
432+
createPreparedV2Event({
433+
id: "7b9a03ca-342a-4150-b56b-989109c45616",
434+
domainId: "fail",
435+
}),
444436
),
445437
),
446-
],
447-
};
438+
),
439+
]);
448440

449441
const result = await createUpsertLetterHandler(mockedDeps)(
450442
evt,

0 commit comments

Comments
 (0)