-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestRequestContentValidation.test.ts
More file actions
492 lines (397 loc) · 15.1 KB
/
testRequestContentValidation.test.ts
File metadata and controls
492 lines (397 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import {
expect,
describe,
it,
jest
} from "@jest/globals"
import {BundleEntry, Task} from "fhir/r4"
import {
BUSINESS_STATUSES,
businessStatus,
codeSystems,
lastModified,
metaLastUpdated,
nhsNumber,
ONE_DAY_IN_MS,
prescriptionID,
resourceType,
taskStatusAgainstBusinessStatus,
transactionBundle,
taskContent,
validateEntry,
ValidationOutcome,
validateContent,
entryContent,
status
} from "../../src/validation/content"
import {generateInvalidNhsNumbers, generateValidNhsNumbers} from "../utils/nhsNumber"
import {
DEFAULT_DATE,
FULL_URL_0,
generateEntry,
validTask
} from "../utils/testUtils"
describe("Unit test for overall task validation", () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
})
it("When task is valid, should return true with no issues.", async () => {
const expectedOutcome = {valid: true, issues: undefined}
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: validTask()}
const actual: ValidationOutcome = validateEntry(entry)
expect(actual).toEqual(expectedOutcome)
})
})
describe("Unit test for validateContent", () => {
it("When task is valid, should return true with no issues.", async () => {
const expectedOutcome = {valid: true, issues: undefined}
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: validTask()}
const actual: ValidationOutcome = validateContent(entry)
expect(actual).toEqual(expectedOutcome)
})
it("When task is invalid in multiple ways, should return false with issues.", async () => {
const task = validTask()
task.focus!.identifier!.system = "invalidLineItemIdCodeSystem"
task.lastModified = "invalidDate"
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: task}
const expectedOutcome = {
valid: false,
issues:
"Date format provided for lastModified is invalid. Invalid CodeSystem(s) - LineItemID."
}
const actual: ValidationOutcome = validateContent(entry)
expect(actual).toEqual(expectedOutcome)
})
})
describe("Unit test for taskContent", () => {
it("When task is valid, should return empty array.", async () => {
const task = validTask()
const actual: Array<string> = taskContent(task)
expect(actual).toEqual([])
})
it("When task is invalid in multiple ways, should return array with all issues.", async () => {
const task = validTask()
task.focus!.identifier!.system = "invalidLineItemIdCodeSystem"
task.lastModified = "invalidDate"
const expectedOutcome = [
"Date format provided for lastModified is invalid.",
"Invalid CodeSystem(s) - LineItemID."
]
const actual: Array<string> = taskContent(task)
expect(actual).toEqual(expectedOutcome)
})
})
describe("Unit test for entryContent", () => {
it("When entry is valid, should return empty array.", async () => {
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: validTask()}
const actual: Array<string> = entryContent(entry)
expect(actual).toEqual([])
})
it("When entry is invalid, should return array with issue.", async () => {
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: validTask()}
entry.fullUrl = "invalid"
const actual: Array<string> = entryContent(entry)
expect(actual).toEqual(["Invalid entry fullUrl or task id."])
})
})
describe("Unit tests for pre-cast validation of bundle", () => {
it("When resourceType is not Bundle, should return false.", async () => {
const body = {resourceType: "NotBundle"}
const actual = transactionBundle(body)
expect(actual).toEqual(false)
})
it("When type is not transaction, should return false.", async () => {
const body = {type: "not_transaction"}
const actual = transactionBundle(body)
expect(actual).toEqual(false)
})
it("When both correct, should return true.", async () => {
const body = {resourceType: "Bundle", type: "transaction"}
const actual = transactionBundle(body)
expect(actual).toEqual(true)
})
})
describe("Unit tests for validation of lastModified", () => {
it("When lastModified is over a day in the future, should return expected issue.", async () => {
const futureDate = new Date(
DEFAULT_DATE.valueOf() + (ONE_DAY_IN_MS + 1000)
)
const task = {lastModified: futureDate.toISOString()}
const expected = "Invalid lastModified value provided."
const actual = lastModified(task as Task)
expect(actual).toEqual(expected)
})
it("When last modified date format is invalid, should return expected issue.", async () => {
const task = {lastModified: "invalid date"}
const expected = "Date format provided for lastModified is invalid."
const actual = lastModified(task as Task)
expect(actual).toEqual(expected)
})
it("When lastModified is valid and not in the future, should return undefined.", async () => {
const task = {lastModified: DEFAULT_DATE.toISOString()}
const actual = lastModified(task as Task)
expect(actual).toEqual(undefined)
})
it("When meta.lastUpdated present, lastModified <= 999 hours into future should be valid.", async () => {
const withinWindow = new Date(
DEFAULT_DATE.valueOf() - (998 * 60 * 60 * 1000)
)
const task = {lastModified: withinWindow.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
const actual = lastModified(task as Task)
expect(actual).toEqual(undefined)
})
it("When meta.lastUpdated present, lastModified > 999 hours into future should return expected issue.", async () => {
const futureDate = new Date(
DEFAULT_DATE.valueOf() + (999 * 60 * 60 * 1000 + 1000)
)
const task = {lastModified: futureDate.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
const expected = "Invalid lastModified value provided."
const actual = lastModified(task as Task)
expect(actual).toEqual(expected)
})
})
describe("Unit tests for validation of metaLastUpdated", () => {
it("When meta.lastUpdated is over a day in the future, should return expected issue.", async () => {
const futureDate = new Date(
DEFAULT_DATE.valueOf() + (ONE_DAY_IN_MS + 1000)
)
const task = {meta: {lastUpdated: futureDate.toISOString()}}
const expected = "Invalid meta.lastUpdated value provided."
const actual = metaLastUpdated(task as Task)
expect(actual).toEqual(expected)
})
it("When meta.lastUpdated date format is invalid, should return expected issue.", async () => {
const task = {meta: {lastUpdated: "invalid date"}}
const expected = "Date format provided for meta.lastUpdated is invalid."
const actual = metaLastUpdated(task as Task)
expect(actual).toEqual(expected)
})
it("When meta.lastUpdated is valid and not in the future, should return undefined.", async () => {
const task = {meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
const actual = metaLastUpdated(task as Task)
expect(actual).toEqual(undefined)
})
it("When meta.lastUpdated is not present, should return undefined.", async () => {
const task = {}
const actual = metaLastUpdated(task as Task)
expect(actual).toEqual(undefined)
})
})
describe("Unit tests for validation of prescription ID", () => {
it.each([
{
testPrescriptionID: "invalid",
expected: "Prescription ID is invalid.",
scenarioDescription:
"When prescription ID is invalid, should return expected issue."
},
{
testPrescriptionID: "07A66F-A83008-1EEEA0",
expected: undefined,
scenarioDescription:
"When prescription ID is valid, should return undefined."
}
])("$scenarioDescription", async ({testPrescriptionID, expected}) => {
const task = {basedOn: [{identifier: {value: testPrescriptionID}}]}
const actual = prescriptionID(task as Task)
expect(actual).toEqual(expected)
})
})
describe("Unit tests for validation of NHS number", () => {
it.each([
{
generatedNhsNumber: generateInvalidNhsNumbers(1)[0],
expected: "NHS number is invalid.",
scenarioDescription:
"When NHS number is invalid, should return expected issue."
},
{
generatedNhsNumber: generateValidNhsNumbers(1)[0],
expected: undefined,
scenarioDescription: "When NHS number is valid, should return undefined."
}
])("$scenarioDescription", async ({generatedNhsNumber, expected}) => {
const task = {for: {identifier: {value: generatedNhsNumber}}}
const actual = nhsNumber(task as Task)
expect(actual).toEqual(expected)
})
})
describe("Unit tests for validation of status", () => {
it.each([
{
updateStatus: "completed",
expected: undefined,
scenarioDescription:
"When status is 'completed', should return undefined."
},
{
updateStatus: "in-progress",
expected: undefined,
scenarioDescription:
"When status is 'in-progress', should return undefined."
},
{
updateStatus: "rejected",
expected: "Unsupported Task.status 'rejected'.",
scenarioDescription:
"When status is unsupported, should return expected issue."
}
])("$scenarioDescription", async ({updateStatus, expected}) => {
const task = {status: updateStatus}
const actual = status(task as Task)
expect(actual).toEqual(expected)
})
})
describe("Unit tests for validation of status against business status", () => {
describe("When task status is 'completed'", () => {
it.each([
{isValid: false, businessStatus: "With Pharmacy"},
{isValid: false, businessStatus: "With Pharmacy - preparing remainder"},
{isValid: false, businessStatus: "Ready to collect - partial"},
{isValid: false, businessStatus: "rEaDy To ColLEcT - pArtIAl"},
{isValid: true, businessStatus: "Ready to collect"},
{isValid: true, businessStatus: "ReAdY tO cOlLeCt"},
{isValid: true, businessStatus: "Collected"},
{isValid: true, businessStatus: "Not dispensed"},
{isValid: true, businessStatus: "Dispatched"},
{isValid: true, businessStatus: "Ready to dispatch"},
{isValid: false, businessStatus: "Ready to Dispatch - Partial"}
])(
"When status is 'completed' and business status is '$businessStatus', should return expected issue.",
({isValid, businessStatus}) => {
const task = {
status: "completed",
businessStatus: {coding: [{code: businessStatus}]}
}
const actual = taskStatusAgainstBusinessStatus(task as Task)
const expected = isValid
? undefined
: // eslint-disable-next-line max-len
`Task.status field set to 'completed' but Task.businessStatus value of '${businessStatus}' requires follow up action.`
expect(actual).toEqual(expected)
}
)
})
describe("When task status is 'in-progress'", () => {
it.each([
{isValid: true, businessStatus: "With Pharmacy"},
{isValid: true, businessStatus: "With Pharmacy - preparing remainder"},
{isValid: true, businessStatus: "Ready to collect"},
{isValid: true, businessStatus: "Ready to collect - partial"},
{isValid: true, businessStatus: "Ready to dispatch"},
{isValid: true, businessStatus: "Ready to Dispatch - Partial"},
{isValid: false, businessStatus: "Collected"},
{isValid: false, businessStatus: "Not dispensed"},
{isValid: false, businessStatus: "Dispatched"}
])(
"When status is 'in-progress' and business status is '$businessStatus', should return expected issue.",
({isValid, businessStatus}) => {
const task = {
status: "in-progress",
businessStatus: {coding: [{code: businessStatus}]}
}
const actual = taskStatusAgainstBusinessStatus(task as Task)
const expected = isValid
? undefined
: // eslint-disable-next-line max-len
`Task.status field set to 'in-progress' but Task.businessStatus value of '${businessStatus}' has no possible follow up action.`
expect(actual).toEqual(expected)
}
)
})
describe("When business status is unsupported", () => {
it.each([
{status: "completed", businessStatus: "unsupported-status"},
{status: "in-progress", businessStatus: "another-unsupported-status"}
])(
"When status is '$status' and business status is '$businessStatus', should return unsupported issue.",
({status, businessStatus}) => {
const task = {
status,
businessStatus: {coding: [{code: businessStatus}]}
}
const actual = taskStatusAgainstBusinessStatus(task as Task)
const expected = `Unsupported Task.businessStatus '${businessStatus}'.`
expect(actual).toEqual(expected)
}
)
})
})
describe("Unit tests for validation of resourceType", () => {
it.each([
{
type: "NotTask",
expected: "Resource's resourceType is not 'Task'.",
scenarioDescription:
"When resourceType is not Task, should return expected issue."
},
{
type: "Task",
expected: undefined,
scenarioDescription:
"When resourceType is Task, should return undefined."
}
])("$scenarioDescription", async ({type, expected}) => {
const task = {resourceType: type}
const actual = resourceType(task as Task)
expect(actual).toEqual(expected)
})
})
describe("Unit tests for validation of transaction bundle", () => {
it.each([
{
resourceType: "NotBundle",
type: "transaction",
expected: false
},
{
resourceType: "Bundle",
type: "not_transaction",
expected: false
},
{
resourceType: "Bundle",
type: "transaction",
expected: true
}
])(
"When resourceType is $resourceType and type is $type, should return $expected.",
async ({resourceType, type, expected}) => {
const body = {resourceType: resourceType, type: type}
const actual = transactionBundle(body)
expect(actual).toEqual(expected)
}
)
})
describe("Unit tests for validation of businessStatus", () => {
it.each(BUSINESS_STATUSES)(
"When businessStatus is valid, should return undefined.",
async (status) => {
const task = {businessStatus: {coding: [{code: status}]}}
const actual = businessStatus(task as Task)
expect(actual).toEqual(undefined)
}
)
it("When businessStatus is invalid, should return expected message.", async () => {
const task = {businessStatus: {coding: [{code: "Invalid"}]}}
const actual = businessStatus(task as Task)
expect(actual).toEqual("Invalid business status.")
})
})
describe("Unit tests for validation of codeSystems", () => {
it("When code systems are all valid, should return undefined.", async () => {
const entry = generateEntry(0) as BundleEntry
const actual = codeSystems(entry.resource as Task)
expect(actual).toEqual(undefined)
})
it("When code systems are invalid, should return expected message.", async () => {
const entry = generateEntry(0) as BundleEntry
const task = entry.resource as Task
task.focus!.identifier!.system = "invalidLineItemIdCodeSystem"
task.for!.identifier!.system = "invalidNhsNumberCodeSystem"
const actual = codeSystems(entry.resource as Task)
expect(actual).toEqual(
"Invalid CodeSystem(s) - LineItemID, PatientNHSNumber."
)
})
})