Skip to content

Commit

Permalink
remove redundant test and unnecessary use of waitFor
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylee80 committed May 21, 2023
1 parent 60b47d4 commit 7843b3d
Showing 1 changed file with 2 additions and 201 deletions.
203 changes: 2 additions & 201 deletions packages/server/src/fhir/operations/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,211 +93,12 @@ describe('System export', () => {
expect(dataRes.status).toBe(200);

// Output format is "ndjson", new line delimited JSON
// However, we only expect one Observation, so we can parse it as JSON
const resourceJSON = dataRes.text.trim().split('\n');
expect(resourceJSON).toHaveLength(1);
expect(JSON.parse(resourceJSON[0])?.subject?.reference).toEqual(`Patient/${res1.body.id}`);
});

test('Parameters', async () => {
const { project } = await createTestProject();
expect(project).toBeDefined();

const accessToken = await initTestAuth();
expect(accessToken).toBeDefined();

const res1 = await request(app)
.post(`/fhir/R4/Patient`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/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: 'alice@example.com' },
],
});
expect(res1.status).toBe(201);

// Create observation
const res2 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send({
resourceType: 'Observation',
status: 'final',
code: { text: 'test' },
subject: { reference: `Patient/${res1.body.id}` },
});
expect(res2.status).toBe(201);

await waitFor(async () => {
// Create later observation
const res3 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send({
resourceType: 'Observation',
status: 'final',
code: { text: 'test2' },
subject: { reference: `Patient/${res1.body.id}` },
});
expect(res3.status).toBe(201);
});
const updatedDate = new Date(res2.body.meta.lastUpdated);
updatedDate.setMilliseconds(updatedDate.getMilliseconds() + 1);

// Start the export
let initRes: any;
await waitFor(async () => {
initRes = await request(app)
.post('/fhir/R4/$export')
.query({
_type: 'Observation',
_since: updatedDate.toISOString(),
})
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.set('X-Medplum', 'extended')
.send({});
expect(initRes.status).toBe(202);
expect(initRes.headers['content-location']).toBeDefined();
});

// Check the export status
const contentLocation = new URL(initRes?.headers?.['content-location']);

let resBody: any;
await waitFor(async () => {
const statusRes = await request(app)
.get(contentLocation.pathname)
.set('Authorization', 'Bearer ' + accessToken);
expect(statusRes.status).toBe(200);
resBody = statusRes.body;
});

const output = resBody?.output as BulkDataExportOutput[];
expect(Object.values(output).map((ex) => ex.type)).toEqual(['Observation']);

// Get the export content
const outputLocation = new URL(output[0]?.url as string);
const dataRes = await request(app)
.get(outputLocation.pathname + outputLocation.search)
.set('Authorization', 'Bearer ' + accessToken);
expect(dataRes.status).toBe(200);

// Output format is "ndjson", new line delimited JSON
const resourceJSON = dataRes.text.trim().split('\n');
expect(JSON.parse(resourceJSON[0])?.code?.text).toEqual('test2');
});

test('Multiple Resources by Resource Type', async () => {
const { project } = await createTestProject();
expect(project).toBeDefined();

const accessToken = await initTestAuth();
expect(accessToken).toBeDefined();
const res1 = await request(app)
.post(`/fhir/R4/Patient`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/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: 'alice@example.com' },
],
});
expect(res1.status).toBe(201);
await waitFor(async () => {
// Create observation
const res2 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send({
resourceType: 'Observation',
status: 'final',
code: { text: 'test' },
subject: { reference: `Patient/${res1.body.id}` },
});
expect(res2.status).toBe(201);

// Create 2nd observation
const res3 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send({
resourceType: 'Observation',
status: 'final',
code: { text: 'test2' },
subject: { reference: `Patient/${res1.body.id}` },
});
expect(res3.status).toBe(201);

// Create third observation
const res4 = await request(app)
.post(`/fhir/R4/Observation`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send({
resourceType: 'Observation',
status: 'final',
code: { text: 'test3' },
subject: { reference: `Patient/${res1.body.id}` },
});
expect(res4.status).toBe(201);
});

// Start the export
let initRes: any;
await waitFor(async () => {
initRes = await request(app)
.post('/fhir/R4/$export')
.query({
_type: 'Observation',
})
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.set('X-Medplum', 'extended')
.send({});
expect(initRes.status).toBe(202);
expect(initRes.headers['content-location']).toBeDefined();
});

// Check the export status
const contentLocation = new URL(initRes?.headers?.['content-location']);

let resBody: any;
await waitFor(async () => {
const statusRes = await request(app)
.get(contentLocation.pathname)
.set('Authorization', 'Bearer ' + accessToken);
expect(statusRes.status).toBe(200);
resBody = statusRes.body;
});

const output = resBody?.output as BulkDataExportOutput[];
expect(Object.values(output).map((ex) => ex.type)).toEqual(['Observation']);

// Get the export content
const outputLocation = new URL(output[0]?.url as string);
const dataRes = await request(app)
.get(outputLocation.pathname + outputLocation.search)
.set('Authorization', 'Bearer ' + accessToken);
expect(dataRes.status).toBe(200);

// Output format is "ndjson", new line delimited JSON
const resourceJSON = dataRes.text.trim().split('\n');
expect(resourceJSON.length).toBeGreaterThan(1);
expect(JSON.parse(resourceJSON[0])?.code?.text).toEqual('test');
});

test('exportResourceType iterating through paginated search results', async () => {
const { project } = await createTestProject();
expect(project).toBeDefined();
Expand Down

0 comments on commit 7843b3d

Please sign in to comment.