Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: specimen source handling #22

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,790 changes: 5,790 additions & 0 deletions data/SampleData/eCR/eCR_full.xml

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions data/Templates/eCR/Entry/Result/_entry.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
{% include 'Reference/DiagnosticReport/Performer' ID: diagnosticId, REF: fullPerformerId -%}
{% endif -%}
{% endif -%}
{% assign collectTime = null %}
{% if entry.organizer.effectiveTime.low and entry.organizer.effectiveTime.low.value -%}
{% assign collectTime = entry.organizer.effectiveTime.low.value -%}
{% elsif entry.organizer.effectiveTime.high and entry.organizer.effectiveTime.high.value -%}
{% assign collectTime = entry.organizer.effectiveTime.high.value -%}
{% elsif entry.organizer.effectiveTime and entry.organizer.effectiveTime.value and entry.organizer.effectiveTime.value != "UNK"-%}
{% assign collectTime = entry.organizer.effectiveTime.value -%}
{% else -%}
{% assign collectTime = "None" -%}
{% endif -%}

{% assign specValue = null %}
{% assign receiveTime = null %}
{% assign comps = entry.organizer.component | to_array -%}
{% for comp in comps -%}
{% if comp.procedure -%}
Expand All @@ -50,15 +51,15 @@
{% assign specValue = playingEntity.code.originalText._ -%}
{% elsif playingEntity.code.displayName -%}
{% assign specValue = playingEntity.code.displayName -%}
{% else -%}
{% assign specValue = "None" -%}
{% endif -%}
{% else -%}
{% assign specValue = "None" -%}
{% endif -%}
{% endif -%}
{% endfor-%}
{% endfor -%}
{% for component in comps %}
{% include 'Entry/Result/entry_organizer_component' with component, text: text %}
{% include 'Entry/Result/entry_organizer_component' with component, text: text, specValue: specValue, collectTime: collectTime, receiveTime: receiveTime, diagnosticId: diagnosticId %}
{% endfor %}
{% endif -%}
{% comment %} Prevent weird variable leakage {% endcomment %}
{% assign specValue = null %}
{% assign receiveTime = null %}
{% assign collectTime = null %}
8 changes: 4 additions & 4 deletions data/Templates/eCR/Resource/_Observation.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@
{% assign observationValueReferenceValue = observationEntry.value.reference.value -%}
"extension":
[ {
{% if specimenValue and specimenValue != "None" or collectTime and collectTime != "None" or observationTextReferenceValue != "None" or observationValueReferenceValue != "None" -%}
{% if specimenValue != null or collectTime != null or receiveTime != null -%}

"url" : "http://hl7.org/fhir/R4/specimen.html",
"extension": [
{% if specimenValue and specimenValue != "None" -%}
{% if specimenValue != null -%}
{
"url" : "specimen source",
"valueString" : "{{ specimenValue }}",
},
{% endif -%}
{% if collectTime and collectTime != "None" -%}
{% if collectTime != null -%}
{
"url" : "specimen collection time",
"valueDateTime" : "{{ collectTime | format_as_date_time }}",
},
{% endif -%}
{% if receiveTime and receiveTime != "None" -%}
{% if receiveTime != null -%}
{
"url" : "specimen receive time",
"valueDateTime" : "{{ receiveTime | format_as_date_time }}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -213,6 +214,20 @@ public static IEnumerable<object[]> GetDataForCcda()
});
}

public static IEnumerable<object[]> GetDataForEcr()
{
var data = new List<string[]>
{
new[] { @"EICR", @"eCR_full.xml", @"eCR_full-expected.json" },
};
return data.Select(item => new[]
{
item[0],
Path.Join(Constants.SampleDataDirectory, "eCR", item[1]),
Path.Join(Constants.ExpectedDataFolder, "eCR", item[0], item[2]),
});
}

public static IEnumerable<object[]> GetDataForJson()
{
var data = new List<string[]>
Expand Down Expand Up @@ -332,8 +347,17 @@ protected void ConvertCCDAMessageAndValidateExpectedResponse(ITemplateProvider t
var actualObject = JObject.Parse(actualContent);

// Remove DocumentReference, where date is different every time conversion is run and gzip result is OS dependent
expectedObject["entry"]?.Last()?.Remove();
actualObject["entry"]?.Last()?.Remove();
if (expectedObject["entry"]?.Last()["resource"]["resourceType"].ToString() == "DocumentReference")
{
expectedObject["entry"]?.Last()?.Remove();
actualObject["entry"]?.Last()?.Remove();
}

var diff = DiffHelper.FindDiff(actualObject, expectedObject);
if (diff.HasValues)
{
Console.WriteLine(diff);
}

Assert.True(JToken.DeepEquals(expectedObject, actualObject));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public void GivenCcdaDocument_WhenConverting_ExpectedFhirResourceShouldBeReturne
ConvertCCDAMessageAndValidateExpectedResponse(templateProvider, rootTemplate, inputFile, expectedFile);
}

[Theory]
[MemberData(nameof(GetDataForEcr))]
public void GivenEcrDocument_WhenConverting_ExpectedFhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
{
var templateDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, Constants.TemplateDirectory, "eCR");
var templateProvider = new TemplateProvider(templateDirectory, DataType.Ccda);

ConvertCCDAMessageAndValidateExpectedResponse(templateProvider, rootTemplate, inputFile, expectedFile);
}

[Theory]
[MemberData(nameof(GetDataForStu3ToR4))]
public void GivenStu3FhirData_WhenConverting_ExpectedR4FhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Linq;
using Newtonsoft.Json.Linq;

namespace Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests
{
// from https://stackoverflow.com/questions/24876082/find-and-return-json-differences-using-newtonsoft-in-c
public static class DiffHelper
{
public static JObject FindDiff(this JToken Current, JToken Model)
{
var diff = new JObject();
if (JToken.DeepEquals(Current, Model)) return diff;

switch(Current.Type)
{
case JTokenType.Object:
{
var current = Current as JObject;
var model = Model as JObject;
var addedKeys = current.Properties().Select(c => c.Name).Except(model.Properties().Select(c => c.Name));
var removedKeys = model.Properties().Select(c => c.Name).Except(current.Properties().Select(c => c.Name));
var unchangedKeys = current.Properties().Where(c => JToken.DeepEquals(c.Value, Model[c.Name])).Select(c => c.Name);
foreach (var k in addedKeys)
{
diff[k] = new JObject
{
["+"] = Current[k]
};
}
foreach (var k in removedKeys)
{
diff[k] = new JObject
{
["-"] = Model[k]
};
}
var potentiallyModifiedKeys = current.Properties().Select(c => c.Name).Except(addedKeys).Except(unchangedKeys);
foreach (var k in potentiallyModifiedKeys)
{
var foundDiff = FindDiff(current[k], model[k]);
if(foundDiff.HasValues) diff[k] = foundDiff;
}
}
break;
case JTokenType.Array:
{
var current = Current as JArray;
var model = Model as JArray;
var plus = new JArray(current.Except(model, new JTokenEqualityComparer()));
var minus = new JArray(model.Except(current, new JTokenEqualityComparer()));
if (plus.HasValues) diff["+"] = plus;
if (minus.HasValues) diff["-"] = minus;
}
break;
default:
diff["+"] = Current;
diff["-"] = Model;
break;
}

return diff;
}
}
}
Loading
Loading