-
Notifications
You must be signed in to change notification settings - Fork 314
/
patienteverything.test.ts
109 lines (98 loc) · 3.93 KB
/
patienteverything.test.ts
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
import { ContentType, LOINC, createReference } from '@medplum/core';
import { Bundle, Patient } from '@medplum/fhirtypes';
import express from 'express';
import request from 'supertest';
import { initApp, shutdownApp } from '../../app';
import { loadTestConfig } from '../../config';
import { initTestAuth } from '../../test.setup';
const app = express();
let accessToken: string;
describe('Patient Everything Operation', () => {
beforeAll(async () => {
const config = await loadTestConfig();
await initApp(app, config);
accessToken = await initTestAuth();
});
afterAll(async () => {
await shutdownApp();
});
test('Success', async () => {
// Create patient
const res1 = await request(app)
.post(`/fhir/R4/Patient`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', ContentType.FHIR_JSON)
.send({
resourceType: 'Patient',
name: [{ given: ['Alice'], family: 'Smith' }],
address: [{ use: 'home', line: ['123 Main St'], city: 'Anywhere', state: 'CA', postalCode: '90210' }],
telecom: [
{ system: 'phone', value: '555-555-5555' },
{ system: 'email', value: '[email protected]' },
],
});
expect(res1.status).toBe(201);
// Create observation
const res2 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', ContentType.FHIR_JSON)
.send({
resourceType: 'Observation',
status: 'final',
code: { coding: [{ system: LOINC, code: '12345-6' }] },
subject: createReference(res1.body as Patient),
});
expect(res2.status).toBe(201);
// Create condition
// This condition references the patient twice, once as subject and once as asserter
// This is to test that the condition is only returned once
const res3 = await request(app)
.post(`/fhir/R4/Condition`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', ContentType.FHIR_JSON)
.send({
resourceType: 'Condition',
code: { coding: [{ system: LOINC, code: '12345-6' }] },
asserter: createReference(res1.body as Patient),
subject: createReference(res1.body as Patient),
});
expect(res3.status).toBe(201);
// Execute the operation
const res4 = await request(app)
.get(`/fhir/R4/Patient/${res1.body.id}/$everything`)
.set('Authorization', 'Bearer ' + accessToken);
expect(res4.status).toBe(200);
expect(res4.body.entry).toHaveLength(3);
// Create another observation
const res5 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', ContentType.FHIR_JSON)
.send({
resourceType: 'Observation',
status: 'final',
code: { coding: [{ system: LOINC, code: '12345-6' }] },
subject: createReference(res1.body as Patient),
});
expect(res5.status).toBe(201);
// Execute the operation with _since
const res6 = await request(app)
.get(`/fhir/R4/Patient/${res1.body.id}/$everything?_since=${res5.body.meta?.lastUpdated}`)
.set('Authorization', 'Bearer ' + accessToken);
expect(res6.status).toBe(200);
expect(res6.body.entry).toHaveLength(1);
// Execute the operation with _count and _offset
const res7 = await request(app)
.get(`/fhir/R4/Patient/${res1.body.id}/$everything?_count=1&_offset=1`)
.set('Authorization', 'Bearer ' + accessToken);
expect(res7.status).toBe(200);
// Bundle should have pagination links
const bundle = res7.body as Bundle;
expect(bundle.entry).toHaveLength(1);
expect(bundle.link).toBeDefined();
expect(bundle.link?.some((link) => link.relation === 'next')).toBeTruthy();
expect(bundle.link?.some((link) => link.relation === 'first')).toBeTruthy();
expect(bundle.link?.some((link) => link.relation === 'previous')).toBeTruthy();
});
});