Completed jsonld-dataset-proxy tests

main
Jackson Morgan 2 years ago
parent 4b477559e2
commit cfec254ab2
  1. 32613
      package-lock.json
  2. 8
      packages/jsonld-dataset-proxy/src/ContextUtil.ts
  3. 29
      packages/jsonld-dataset-proxy/test/ContextUtil.test.ts
  4. 162
      packages/jsonld-dataset-proxy/test/jsonldDatasetProxy.test.ts
  5. 93
      packages/jsonld-dataset-proxy/test/patientExampleData.ts
  6. 122
      packages/jsonld-dataset-proxy/test/scopedExampleData.ts

32613
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -65,9 +65,8 @@ export class ContextUtil {
*/
private getRelevantContext(
key: string,
typeNames?: NamedNode[],
typeNames: NamedNode[],
): ContextDefinition | LdoJsonldContext {
if (!typeNames) return this.context;
for (const typeNameNode of typeNames) {
const typeName = this.iriToKey((typeNameNode as NamedNode).value, []);
if (
@ -116,7 +115,9 @@ export class ContextUtil {
let relevantMap = this.iriToKeyMap;
for (const typeNameNode of typeNames) {
const typeName = this.iriToKey((typeNameNode as NamedNode).value, []);
relevantMap = this.typeNameToIriToKeyMap[typeName] || this.iriToKeyMap;
relevantMap = this.typeNameToIriToKeyMap[typeName]?.[iri]
? this.typeNameToIriToKeyMap[typeName]
: this.iriToKeyMap;
}
if (relevantMap[iri]) {
return relevantMap[iri];
@ -148,7 +149,6 @@ export class ContextUtil {
return !!(
relevantContext[key] &&
typeof relevantContext[key] === "object" &&
(relevantContext[key] as ExpandedTermDefinition)["@container"] &&
((relevantContext[key] as ExpandedTermDefinition)["@container"] ===
"@set" ||
(relevantContext[key] as LdoJsonldContextExpandedTermDefinition)[

@ -1,4 +1,6 @@
import { namedNode } from "@rdfjs/data-model";
import { ContextUtil } from "../src/ContextUtil";
import { scopedContext } from "./scopedExampleData";
describe("ContextUtil", () => {
describe("keyToIri and iriToKey", () => {
@ -18,12 +20,26 @@ describe("ContextUtil", () => {
);
});
it("handles a context that existsm, but does not have an id", () => {
it("handles a context that exists, but does not have an id", () => {
const contextUtil = new ContextUtil({
name: { "@type": "http://www.w3.org/2001/XMLSchema#string" },
});
expect(contextUtil.keyToIri("name", [])).toBe("name");
});
it("handles a nested context", () => {
const contextUtil = new ContextUtil(scopedContext);
expect(
contextUtil.keyToIri("element", [
namedNode("https://example.com/Bender"),
]),
).toBe("https://example.com/element");
expect(
contextUtil.iriToKey("https://example.com/element", [
namedNode("https://example.com/Bender"),
]),
).toBe("element");
});
});
describe("getType", () => {
@ -36,4 +52,15 @@ describe("ContextUtil", () => {
);
});
});
describe("isArray", () => {
it("indicates that the special @isCollection field means array", () => {
const contextUtil = new ContextUtil(scopedContext);
expect(
contextUtil.isArray("element", [
namedNode("https://example.com/Avatar"),
]),
).toBe(true);
});
});
});

@ -18,18 +18,20 @@ import {
import type { ObservationShape, PatientShape } from "./patientExampleData";
import {
patientData,
patientContext,
tinyPatientData,
tinyArrayPatientData,
patientDataWithBlankNodes,
tinyPatientDataWithBlankNodes,
tinyPatientDataWithLanguageTags,
patientUnnestedContext,
patientNestedContext,
} from "./patientExampleData";
import { namedNode, quad, literal, defaultGraph } from "@rdfjs/data-model";
import type { Dataset, NamedNode } from "@rdfjs/types";
import type { ContextDefinition } from "jsonld";
import type { LdoJsonldContext } from "../src/LdoJsonldContext";
describe("jsonldDatasetProxy", () => {
const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
async function getLoadedDataset(): Promise<
[Dataset, ObservationShape, JsonldDatasetProxyBuilder]
> {
@ -412,18 +414,20 @@ describe("jsonldDatasetProxy", () => {
describe("write", () => {
it("sets a primitive value that doesn't exist yet", async () => {
const [dataset, observation] = await getEmptyObservationDataset();
observation.type = { "@id": "Observation" };
observation.notes = "Cool Notes";
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/notes> "Cool Notes" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/notes> "Cool Notes" .\n',
);
});
it("sets primitive number and boolean values", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.age = 35;
patient.isHappy = true;
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/age> "35"^^<http://www.w3.org/2001/XMLSchema#integer> .\n<http://example.com/Patient1> <http://hl7.org/fhir/isHappy> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/age> "35"^^<http://www.w3.org/2001/XMLSchema#integer> .\n<http://example.com/Patient1> <http://hl7.org/fhir/isHappy> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .\n',
);
});
@ -437,16 +441,21 @@ describe("jsonldDatasetProxy", () => {
it("replaces a primitive value that currently exists", async () => {
const [dataset, observation] = await getEmptyObservationDataset();
dataset.add(
dataset.addAll([
quad(
namedNode("http://example.com/Observation1"),
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
namedNode("http://hl7.org/fhir/Observation"),
),
quad(
namedNode("http://example.com/Observation1"),
namedNode("http://hl7.org/fhir/notes"),
literal("Cool Notes"),
),
);
]);
observation.notes = "Lame Notes";
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/notes> "Lame Notes" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/notes> "Lame Notes" .\n',
);
});
@ -454,11 +463,13 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getEmptyObservationDataset();
const patient: PatientShape = {
"@id": "http://example.com/Patient1",
type: { "@id": "Patient" },
birthdate: "2001-01-01",
};
observation.type = { "@id": "Observation" };
observation.subject = patient;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/birthdate> "2001-01-01"^^<http://www.w3.org/2001/XMLSchema#date> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/birthdate> "2001-01-01"^^<http://www.w3.org/2001/XMLSchema#date> .\n',
);
});
@ -477,13 +488,14 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
observation.subject = undefined;
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
it("Creates a blank node if the id is blank during set", async () => {
const [dataset, observation] = await getEmptyObservationDataset();
observation.subject = { name: ["Joe"] };
observation.type = { "@id": "Observation" };
observation.subject = { type: { "@id": "Patient" }, name: ["Joe"] };
expect(observation.subject?.["@id"]).toBeUndefined();
expect(observation.subject.name).toEqual(["Joe"]);
expect(
@ -500,12 +512,14 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getEmptyObservationDataset();
const patient: PatientShape = {
"@id": "http://example.com/Patient1",
type: { "@id": "Patient" },
birthdate: "2001-01-01",
name: ["Jon", "Bon", "Jovi"],
};
observation.type = { "@id": "Observation" };
observation.subject = patient;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/birthdate> "2001-01-01"^^<http://www.w3.org/2001/XMLSchema#date> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Jon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Jovi" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/birthdate> "2001-01-01"^^<http://www.w3.org/2001/XMLSchema#date> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Jon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Jovi" .\n',
);
});
@ -513,41 +527,47 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getEmptyObservationDataset();
const patient1: PatientShape = {
"@id": "http://example.com/Patient1",
type: { "@id": "Patient" },
name: ["jon"],
};
const patient2: PatientShape = {
"@id": "http://example.com/patient2",
type: { "@id": "Patient" },
name: ["jane"],
roommate: [patient1],
};
patient1.roommate = [patient2];
observation.type = { "@id": "Observation" };
observation.subject = patient1;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "jon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/patient2> .\n<http://example.com/patient2> <http://hl7.org/fhir/name> "jane" .\n<http://example.com/patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "jon" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/patient2> .\n<http://example.com/patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/patient2> <http://hl7.org/fhir/name> "jane" .\n<http://example.com/patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
it("adds a proxy object to the array", async () => {
const [, , builder] = await getTinyLoadedDataset();
const patient3 = builder.fromSubject(
const patient3: PatientShape = builder.fromSubject(
namedNode("http://example.com/Patient3"),
);
const patient1 = builder.fromSubject(
patient3.type = { "@id": "Patient" };
const patient1: PatientShape = builder.fromSubject(
namedNode("http://example.com/Patient1"),
);
patient3.roommate.push(patient1);
patient3.roommate?.push(patient1);
});
it("sets a primitive on an array", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
(patient.name as string[])[0] = "jon";
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "jon" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "jon" .\n',
);
});
it("sets a primitive on an array and overwrites one that already is there", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
dataset.add(
quad(
namedNode("http://example.com/Patient1"),
@ -557,15 +577,16 @@ describe("jsonldDatasetProxy", () => {
);
(patient.name as string[])[0] = "not jon";
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "not jon" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "not jon" .\n',
);
});
it("sets an array", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name = ["Joe", "Mama"];
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Joe" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Mama" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Joe" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Mama" .\n',
);
});
@ -573,19 +594,23 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
const replacementPatient: PatientShape = {
"@id": "http://example.com/ReplacementPatient",
type: { "@id": "Patient" },
name: ["Jackson"],
};
observation.subject = replacementPatient;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/ReplacementPatient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n<http://example.com/ReplacementPatient> <http://hl7.org/fhir/name> "Jackson" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/ReplacementPatient> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n<http://example.com/ReplacementPatient> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/ReplacementPatient> <http://hl7.org/fhir/name> "Jackson" .\n',
);
});
it("Does not overwrite the full object when a partial object is provided", async () => {
const [dataset, observation] = await getTinyLoadedDataset();
observation.subject = { "@id": "http://example.com/Patient2" };
observation.subject = {
"@id": "http://example.com/Patient2",
type: { "@id": "Patient" },
};
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient2> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient2> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
@ -593,12 +618,13 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
const replacementPatient: PatientShape = {
"@id": "http://example.com/ReplacementPatient",
type: { "@id": "Patient" },
name: ["Jackson"],
};
const roommateArr = observation?.subject?.roommate as PatientShape[];
roommateArr[0] = replacementPatient;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/ReplacementPatient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n<http://example.com/ReplacementPatient> <http://hl7.org/fhir/name> "Jackson" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/ReplacementPatient> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n<http://example.com/ReplacementPatient> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/ReplacementPatient> <http://hl7.org/fhir/name> "Jackson" .\n',
);
});
@ -607,6 +633,7 @@ describe("jsonldDatasetProxy", () => {
const roommateArr = observation.subject?.roommate as PatientShape[];
roommateArr[0] = {
"@id": "http://example.com/ReplacementPatient",
type: { "@id": "Patient" },
name: ["Jackson"],
};
expect(roommateArr.length).toBe(2);
@ -619,7 +646,7 @@ describe("jsonldDatasetProxy", () => {
patient["@id"] = "http://example.com/RenamedPatient";
expect(patient["@id"]).toBe("http://example.com/RenamedPatient");
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/RenamedPatient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/RenamedPatient> .\n<http://example.com/RenamedPatient> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/RenamedPatient> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/RenamedPatient> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/RenamedPatient> .\n<http://example.com/RenamedPatient> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/RenamedPatient> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/RenamedPatient> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n',
);
});
@ -627,7 +654,7 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
delete observation.subject;
expect(dataset.toString()).toBe(
'<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n',
);
});
@ -635,7 +662,7 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
delete observation.subject?.name;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
@ -643,23 +670,23 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
delete observation.subject?.roommate?.[0];
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n',
);
});
it("Removes all adjoining triples when garbage collection is indicated via the delete operator on an array with blank nodes", async () => {
const [dataset, observation] = await getTinyLoadedDatasetWithBlankNodes();
const deletedBlankNode =
observation.subject?.roommate?.[0][_getUnderlyingNode];
delete observation.subject?.roommate?.[0];
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> _:b26_Patient1 .\n_:b26_Patient1 <http://hl7.org/fhir/name> "Garrett" .\n',
);
expect(dataset.match(deletedBlankNode).size).toBe(0);
});
it("Removes a literal in an array when using the delete operator", async () => {
const [dataset, observation] = await getTinyLoadedDataset();
delete observation.subject?.name?.[0];
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
@ -670,7 +697,7 @@ describe("jsonldDatasetProxy", () => {
delete observation["@id"];
expect(observation).toEqual({ "@id": "http://example.com/Observation1" });
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
@ -711,27 +738,29 @@ describe("jsonldDatasetProxy", () => {
const [dataset, observation] = await getTinyLoadedDataset();
const replacementPatient: PatientShape = {
"@id": "http://example.com/Patient1",
type: { "@id": "Patient" },
name: ["Mister Sneaky"],
};
observation.subject = replacementPatient;
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Mister Sneaky" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Mister Sneaky" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
it("handles Object.assign", async () => {
const [dataset, observation] = await getTinyLoadedDataset();
Object.assign(observation, {
Object.assign(observation.subject!, {
age: 35,
isHappy: true,
});
expect(dataset.toString()).toBe(
'<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Observation1> <http://hl7.org/fhir/age> "35"^^<http://www.w3.org/2001/XMLSchema#integer> .\n<http://example.com/Observation1> <http://hl7.org/fhir/isHappy> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
'<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/roommate> <http://example.com/Patient2> .\n<http://example.com/Patient1> <http://hl7.org/fhir/age> "35"^^<http://www.w3.org/2001/XMLSchema#integer> .\n<http://example.com/Patient1> <http://hl7.org/fhir/isHappy> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .\n<http://example.com/Patient2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient2> <http://hl7.org/fhir/name> "Rob" .\n<http://example.com/Patient2> <http://hl7.org/fhir/roommate> <http://example.com/Patient1> .\n',
);
});
it("Adds elements to the array even if they were modified by the datastore", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name = ["Joe", "Blow"];
dataset.add(
quad(
@ -745,6 +774,7 @@ describe("jsonldDatasetProxy", () => {
it("Removes elements from the array even if they were modified by the datastore", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name = ["Joe", "Blow"];
dataset.delete(
quad(
@ -758,6 +788,7 @@ describe("jsonldDatasetProxy", () => {
it("Removes and adds from the array even if they were modified by the datastore", async () => {
const [dataset, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name = ["Joe", "Blow"];
dataset.delete(
quad(
@ -793,7 +824,10 @@ describe("jsonldDatasetProxy", () => {
it("Prevents duplicates for Objects", async () => {
const [, observation] = await getLoadedDataset();
const roommates = observation.subject?.roommate as PatientShape[];
roommates[0] = { "@id": "http://example.com/Patient3" };
roommates[0] = {
"@id": "http://example.com/Patient3",
type: { "@id": "Patient" },
};
expect(roommates.length).toBe(1);
expect(roommates[0].name?.[0]).toBe("Amy");
});
@ -824,12 +858,13 @@ describe("jsonldDatasetProxy", () => {
delete arr[5];
expect(arr).toEqual(["Garrett", "Bobby", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
it("Can set a triple object named node with just a string", async () => {
const [dataset, observation] = await getEmptyObservationDataset();
observation.type = { "@id": "Observation" };
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
observation.subject = "http://example.com/Patient1";
@ -837,7 +872,7 @@ describe("jsonldDatasetProxy", () => {
"@id": "http://example.com/Patient1",
});
expect(dataset.toString()).toBe(
"<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n",
"<http://example.com/Observation1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Observation> .\n<http://example.com/Observation1> <http://hl7.org/fhir/subject> <http://example.com/Patient1> .\n",
);
});
@ -848,7 +883,7 @@ describe("jsonldDatasetProxy", () => {
arr.copyWithin(0, 2, 3);
expect(arr).toEqual(["Ferguson", "Bobby"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -858,7 +893,7 @@ describe("jsonldDatasetProxy", () => {
arr.copyWithin(0, 2);
expect(arr).toEqual(["Ferguson", "Bobby"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -870,7 +905,7 @@ describe("jsonldDatasetProxy", () => {
expect(() => arr.copyWithin(0, undefined, 2)).not.toThrowError();
expect(arr).toEqual(["Garrett", "Bobby", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -880,7 +915,7 @@ describe("jsonldDatasetProxy", () => {
arr.fill("Beepy", 2, 5);
expect(arr).toEqual(["Garrett", "Bobby", "Beepy"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
);
});
@ -890,7 +925,7 @@ describe("jsonldDatasetProxy", () => {
expect(arr.pop()).toBe("Ferguson");
expect(arr).toEqual(["Garrett", "Bobby"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n',
);
});
@ -906,7 +941,7 @@ describe("jsonldDatasetProxy", () => {
arr.push("Beepy");
expect(arr).toEqual(["Garrett", "Bobby", "Ferguson", "Beepy"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
);
});
@ -915,7 +950,7 @@ describe("jsonldDatasetProxy", () => {
patient.name?.reverse();
expect(patient.name).toEqual(["Ferguson", "Bobby", "Garrett"]);
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -925,7 +960,7 @@ describe("jsonldDatasetProxy", () => {
expect(arr.shift()).toEqual("Garrett");
expect(arr).toEqual(["Bobby", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -942,7 +977,7 @@ describe("jsonldDatasetProxy", () => {
});
expect(patient.name).toEqual(["Bobby", "Garrett", "Ferguson"]);
expect(dataset.toString()).toBe(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -974,7 +1009,7 @@ describe("jsonldDatasetProxy", () => {
arr.splice(1, 0, "Beepy");
expect(arr).toEqual(["Garrett", "Beepy", "Bobby", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
);
});
@ -1008,7 +1043,7 @@ describe("jsonldDatasetProxy", () => {
arr.splice(1, 1);
expect(arr).toEqual(["Garrett", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n',
);
});
@ -1018,7 +1053,7 @@ describe("jsonldDatasetProxy", () => {
arr.unshift("Beepy");
expect(arr).toEqual(["Beepy", "Garrett", "Bobby", "Ferguson"]);
expect(dataset.toString()).toEqual(
'<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
'<http://example.com/Patient1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Garrett" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Bobby" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Ferguson" .\n<http://example.com/Patient1> <http://hl7.org/fhir/name> "Beepy" .\n',
);
});
});
@ -1132,6 +1167,8 @@ describe("jsonldDatasetProxy", () => {
it("errors if an object is added without the correct parameters", async () => {
expect(() =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
patients.push({
"@id": "http://example.com/Patient4",
name: ["Dippy"],
@ -1241,7 +1278,11 @@ describe("jsonldDatasetProxy", () => {
it("cannot write to a collection that matches the null, predicate, null pattern", () => {
expect(
() => (patients[1] = { "@id": "http://example.com/Patient4" }),
() =>
(patients[1] = {
"@id": "http://example.com/Patient4",
type: { "@id": "Patient" },
}),
).toThrow(
"A collection that does not specify a match for both a subject or predicate cannot be modified directly.",
);
@ -1267,11 +1308,13 @@ describe("jsonldDatasetProxy", () => {
it("initializes a patient using the fromJSON method", async () => {
const [, , builder] = await getEmptyPatientDataset();
const patient = builder.fromJson<PatientShape>({
type: { "@id": "Patient" },
name: ["Jack", "Horner"],
birthdate: "1725/11/03",
age: 298,
roommate: [
{
type: { "@id": "Patient" },
name: ["Ethical", "Bug"],
},
],
@ -1288,11 +1331,13 @@ describe("jsonldDatasetProxy", () => {
const [, , builder] = await getEmptyPatientDataset();
const patient = builder.fromJson<PatientShape>({
"@id": "http://example.com/Patient13",
type: { "@id": "Patient" },
name: ["Jack", "Horner"],
birthdate: "1725/11/03",
age: 298,
roommate: [
{
type: { "@id": "Patient" },
name: ["Ethical", "Bug"],
},
],
@ -1314,9 +1359,10 @@ describe("jsonldDatasetProxy", () => {
const patient4 = builder
.write(namedNode("http://example.com/Patient4Doc"))
.fromSubject<PatientShape>(namedNode("https://example.com/Patient4"));
patient4.type = { "@id": "Patient" };
patient4.name = ["Jackson"];
expect(dataset.toString()).toBe(
'<https://example.com/Patient4> <http://hl7.org/fhir/name> "Jackson" <http://example.com/Patient4Doc> .\n',
'<https://example.com/Patient4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://hl7.org/fhir/Patient> <http://example.com/Patient4Doc> .\n<https://example.com/Patient4> <http://hl7.org/fhir/name> "Jackson" <http://example.com/Patient4Doc> .\n',
);
});
});
@ -1399,6 +1445,7 @@ describe("jsonldDatasetProxy", () => {
const doc3 = namedNode("http://example.com/Doc3");
const [, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name?.push("default");
const end1 = write(doc1).using(patient);
patient.name?.push("1");
@ -1426,6 +1473,7 @@ describe("jsonldDatasetProxy", () => {
const doc1 = namedNode("http://example.com/Doc1");
const [, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" };
patient.name?.push("Default");
const [patientOnDoc1] = write(doc1).usingCopy(patient);
patientOnDoc1.name?.push("Doc1");
@ -1676,4 +1724,14 @@ describe("jsonldDatasetProxy", () => {
expect(enSet.size).toBe(0);
});
});
});
};
describe(
"jsonldDatasetProxy - unnested context",
testJsonldDatasetProxy(patientUnnestedContext),
);
describe(
"jsonldDatasetProxy - nested context",
testJsonldDatasetProxy(patientNestedContext),
);

@ -1,9 +1,11 @@
import type { ContextDefinition } from "jsonld";
import type { Schema } from "shexj";
import type { LdoJsonldContext } from "../src/LdoJsonldContext";
export interface ObservationShape {
"@id"?: string;
"@context"?: ContextDefinition;
type: { "@id": "Observation" };
subject?: PatientShape;
notes?: string;
langNotes?: string;
@ -12,7 +14,7 @@ export interface ObservationShape {
export type PatientShape = {
"@id"?: string;
"@context"?: ContextDefinition;
type?: { "@id": "Patient" };
type: { "@id": "Patient" };
name?: string[];
langName?: string[];
birthdate?: string;
@ -26,11 +28,12 @@ export type PatientShape = {
// @ts-ignore
export const patientSchema: Schema = {};
export const patientContext: ContextDefinition = {
export const patientUnnestedContext: ContextDefinition = {
type: {
"@id": "@type",
},
Patient: "http://hl7.org/fhir/Patient",
Observation: "http://hl7.org/fhir/Observation",
subject: { "@id": "http://hl7.org/fhir/subject", "@type": "@id" },
name: {
"@id": "http://hl7.org/fhir/name",
@ -69,17 +72,69 @@ export const patientContext: ContextDefinition = {
},
};
export const patientNestedContext: LdoJsonldContext = {
type: {
"@id": "@type",
},
Observation: {
"@id": "http://hl7.org/fhir/Observation",
"@context": {
notes: {
"@id": "http://hl7.org/fhir/notes",
"@type": "http://www.w3.org/2001/XMLSchema#string",
},
langNotes: {
"@id": "http://hl7.org/fhir/langNotes",
"@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString",
},
subject: { "@id": "http://hl7.org/fhir/subject", "@type": "@id" },
},
},
Patient: {
"@id": "http://hl7.org/fhir/Patient",
"@context": {
name: {
"@id": "http://hl7.org/fhir/name",
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@isCollection": true,
},
langName: {
"@id": "http://hl7.org/fhir/langName",
"@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString",
"@isCollection": true,
},
birthdate: {
"@id": "http://hl7.org/fhir/birthdate",
"@type": "http://www.w3.org/2001/XMLSchema#date",
},
age: {
"@id": "http://hl7.org/fhir/age",
"@type": "http://www.w3.org/2001/XMLSchema#integer",
},
isHappy: {
"@id": "http://hl7.org/fhir/isHappy",
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
},
roommate: {
"@id": "http://hl7.org/fhir/roommate",
"@type": "@id",
"@isCollection": true,
},
},
},
};
export const patientData = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
example:Observation1
example:Observation1 a fhir:Observation ;
fhir:notes "Cool Notes"^^xsd:string ;
fhir:subject example:Patient1 .
example:Patient1
example:Patient1 a fhir:Patient ;
rdf:type fhir:Patient ;
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string ;
fhir:birthdate "1986-01-01"^^xsd:date ;
@ -87,7 +142,7 @@ example:Patient1
fhir:isHappy "true"^^xsd:boolean ;
fhir:roommate example:Patient2, example:Patient3 .
example:Patient2
example:Patient2 a fhir:Patient ;
rdf:type fhir:Patient ;
fhir:name "Rob"^^xsd:string ;
fhir:birthdate "1987-01-01"^^xsd:date ;
@ -95,7 +150,7 @@ example:Patient2
fhir:isHappy "false"^^xsd:boolean ;
fhir:roommate example:Patient1, example:Patient3 .
example:Patient3
example:Patient3 a fhir:Patient ;
rdf:type fhir:Patient ;
fhir:name "Amy"^^xsd:string ;
fhir:birthdate "1988-01-01"^^xsd:date ;
@ -108,25 +163,25 @@ export const patientDataWithBlankNodes = `
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
example:Observation1 a fhir:Observation ;
fhir:notes "Cool Notes"^^xsd:string ;
fhir:subject _:Patient1 .
_:Patient1
_:Patient1 a fhir:Patient ;
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string ;
fhir:birthdate "1986-01-01"^^xsd:date ;
fhir:age "35"^^xsd:integer ;
fhir:isHappy "true"^^xsd:boolean ;
fhir:roommate _:Patient2, _:Patient3 .
_:Patient2
_:Patient2 a fhir:Patient ;
fhir:name "Rob"^^xsd:string ;
fhir:birthdate "1987-01-01"^^xsd:date ;
fhir:age "34"^^xsd:integer ;
fhir:isHappy "false"^^xsd:boolean ;
fhir:roommate _:Patient1, _:Patient3 .
_:Patient3
_:Patient3 a fhir:Patient ;
fhir:name "Amy"^^xsd:string ;
fhir:birthdate "1988-01-01"^^xsd:date ;
fhir:age "33"^^xsd:integer ;
@ -138,14 +193,14 @@ export const tinyPatientData = `
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
example:Observation1 a fhir:Observation ;
fhir:subject example:Patient1 .
example:Patient1
example:Patient1 a fhir:Patient ;
fhir:name "Garrett"^^xsd:string ;
fhir:roommate example:Patient2 .
example:Patient2
example:Patient2 a fhir:Patient ;
fhir:name "Rob"^^xsd:string ;
fhir:roommate example:Patient1 .
`;
@ -155,7 +210,7 @@ export const tinyArrayPatientData = `
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Patient1
example:Patient1 a fhir:Patient ;
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string .
`;
@ -164,14 +219,14 @@ export const tinyPatientDataWithBlankNodes = `
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
example:Observation1 a fhir:Observation ;
fhir:subject _:Patient1 .
_:Patient1
_:Patient1 a fhir:Patient ;
fhir:name "Garrett"^^xsd:string ;
fhir:roommate _:Patient2 .
_:Patient2
_:Patient2 a fhir:Patient ;
fhir:name "Rob"^^xsd:string ;
fhir:roommate _:Patient1 .
`;
@ -181,14 +236,14 @@ export const tinyPatientDataWithLanguageTags = `
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
example:Observation1 a fhir:Observation ;
fhir:subject example:Patient1 ;
fhir:langNotes "Cool Notes" ;
fhir:langNotes "Cooler Notes"@en ;
fhir:langNotes "Notas Geniales"@es ;
fhir:langNotes "Notes Sympas"@fr .
example:Patient1
example:Patient1 a fhir:Patient ;
fhir:langName "Jon" ;
fhir:langName "John"@en ;
fhir:langName "Juan"@es ;

@ -1,11 +1,11 @@
import type { ContextDefinition } from "jsonld";
import type { Schema } from "shexj";
import type { LdoJsonldContext } from "../src/LdoJsonldContext";
export interface Bender {
"@id"?: string;
"@context"?: ContextDefinition;
type: "Bender";
name: string;
element: Element;
friend: (Bender | NonBender | Avatar)[];
}
@ -14,6 +14,7 @@ export interface Avatar {
"@id"?: string;
"@context"?: ContextDefinition;
type: "Avatar";
name: string;
element: Element[];
friend: (Bender | NonBender | Avatar)[];
}
@ -22,6 +23,7 @@ export interface NonBender {
"@id"?: string;
"@context"?: ContextDefinition;
type: "Avatar";
name: string;
friend: (Bender | NonBender | Avatar)[];
}
@ -39,13 +41,16 @@ export type Element =
"@id": "CreativeWork";
};
export const patientContext: LdoJsonldContext = {
export const scopedContext: LdoJsonldContext = {
Bender: {
"@id": "https://example.com/Bender",
"@context": {
type: {
"@id": "@type",
},
name: {
"@id": "https://example.com/name",
},
element: {
"@id": "https://example.com/element",
},
@ -56,11 +61,14 @@ export const patientContext: LdoJsonldContext = {
},
},
Avatar: {
"@id": "https://example.com/Bender",
"@id": "https://example.com/Avatar",
"@context": {
type: {
"@id": "@type",
},
name: {
"@id": "https://example.com/name",
},
element: {
"@id": "https://example.com/element",
"@isCollection": true,
@ -77,6 +85,9 @@ export const patientContext: LdoJsonldContext = {
type: {
"@id": "@type",
},
name: {
"@id": "https://example.com/name",
},
friend: {
"@id": "https://example.com/friend",
"@isCollection": true,
@ -85,113 +96,24 @@ export const patientContext: LdoJsonldContext = {
},
};
export const patientData = `
@prefix example: <http://example.com/> .
export const scopedData = `
@prefix example: <https://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
example:Aang a example:Avatar
example:Aang a example:Avatar ;
example:name "Aang" ;
example:element example:Air, example:Water ;
example:friend example:Sokka, example:Katara .
example:Katara a example:Avatar
example:Katara a example:Bender ;
example:name "Katara" ;
example:element example:Water ;
example:friend example:Sokka, example:Aang .
example:Sokka a example:Avatar
example:Sokka a example:NonBender ;
example:name "Sokka" ;
example:element example:Water ;
example:friend example:Sokka, example:Aang .
`;
export const patientDataWithBlankNodes = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
fhir:notes "Cool Notes"^^xsd:string ;
fhir:subject _:Patient1 .
_:Patient1
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string ;
fhir:birthdate "1986-01-01"^^xsd:date ;
fhir:age "35"^^xsd:integer ;
fhir:isHappy "true"^^xsd:boolean ;
fhir:roommate _:Patient2, _:Patient3 .
_:Patient2
fhir:name "Rob"^^xsd:string ;
fhir:birthdate "1987-01-01"^^xsd:date ;
fhir:age "34"^^xsd:integer ;
fhir:isHappy "false"^^xsd:boolean ;
fhir:roommate _:Patient1, _:Patient3 .
_:Patient3
fhir:name "Amy"^^xsd:string ;
fhir:birthdate "1988-01-01"^^xsd:date ;
fhir:age "33"^^xsd:integer ;
fhir:isHappy "true"^^xsd:boolean .
`;
export const tinyPatientData = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
fhir:subject example:Patient1 .
example:Patient1
fhir:name "Garrett"^^xsd:string ;
fhir:roommate example:Patient2 .
example:Patient2
fhir:name "Rob"^^xsd:string ;
fhir:roommate example:Patient1 .
`;
export const tinyArrayPatientData = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Patient1
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string .
`;
export const tinyPatientDataWithBlankNodes = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
fhir:subject _:Patient1 .
_:Patient1
fhir:name "Garrett"^^xsd:string ;
fhir:roommate _:Patient2 .
_:Patient2
fhir:name "Rob"^^xsd:string ;
fhir:roommate _:Patient1 .
`;
export const tinyPatientDataWithLanguageTags = `
@prefix example: <http://example.com/> .
@prefix fhir: <http://hl7.org/fhir/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
example:Observation1
fhir:subject example:Patient1 ;
fhir:langNotes "Cool Notes" ;
fhir:langNotes "Cooler Notes"@en ;
fhir:langNotes "Notas Geniales"@es ;
fhir:langNotes "Notes Sympas"@fr .
example:Patient1
fhir:langName "Jon" ;
fhir:langName "John"@en ;
fhir:langName "Juan"@es ;
fhir:langName "Jean"@fr .
`;

Loading…
Cancel
Save