Skip to content

Commit

Permalink
Handle Code<T> as generic sub type of DataType. Add tests for additio…
Browse files Browse the repository at this point in the history
…nal types.
  • Loading branch information
jjrdk committed Mar 23, 2021
1 parent 4546499 commit 0a791c0
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 65 deletions.
153 changes: 100 additions & 53 deletions src/Spark.Engine.Test/Service/PatchServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,83 @@

public class PatchServiceTests
{
private readonly FhirJsonParser _jsonParser = new FhirJsonParser();
private readonly PatchService _applier = new PatchService();
private readonly FhirXmlParser _xmlParser = new FhirXmlParser();

[Fact]
public void CanReplaceStatusOnMedicationRequest()
{
var json = File.ReadAllText(Path.Combine("TestData", "R4", "medication-status-replace-patch.json"));
var parameters = _jsonParser.Parse<Parameters>(json);

var resource = new MedicationRequest {Id = "test"};
resource = (MedicationRequest) _applier.Apply(resource, parameters);

Assert.Equal(MedicationRequest.medicationrequestStatus.Completed, resource.Status);
}

[Fact]
public void CanReplacePerformerTypeOnMedicationRequest()
{
var resource = new MedicationRequest {Id = "test"};
var json = File.ReadAllText(
Path.Combine("TestData", "R4", "medication-replace-codeable-concept-patch.json"));
var parameters = _jsonParser.Parse<Parameters>(json);

resource = (MedicationRequest) _applier.Apply(resource, parameters);

Assert.Equal("abc", resource.PerformerType.Coding[0].System);
Assert.Equal("123", resource.PerformerType.Coding[0].Code);
Assert.Equal("test", resource.PerformerType.Text);
}

[Fact]
public void CanReplaceSubjectOnMedicationRequest()
{
var resource = new MedicationRequest {Id = "test", Subject = new ResourceReference("abc")};
var json = File.ReadAllText(
Path.Combine("TestData", "R4", "medication-replace-resource-reference-patch.json"));
var parameters = _jsonParser.Parse<Parameters>(json);

resource = (MedicationRequest) _applier.Apply(resource, parameters);

Assert.Equal("abc", resource.Subject.Reference);
}

[Fact]
public void CanReplaceInstantiatesCanonicalOnMedicationRequest()
{
var resource = new MedicationRequest {Id = "test"};
var json = File.ReadAllText(Path.Combine("TestData", "R4", "medication-replace-canonical-patch.json"));
var parameters = _jsonParser.Parse<Parameters>(json);

resource = (MedicationRequest) _applier.Apply(resource, parameters);

Assert.Equal("abc", resource.InstantiatesCanonical.First());
}

[Fact]
public void CanReplaceDosageOnMedicationRequest()
{
var resource = new MedicationRequest {Id = "test"};
var json = File.ReadAllText(
Path.Combine("TestData", "R4", "medication-replace-dosage-instruction-patch.json"));
var parameters = _jsonParser.Parse<Parameters>(json);

resource = (MedicationRequest) _applier.Apply(resource, parameters);

Assert.Equal(1m, resource.DosageInstruction[0].MaxDosePerLifetime.Value);
}

[Fact]
public void CanApplyPropertyAssignmentPatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "property-assignment-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient { Id = "test" };
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
var resource = new Patient {Id = "test"};
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("1930-01-01", resource.BirthDate);
}
Expand All @@ -29,25 +96,21 @@ public void CanApplyPropertyAssignmentPatch()
public void WhenApplyingPropertyAssignmentPatchToNonEmptyPropertyThenThrows()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "property-assignment-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient {Id = "test", BirthDate = "1930-01-01"};
var applier = new PatchService();

Assert.Throws<TargetInvocationException>(() => applier.Apply(resource, parameters));
Assert.Throws<TargetInvocationException>(() => _applier.Apply(resource, parameters));
}

