From dd98ccc9ff32d6f7f88123be55b57139c5cb0a9d Mon Sep 17 00:00:00 2001 From: Anton Vasetenkov Date: Sat, 31 Aug 2024 08:31:10 +1200 Subject: [PATCH] Set ValueSetRef.preserve to true for included value sets (#1409) * #1407: Set preserve to true for included value sets * Clean up tests --- .../cql/cql2elm/LibraryBuilder.java | 3 ++ .../cql/cql2elm/SemanticTests.java | 24 +++++++++++-- .../cql/cql2elm/LibraryTests/Issue1407.cql | 11 ++++++ .../LibraryTests/Issue1407ValueSets.cql | 5 +++ .../execution/IncludedValueSetRefTest.java | 34 +++---------------- 5 files changed, 45 insertions(+), 32 deletions(-) create mode 100644 Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407.cql create mode 100644 Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407ValueSets.cql diff --git a/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/LibraryBuilder.java b/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/LibraryBuilder.java index 953ac12af..4ece6bfc9 100644 --- a/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/LibraryBuilder.java +++ b/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/LibraryBuilder.java @@ -2941,6 +2941,9 @@ public Expression resolveAccessor(Expression left, String memberIdentifier) { ValueSetRef result = of.createValueSetRef().withLibraryName(libraryName).withName(memberIdentifier); result.setResultType(element.getResultType()); + if (isCompatibleWith("1.5")) { + result.setPreserve(true); + } return result; } diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/SemanticTests.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/SemanticTests.java index 9de3d0840..5d9ddf396 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/SemanticTests.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/SemanticTests.java @@ -2,9 +2,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.util.HashMap; @@ -772,6 +770,26 @@ public void testIdentifierDoesNotResolveCaseMismatchExistIdentifier() throws IOE assertThat(warnings, hasSize(0)); } + @Test + void issue1407() throws IOException { + assertNull(issue1407GetIsPreserve("1.4")); + assertTrue(issue1407GetIsPreserve("1.5")); + } + + private Boolean issue1407GetIsPreserve(String compatibilityLevel) throws IOException { + CqlTranslator translator = runSemanticTest( + "LibraryTests/Issue1407.cql", 0, new CqlCompilerOptions().withCompatibilityLevel(compatibilityLevel)); + var library = translator.toELM(); + var testExpression = library.getStatements().getDef().stream() + .filter(def -> def.getName().equals("TestStatement")) + .findFirst() + .orElseThrow() + .getExpression(); + + assertThat(testExpression, instanceOf(ValueSetRef.class)); + return ((ValueSetRef) testExpression).isPreserve(); + } + private CqlTranslator runSemanticTest(String testFileName) throws IOException { return runSemanticTest(testFileName, 0); } diff --git a/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407.cql b/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407.cql new file mode 100644 index 000000000..a6a63395a --- /dev/null +++ b/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407.cql @@ -0,0 +1,11 @@ +library Issue1407 + +using FHIR version '4.0.1' + +include FHIRHelpers version '4.0.1' called FHIRHelpers + +include Issue1407ValueSets + +context Patient + +define TestStatement: Issue1407ValueSets.TestValueSet \ No newline at end of file diff --git a/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407ValueSets.cql b/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407ValueSets.cql new file mode 100644 index 000000000..98f5506af --- /dev/null +++ b/Src/java/cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/LibraryTests/Issue1407ValueSets.cql @@ -0,0 +1,5 @@ +library Issue1407ValueSets + +using FHIR version '4.0.1' + +valueset TestValueSet: 'garb' \ No newline at end of file diff --git a/Src/java/engine/src/test/java/org/opencds/cqf/cql/engine/execution/IncludedValueSetRefTest.java b/Src/java/engine/src/test/java/org/opencds/cqf/cql/engine/execution/IncludedValueSetRefTest.java index e9c7a66a5..a99199d1e 100644 --- a/Src/java/engine/src/test/java/org/opencds/cqf/cql/engine/execution/IncludedValueSetRefTest.java +++ b/Src/java/engine/src/test/java/org/opencds/cqf/cql/engine/execution/IncludedValueSetRefTest.java @@ -2,17 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.opencds.cqf.cql.engine.execution.CqlConceptTest.assertEqual; -import java.util.Collections; -import java.util.List; import org.cqframework.cql.cql2elm.LibraryManager; import org.cqframework.cql.cql2elm.ModelManager; import org.junit.jupiter.api.Test; -import org.opencds.cqf.cql.engine.runtime.Code; -import org.opencds.cqf.cql.engine.terminology.CodeSystemInfo; -import org.opencds.cqf.cql.engine.terminology.TerminologyProvider; -import org.opencds.cqf.cql.engine.terminology.ValueSetInfo; +import org.opencds.cqf.cql.engine.runtime.ValueSet; class IncludedValueSetRefTest { @@ -21,33 +15,15 @@ void all_included_valueset() { LibraryManager libraryManager = new LibraryManager(new ModelManager()); libraryManager.getLibrarySourceLoader().registerProvider(new TestLibrarySourceProvider()); - Code expected = new Code().withCode("M").withSystem("http://test.com/system"); - TerminologyProvider terminologyProvider = new TerminologyProvider() { - public boolean in(Code code, ValueSetInfo valueSet) { - return true; - } - - public Iterable expand(ValueSetInfo valueSet) { - return Collections.singletonList(expected); - } - - public Code lookup(Code code, CodeSystemInfo codeSystem) { - return null; - } - }; - - Environment environment = new Environment(libraryManager, null, terminologyProvider); + Environment environment = new Environment(libraryManager); CqlEngine engine = new CqlEngine(environment); var results = engine.evaluate(CqlTestBase.toElmIdentifier("IncludedValueSetRefTest")); - @SuppressWarnings("unchecked") - List actual = - (List) results.forExpression("IncludedValueSet").value(); - assertNotNull(actual); - assertEquals(1, actual.size()); + var actual = (ValueSet) results.forExpression("IncludedValueSet").value(); - assertEqual(expected, actual.get(0)); + assertNotNull(actual); + assertEquals("http://test/common", actual.getId()); } }