Skip to content

Commit

Permalink
Merge pull request #2825 from FirelyTeam/bugfix/2792-excluding-parent…
Browse files Browse the repository at this point in the history
…-property-shouldn't-exclude-children

ValueSet expander: excluding a code, shouldn't remove its children
  • Loading branch information
ewoutkramer authored Jul 24, 2024
2 parents c150395 + ac0c22b commit 5d5d360
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,12 @@ public static ValueSet.ContainsComponent Add(this List<ValueSet.ContainsComponen

public static void Remove(this List<ValueSet.ContainsComponent> dest, string system, string code)
{
var children = dest.Where(c => c.System == system && c.Code == code).SelectMany(c => c.Contains).ToList();
dest.RemoveAll(c => c.System == system && c.Code == code);

//add children back to the list, they do not necessarily need to be removed when the parent is removed.
dest.AddRange(children);

// Look for this code in children too
foreach (var component in dest)
{
Expand All @@ -334,9 +338,15 @@ public static void Remove(this List<ValueSet.ContainsComponent> dest, string sys

public static void Remove(this List<ValueSet.ContainsComponent> dest, List<ValueSet.ContainsComponent> source)
{
//TODO: Pretty unclear what to do with child concepts in the source - they all need to be removed from dest?
foreach (var sourceConcept in source)
{
dest.Remove(sourceConcept.System, sourceConcept.Code);

//check if there are children that need to be removed too.
if (sourceConcept.Contains.Any())
dest.Remove(sourceConcept.Contains);
}

}

internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.ConceptDefinitionComponent source, CodeSystem system, ValueSetExpanderSettings settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,123 @@ public async Tasks.Task TestExpandingVsWithUnknownSystem()
var job = async () => await expander.ExpandAsync(vs);
await job.Should().ThrowAsync<ValueSetUnknownException>().WithMessage("The ValueSet expander cannot find codesystem 'http://www.unknown.org/', so the expansion cannot be completed.");
}

[Fact]
public async Tasks.Task TestExcludingOnlyParent()
{
var resolver = new InMemoryResourceResolver();
var vs = new ValueSet
{
Compose = new()
{
Include = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/",
Filter = new List<ValueSet.FilterComponent>
{
new()
{
Property = "concept",
Op = FilterOperator.IsA,
Value = "parent"
}
}
}
},
Exclude = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/",
Concept = new List<ValueSet.ConceptReferenceComponent>
{
new()
{
Code = "parent"
}
}
}
}
}
};

var cs = new CodeSystem
{
Url = "http://www.unknown.org/",
Content = CodeSystemContentMode.Complete,
Concept = new List<CodeSystem.ConceptDefinitionComponent>
{
new()
{
Code = "parent",
Concept = new List<CodeSystem.ConceptDefinitionComponent>
{
new()
{
Code = "child1"
},
new()
{
Code = "child2"
}
}
}
}
};

resolver.Add(cs, vs);

var expander = new ValueSetExpander(new ValueSetExpanderSettings { ValueSetSource = resolver });
await expander.ExpandAsync(vs);
vs.Expansion.Contains.Select(c => c.Code).Should().BeEquivalentTo(["child1", "child2"]);


vs.Compose.Exclude = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/",
Concept = new List<ValueSet.ConceptReferenceComponent>
{
new()
{
Code = "parent"
},
new()
{
Code = "child1"
}
}
}
};

resolver.Reload(cs, vs);
await expander.ExpandAsync(vs);
vs.Expansion.Contains.Select(c => c.Code).Should().BeEquivalentTo(["child2"]);

vs.Compose.Exclude = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/",
Filter = new List<ValueSet.FilterComponent>
{
new()
{
Property = "concept",
Op = FilterOperator.IsA,
Value = "parent"
}
}
}
};

resolver.Reload(cs, vs);
await expander.ExpandAsync(vs);
vs.Expansion.Contains.Select(c => c.Code).Should().BeEmpty();

}
}
}

0 comments on commit 5d5d360

Please sign in to comment.