Skip to content

Commit 40d2751

Browse files
committed
Flesh out dynamo tests a bit more, and stop sorting in the dynamo client
1 parent ef7610f commit 40d2751

2 files changed

Lines changed: 97 additions & 24 deletions

File tree

packages/postDatedLambda/src/databaseClient.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ export async function getExistingRecordsByPrescriptionID(
7373
recordCount: items.length
7474
})
7575

76-
// Sort by LastModified ascending so most recent is first
77-
items.sort((a, b) => new Date(b.LastModified).valueOf() - new Date(a.LastModified).valueOf())
78-
7976
return items
8077
} catch (err) {
8178
logger.error("Error querying DynamoDB for existing prescription records", {

packages/postDatedLambda/tests/testDatabaseClient.test.ts

Lines changed: 97 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const {
3535
const logger = new Logger({serviceName: "postDatedLambdaTEST"})
3636

3737
describe("databaseClient", () => {
38+
beforeEach(() => {
39+
mockSend.mockReset()
40+
})
41+
3842
describe("getExistingRecordsByPrescriptionID", () => {
3943
it("should return existing records from DynamoDB", async () => {
4044
const prescriptionID = "testPrescID"
@@ -43,12 +47,12 @@ describe("databaseClient", () => {
4347
const mockItems = [
4448
{
4549
PrescriptionID: {S: prescriptionID},
46-
Status: {S: "Dispensed"},
50+
Status: {S: "With pharmacy"},
4751
LastModified: {S: "2024-01-01T12:00:00Z"}
4852
},
4953
{
5054
PrescriptionID: {S: prescriptionID},
51-
Status: {S: "ReadyForCollection"},
55+
Status: {S: "Ready to collect"},
5256
LastModified: {S: "2023-12-31T12:00:00Z"}
5357
}
5458
]
@@ -64,8 +68,21 @@ describe("databaseClient", () => {
6468
)
6569

6670
expect(records).toHaveLength(2)
67-
expect(records[0].Status).toBe("Dispensed")
68-
expect(records[1].Status).toBe("ReadyForCollection")
71+
expect(records[0].Status).toBe("With pharmacy")
72+
expect(records[1].Status).toBe("Ready to collect")
73+
})
74+
75+
it("should return an empty array when DynamoDB returns no items", async () => {
76+
const prescriptionID = "noRecordsPrescID"
77+
78+
mockSend.mockReturnValueOnce({
79+
Items: [],
80+
LastEvaluatedKey: undefined
81+
})
82+
83+
const records = await getExistingRecordsByPrescriptionID(prescriptionID, logger)
84+
85+
expect(records).toEqual([])
6986
})
7087

7188
it("Should log and throw an error if the DynamoDB query fails", async () => {
@@ -84,6 +101,45 @@ describe("databaseClient", () => {
84101
)
85102
).rejects.toThrow("DynamoDB query failed")
86103
})
104+
105+
it("should paginate through multiple DynamoDB result pages", async () => {
106+
const prescriptionID = "pagedPrescID"
107+
108+
const firstPageItems = [
109+
{
110+
PrescriptionID: {S: prescriptionID},
111+
Status: {S: "Ready to collect"},
112+
LastModified: {S: "2024-01-01T12:00:00Z"}
113+
}
114+
]
115+
116+
const secondPageItems = [
117+
{
118+
PrescriptionID: {S: prescriptionID},
119+
Status: {S: "With pharmacy"},
120+
LastModified: {S: "2024-01-02T12:00:00Z"}
121+
}
122+
]
123+
124+
mockSend
125+
.mockReturnValueOnce({
126+
Items: firstPageItems,
127+
LastEvaluatedKey: {
128+
PrescriptionID: {S: prescriptionID}
129+
}
130+
})
131+
.mockReturnValueOnce({
132+
Items: secondPageItems,
133+
LastEvaluatedKey: undefined
134+
})
135+
136+
const records = await getExistingRecordsByPrescriptionID(prescriptionID, logger)
137+
138+
expect(mockSend).toHaveBeenCalledTimes(2)
139+
expect(records).toHaveLength(2)
140+
expect(records[0].Status).toBe("Ready to collect")
141+
expect(records[1].Status).toBe("With pharmacy")
142+
})
87143
})
88144

89145
describe("fetchExistingRecordsForPrescriptions", () => {
@@ -98,7 +154,7 @@ describe("databaseClient", () => {
98154
{
99155
PrescriptionID: {S: "presc1"},
100156
PharmacyODSCode: {S: "pharmA"},
101-
Status: {S: "Dispensed"},
157+
Status: {S: "With pharmacy"},
102158
LastModified: {S: "2024-01-01T12:00:00Z"}
103159
}
104160
]
@@ -107,7 +163,7 @@ describe("databaseClient", () => {
107163
{
108164
PrescriptionID: {S: "presc2"},
109165
PharmacyODSCode: {S: "pharmB"},
110-
Status: {S: "ReadyForCollection"},
166+
Status: {S: "Ready to collect"},
111167
LastModified: {S: "2024-01-02T12:00:00Z"}
112168
}
113169
]
@@ -129,9 +185,9 @@ describe("databaseClient", () => {
129185

130186
expect(result.length).toBe(2)
131187
expect(result[0].existingRecords.length).toBe(1)
132-
expect(result[0].existingRecords[0].Status).toBe("Dispensed")
188+
expect(result[0].existingRecords[0].Status).toBe("With pharmacy")
133189
expect(result[1].existingRecords.length).toBe(1)
134-
expect(result[1].existingRecords[0].Status).toBe("ReadyForCollection")
190+
expect(result[1].existingRecords[0].Status).toBe("Ready to collect")
135191
})
136192

137193
it(
@@ -147,7 +203,7 @@ describe("databaseClient", () => {
147203
{
148204
PrescriptionID: {S: "presc1"},
149205
PharmacyODSCode: {S: "pharmA"},
150-
Status: {S: "Dispensed"},
206+
Status: {S: "With pharmacy"},
151207
LastModified: {S: "2024-01-01T12:00:00Z"}
152208
}
153209
]
@@ -168,9 +224,32 @@ describe("databaseClient", () => {
168224

169225
expect(result.length).toBe(2)
170226
expect(result[0].existingRecords.length).toBe(1)
171-
expect(result[0].existingRecords[0].Status).toBe("Dispensed")
227+
expect(result[0].existingRecords[0].Status).toBe("With pharmacy")
172228
expect(result[1].existingRecords.length).toBe(0)
173229
})
230+
231+
it("should return prescriptions with empty existingRecords when DynamoDB has no matches", async () => {
232+
const prescriptions = [
233+
createMockPostModifiedDataItem({PrescriptionID: "noPresc1", PharmacyODSCode: "pharmA"}),
234+
createMockPostModifiedDataItem({PrescriptionID: "noPresc2", PharmacyODSCode: "pharmB"})
235+
]
236+
237+
mockSend
238+
.mockReturnValueOnce({
239+
Items: [],
240+
LastEvaluatedKey: undefined
241+
})
242+
.mockReturnValueOnce({
243+
Items: [],
244+
LastEvaluatedKey: undefined
245+
})
246+
247+
const result = await fetchExistingRecordsForPrescriptions(prescriptions, logger)
248+
249+
expect(result).toHaveLength(2)
250+
expect(result[0].existingRecords).toEqual([])
251+
expect(result[1].existingRecords).toEqual([])
252+
})
174253
})
175254

176255
describe("enrichMessagesWithExistingRecords", () => {
@@ -185,7 +264,7 @@ describe("databaseClient", () => {
185264
{
186265
PrescriptionID: {S: "presc1"},
187266
PharmacyODSCode: {S: "pharmA"},
188-
Status: {S: "Dispensed"},
267+
Status: {S: "With pharmacy"},
189268
LastModified: {S: "2024-01-01T12:00:00Z"}
190269
}
191270
]
@@ -194,7 +273,7 @@ describe("databaseClient", () => {
194273
{
195274
PrescriptionID: {S: "presc2"},
196275
PharmacyODSCode: {S: "pharmB"},
197-
Status: {S: "ReadyForCollection"},
276+
Status: {S: "Ready to collect"},
198277
LastModified: {S: "2024-01-02T12:00:00Z"}
199278
}
200279
]
@@ -220,18 +299,15 @@ describe("databaseClient", () => {
220299

221300
expect(enrichedMessages.length).toBe(2)
222301
expect(enrichedMessages[0].existingRecords.length).toBe(1)
223-
expect(enrichedMessages[0].existingRecords[0].Status).toBe("Dispensed")
302+
expect(enrichedMessages[0].existingRecords[0].Status).toBe("With pharmacy")
224303
expect(enrichedMessages[1].existingRecords.length).toBe(1)
225-
expect(enrichedMessages[1].existingRecords[0].Status).toBe("ReadyForCollection")
304+
expect(enrichedMessages[1].existingRecords[0].Status).toBe("Ready to collect")
226305
})
227-
})
228306

229-
it("Should return empty array when no messages are provided", async () => {
230-
const enrichedMessages = await enrichMessagesWithExistingRecords(
231-
[],
232-
logger
233-
)
307+
it("should return an empty array when no messages are provided", async () => {
308+
const enrichedMessages = await enrichMessagesWithExistingRecords([], logger)
234309

235-
expect(enrichedMessages).toEqual([])
310+
expect(enrichedMessages).toEqual([])
311+
})
236312
})
237313
})

0 commit comments

Comments
 (0)