[Fact]
public void CanApplyCollectionAddPatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "collection-add-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient { Id = "test" };
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
var resource = new Patient {Id = "test"};
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("John", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -57,16 +120,13 @@ public void CanApplyCollectionAddPatch()
public void CanApplyCollectionReplacePatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "collection-replace-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient
{
Id = "test",
Name = { new HumanName { Given = new[] { "John" }, Family = "Johnson" } }
Id = "test", Name = {new HumanName {Given = new[] {"John"}, Family = "Johnson"}}
};
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("Jane", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -77,20 +137,17 @@ public void CanFullResourceReplacePatch()
{
var resource = new Patient
{
Id = "test",
Name = { new HumanName { Given = new[] { "John" }, Family = "Johnson" } }
Id = "test", Name = {new HumanName {Given = new[] {"John"}, Family = "Johnson"}}
};

var replacement = new Patient
{
Id = "test",
Name = { new HumanName { Given = new[] { "Jane" }, Family = "Doe" } }
Id = "test", Name = {new HumanName {Given = new[] {"Jane"}, Family = "Doe"}}
};

var applier = new PatchService();
var parameters = replacement.ToPatch();

resource = (Patient)applier.Apply(resource, parameters);
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("Jane", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -103,14 +160,14 @@ public void CanCreateDiffPatch()
{
Id = "test",
Gender = AdministrativeGender.Male,
Name = { new HumanName { Given = new[] { "John" }, Family = "Johnson" } }
Name = {new HumanName {Given = new[] {"John"}, Family = "Johnson"}}
};

var replacement = new Patient
{
Id = "test",
BirthDateElement = new Hl7.Fhir.Model.Date(2020, 1, 2),
Name = { new HumanName { Given = new[] { "Jane" }, Family = "Doe" } }
Name = {new HumanName {Given = new[] {"Jane"}, Family = "Doe"}}
};

var parameters = replacement.ToPatch(resource);
Expand All @@ -125,21 +182,20 @@ public void CanApplyCreatedDiffPatch()
{
Id = "test",
Gender = AdministrativeGender.Male,
Name = { new HumanName { Given = new[] { "John" }, Family = "Johnson" } }
Name = {new HumanName {Given = new[] {"John"}, Family = "Johnson"}}
};

var replacement = new Patient
{
Id = "test",
BirthDateElement = new Hl7.Fhir.Model.Date(2020, 1, 2),
Name = { new HumanName { Given = new[] { "Jane" }, Family = "Doe" } }
Name = {new HumanName {Given = new[] {"Jane"}, Family = "Doe"}}
};

var patch = replacement.ToPatch(resource);
var service = new PatchService();
service.Apply(resource, patch);
_applier.Apply(resource, patch);

Assert.Null(resource.Gender);
Assert.False(resource.Gender.HasValue);
Assert.Equal(replacement.BirthDate, resource.BirthDate);
Assert.Equal("Jane", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -149,16 +205,13 @@ public void CanApplyCreatedDiffPatch()
public void CanApplyCollectionInsertPatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "collection-insert-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient
{
Id = "test",
Name = { new HumanName { Given = new[] { "John" }, Family = "Johnson" } }
Id = "test", Name = {new HumanName {Given = new[] {"John"}, Family = "Johnson"}}
};
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("Jane", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -171,8 +224,7 @@ public void CanApplyCollectionInsertPatch()
public void CanApplyCollectionMovePatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "collection-move-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient
{
Expand All @@ -183,8 +235,7 @@ public void CanApplyCollectionMovePatch()
new HumanName {Given = new[] {"Jane"}, Family = "Doe"}
}
};
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("Jane", resource.Name[0].Given.First());
Assert.Equal("Doe", resource.Name[0].Family);
Expand All @@ -197,12 +248,10 @@ public void CanApplyCollectionMovePatch()
public void CanApplyPropertyReplacementPatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "property-replace-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient { Id = "test", BirthDate = "1970-12-24" };
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
var resource = new Patient {Id = "test", BirthDate = "1970-12-24"};
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Equal("1930-01-01", resource.BirthDate);
}
Expand All @@ -211,12 +260,10 @@ public void CanApplyPropertyReplacementPatch()
public void CanApplyCollectionDeletePatch()
{
var xml = File.ReadAllText(Path.Combine("TestData", "R4", "collection-delete-patch.xml"));
var parser = new FhirXmlParser();
var parameters = parser.Parse<Parameters>(xml);
var parameters = _xmlParser.Parse<Parameters>(xml);

var resource = new Patient { Id = "test", Name = { new HumanName { Text = "John Doe" } } };
var applier = new PatchService();
resource = (Patient)applier.Apply(resource, parameters);
var resource = new Patient {Id = "test", Name = {new HumanName {Text = "John Doe"}}};
resource = (Patient) _applier.Apply(resource, parameters);

Assert.Empty(resource.Name);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Spark.Engine.Test/Spark.Engine.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
<None Update="TestData\R4\collection-replace-patch.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\medication-replace-canonical-patch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\medication-replace-codeable-concept-patch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\medication-replace-dosage-instruction-patch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\medication-replace-resource-reference-patch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\medication-status-replace-patch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\R4\patient-example.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"resourceType": "Parameters",
"parameter": [
{
"name": "operation",
"part": [
{
"name": "path",
"valueString": "MedicationRequest"
},
{
"name": "type",
"valueCode": "add"
},
{
"name": "name",
"valueString": "instantiatesCanonical"
},
{
"name": "value",
"valueCanonical": "abc"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"resourceType": "Parameters",
"parameter": [
{
"name": "operation",
"part": [
{
"name": "path",
"valueString": "MedicationRequest.performerType"
},
{
"name": "type",
"valueCode": "replace"
},
{
"name": "value",
"part": [
{
"valueCodeableConcept": {
"coding": [
{
"system": "abc",
"code": "123"
}
],
"text": "test"
}
}
]
}
]
},
{
"name": "operation",
"part": [
{
"name": "path",
"valueString": "MedicationRequest.id"
},
{
"name": "type",
"valueCode": "replace"
},
{
"name": "value",
"valueId": "test"
}
]
}
]
}
Loading

0 comments on commit 0a791c0

Please sign in to comment.