Skip to content

Commit

Permalink
Merge branch 'main' into mcm/fix-spec-source
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmcgrath13 authored Dec 13, 2024
2 parents 9b76856 + ce1d320 commit 7546817
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 11 deletions.
10 changes: 5 additions & 5 deletions data/Templates/eCR/DataType/_Address.liquid
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{% assign lines = Address.streetAddressLine | to_array -%}
{% if lines.first._ or Address.city._ or Address.state._ or Address.country._ or Address.postalCode._ or Address.county._ -%}
"use": "{{ Address.use | get_property: 'ValueSet/AddressUse' }}",

"line": [
{% assign lines = Address.streetAddressLine | to_array -%}
{% for l in lines -%}
{%- assign lines = Address.streetAddressLine | to_array -%}
{%- for l in lines -%}
"{{l._}}",
{% endfor -%}
{%- endfor -%}
],
"city": "{{Address.city._}}",
"state": "{{Address.state._}}",
"country": "{{Address.country._}}",
"postalCode": "{{Address.postalCode._}}",
"district": "{{Address.county._}}",
{% endif -%}
"period": { {% include 'DataType/Period' Period: Address.useablePeriod %} },
{% endif -%}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests
{
public class BaseConvertLiquidTemplate
public class BaseECRLiquidTests
{

/// <summary>
/// Given a path to an eCR template, and attributes. Check that the rendered template
/// matches the expected contents.
/// </summary>
protected void ConvertCheckLiquidTemplate(string templatePath, Dictionary<string, object> attributes, string expectedContent)
/// <param name="templatePath">Path to the template being tested</param>
/// <param name="attributes">Dictionary of attributes to hydrate the template</param>
/// <param name="expectedContent">Serialized string that ought to be returned</param>
protected static void ConvertCheckLiquidTemplate(string templatePath, Dictionary<string, object> attributes, string expectedContent)
{
var templateContent = File.ReadAllText(templatePath);
var template = TemplateUtility.ParseLiquidTemplate(templatePath, templateContent);
Expand Down Expand Up @@ -53,7 +55,8 @@ protected void ConvertCheckLiquidTemplate(string templatePath, Dictionary<string

// Render and strip out unhelpful whitespace (actual post-processing gets rid of this
// at the end of the day anyway)
var actualContent = template.Render(RenderParameters.FromContext(context, CultureInfo.InvariantCulture)).Trim().Replace("\n", " ").Replace("\t", "");
var actualContent = template.Render(RenderParameters.FromContext(context, CultureInfo.InvariantCulture)).Trim().Replace("\n", " ").Replace("\t", string.Empty);
actualContent = Filters.CleanStringFromTabs(actualContent);

// Many are harmless, but can be helpful for debugging
foreach (var err in template.Errors)
Expand All @@ -64,5 +67,4 @@ protected void ConvertCheckLiquidTemplate(string templatePath, Dictionary<string
Assert.Equal(expectedContent, actualContent);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Collections.Generic;
using System.IO;
using DotLiquid;
using Xunit;

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests
{
public class AddressTests : BaseECRLiquidTests
{
private static readonly string ECRPath = Path.Join(
TestConstants.ECRTemplateDirectory, "DataType", "_Address.liquid"
);

[Fact]
public void GivenNoAttributeReturnsEmpty()
{
ConvertCheckLiquidTemplate(ECRPath, new Dictionary<string, object>(), string.Empty);
}

[Fact]
public void GivenSingleStreetAddresReturnsLines()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { streetAddressLine = new { _ = "132 Main St" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ""132 Main St"", ], ""city"": """", ""state"": """", ""country"": """", ""postalCode"": """", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenArrayStreetAddresReturnsLines()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { streetAddressLine = new List<object> {new {_ = "132 Main St"}, new { _ ="Unit 2"} }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ""132 Main St"", ""Unit 2"", ], ""city"": """", ""state"": """", ""country"": """", ""postalCode"": """", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenCityReturnsCity()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { city = new { _ = "Town" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": ""Town"", ""state"": """", ""country"": """", ""postalCode"": """", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenStateReturnsState()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { state = new { _ = "State" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": """", ""state"": ""State"", ""country"": """", ""postalCode"": """", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenPostalCodeReturnsPostalCode()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { postalCode = new { _ = "PostalCode" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": """", ""state"": """", ""country"": """", ""postalCode"": ""PostalCode"", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenCountyReturnsDistrict()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { county = new { _ = "County" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": """", ""state"": """", ""country"": """", ""postalCode"": """", ""district"": ""County"", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenCountrReturnsCountry()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { country = new { _ = "Country" }})}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": """", ""state"": """", ""country"": ""Country"", ""postalCode"": """", ""district"": """", ""period"": { ""start"":"""", ""end"":"""", },");
}

[Fact]
public void GivenPeriodReturnsNothing()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { useablePeriod = new { low = new { value = "20240313"}} })}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"");
}

[Fact]
public void GivenPeriodAndCountryReturnsBoth()
{
var attributes = new Dictionary<string, object>{
{"Address", Hash.FromAnonymousObject(new { country = new {_ = "Country" }, useablePeriod = new { low = new { value = "20240313"}} })}
};
ConvertCheckLiquidTemplate(
ECRPath,
attributes,
@"""use"": """", ""line"": [ ], ""city"": """", ""state"": """", ""country"": ""Country"", ""postalCode"": """", ""district"": """", ""period"": { ""start"":""2024-03-13"", ""end"":"""", },");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests
{
public class ContactPointTests : BaseConvertLiquidTemplate
public class ContactPointTests : BaseECRLiquidTests
{
private static readonly string ECRPath = Path.Join(
TestConstants.ECRTemplateDirectory, "DataType", "_ContactPoint.liquid"
Expand Down

0 comments on commit 7546817

Please sign in to comment.