Search in sources :

Example 1 with ResourcePathBuilder

use of au.csiro.pathling.test.builders.ResourcePathBuilder in project pathling by aehrc.

the class CountFunctionTest method countsByGrouping.

@Test
void countsByGrouping() {
    final Dataset<Row> inputDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withColumn("active", DataTypes.BooleanType).withRow("patient-1", "female", true).withRow("patient-2", "female", false).withRow("patient-2", "male", true).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(inputDataset);
    final ResourcePath inputPath = new ResourcePathBuilder(spark).database(database).resourceType(ResourceType.PATIENT).expression("Patient").build();
    final Column groupingColumn = inputPath.getElementColumn("gender");
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(groupingColumn)).inputExpression("Patient").build();
    final NamedFunctionInput countInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction count = NamedFunction.getInstance("count");
    final FhirPath result = count.invoke(countInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withColumn(DataTypes.StringType).withColumn(DataTypes.LongType).withRow("female", 2L).withRow("male", 1L).build();
    assertThat(result).hasExpression("count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectGroupingResult(Collections.singletonList(groupingColumn)).hasRows(expectedDataset);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) IntegerPath(au.csiro.pathling.fhirpath.element.IntegerPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Column(org.apache.spark.sql.Column) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ResourcePathBuilder

use of au.csiro.pathling.test.builders.ResourcePathBuilder in project pathling by aehrc.

the class FirstFunctionTest method firstOfUngroupedSubResources.

@Test
void firstOfUngroupedSubResources() {
    final String subresourceId = randomAlias();
    final String statusColumn = randomAlias();
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(subresourceId, DataTypes.StringType).withColumn(statusColumn, DataTypes.StringType).withRow("patient-1", makeEid(2), "Encounter/5", "in-progress").withRow("patient-1", makeEid(1), "Encounter/1", "in-progress").withRow("patient-1", makeEid(0), "Encounter/2", "finished").withRow("patient-2", makeEid(0), "Encounter/3", "in-progress").withRow("patient-3", null, null, null).build();
    final ResourcePath inputPath = new ResourcePathBuilder(spark).expression("reverseResolve(Encounter.subject)").dataset(inputDataset).idEidAndValueColumns().valueColumn(inputDataset.col(subresourceId)).resourceType(ResourceType.ENCOUNTER).buildCustom();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).build();
    final NamedFunctionInput firstInput = new NamedFunctionInput(parserContext, inputPath, Collections.emptyList());
    final NamedFunction firstFunction = NamedFunction.getInstance("first");
    final FhirPath result = firstFunction.invoke(firstInput);
    final Dataset<Row> expectedDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withIdColumn().withRow("patient-1", null, "Encounter/2").withRow("patient-2", null, "Encounter/3").withRow("patient-3", null, null).build();
    assertThat(result).isResourcePath().hasExpression("reverseResolve(Encounter.subject).first()").isSingular().hasResourceType(ResourceType.ENCOUNTER).selectOrderedResultWithEid().hasRows(expectedDataset);
}
Also used : ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ResourcePathBuilder

use of au.csiro.pathling.test.builders.ResourcePathBuilder in project pathling by aehrc.

the class IifFunctionTest method throwsErrorWithElementAndResourceResults.

@Test
void throwsErrorWithElementAndResourceResults() {
    final NonLiteralPath condition = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).expression("valueBoolean").singular(true).build().toThisPath();
    final ElementPath ifTrue = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.STRING).expression("someString").build();
    final ResourcePath otherwise = new ResourcePathBuilder(spark).expression("someResource").resourceType(ResourceType.CONDITION).build();
    final NamedFunctionInput iifInput = new NamedFunctionInput(parserContext, condition, Arrays.asList(condition, ifTrue, otherwise));
    final NamedFunction notFunction = NamedFunction.getInstance("iif");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> notFunction.invoke(iifInput));
    assertEquals("Paths cannot be merged into a collection together: someString, someResource", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ResourcePathBuilder

use of au.csiro.pathling.test.builders.ResourcePathBuilder in project pathling by aehrc.

the class IifFunctionTest method returnsCorrectResultsForLiteralAndNonLiteral.

@Test
void returnsCorrectResultsForLiteralAndNonLiteral() {
    final Dataset<Row> inputContextDataset = new ResourceDatasetBuilder(spark).withIdColumn().withRow("observation-1").withRow("observation-2").withRow("observation-3").withRow("observation-4").withRow("observation-5").build();
    when(database.read(ResourceType.OBSERVATION)).thenReturn(inputContextDataset);
    final ResourcePath inputContext = new ResourcePathBuilder(spark).expression("Observation").resourceType(ResourceType.OBSERVATION).database(database).singular(true).build();
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.BooleanType).withRow("observation-1", makeEid(0), false).withRow("observation-2", makeEid(0), true).withRow("observation-3", makeEid(0), null).withRow("observation-4", makeEid(0), true).withRow("observation-4", makeEid(1), false).withRow("observation-5", makeEid(0), null).withRow("observation-5", makeEid(1), null).build();
    final ElementPath inputPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).dataset(inputDataset).idAndEidAndValueColumns().expression("valueBoolean").singular(false).build();
    parserContext = new ParserContextBuilder(spark, fhirContext).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).build();
    final NonLiteralPath condition = inputPath.toThisPath();
    final Dataset<Row> ifTrueDataset = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 1).withRow("observation-2", makeEid(0), 2).withRow("observation-3", makeEid(0), 3).withRow("observation-4", makeEid(0), 4).withRow("observation-5", makeEid(0), 5).build();
    final ElementPath ifTrue = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.INTEGER).dataset(ifTrueDataset).idAndEidAndValueColumns().expression("someInteger").singular(true).build();
    final IntegerLiteralPath otherwise = IntegerLiteralPath.fromString("99", inputContext);
    final NamedFunctionInput iifInput1 = new NamedFunctionInput(parserContext, inputPath, Arrays.asList(condition, ifTrue, otherwise));
    final FhirPath result1 = NamedFunction.getInstance("iif").invoke(iifInput1);
    final Dataset<Row> expectedDataset1 = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 99).withRow("observation-2", makeEid(0), 2).withRow("observation-3", makeEid(0), 99).withRow("observation-4", makeEid(0), 4).withRow("observation-4", makeEid(1), 99).withRow("observation-5", makeEid(0), 99).withRow("observation-5", makeEid(1), 99).build();
    assertThat(result1).hasExpression("valueBoolean.iif($this, someInteger, 99)").isNotSingular().isElementPath(IntegerPath.class).selectOrderedResultWithEid().hasRows(expectedDataset1);
    final NamedFunctionInput iifInput2 = new NamedFunctionInput(parserContext, inputPath, Arrays.asList(condition, otherwise, ifTrue));
    final FhirPath result2 = NamedFunction.getInstance("iif").invoke(iifInput2);
    final Dataset<Row> expectedDataset2 = new DatasetBuilder(spark).withIdColumn(ID_ALIAS).withEidColumn().withColumn(DataTypes.IntegerType).withRow("observation-1", makeEid(0), 1).withRow("observation-2", makeEid(0), 99).withRow("observation-3", makeEid(0), 3).withRow("observation-4", makeEid(0), 99).withRow("observation-4", makeEid(1), 4).withRow("observation-5", makeEid(0), 5).withRow("observation-5", makeEid(1), 5).build();
    assertThat(result2).hasExpression("valueBoolean.iif($this, 99, someInteger)").isNotSingular().isElementPath(IntegerPath.class).selectOrderedResultWithEid().hasRows(expectedDataset2);
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) IntegerLiteralPath(au.csiro.pathling.fhirpath.literal.IntegerLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ResourcePathBuilder

use of au.csiro.pathling.test.builders.ResourcePathBuilder in project pathling by aehrc.

the class IifFunctionTest method throwsErrorWithResourceAndLiteralResults.

@Test
void throwsErrorWithResourceAndLiteralResults() {
    final NonLiteralPath condition = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.BOOLEAN).expression("valueBoolean").singular(true).build().toThisPath();
    final ResourcePath ifTrue = new ResourcePathBuilder(spark).expression("someResource").resourceType(ResourceType.PATIENT).build();
    final StringLiteralPath otherwise = StringLiteralPath.fromString("foo", condition);
    final NamedFunctionInput iifInput = new NamedFunctionInput(parserContext, condition, Arrays.asList(condition, ifTrue, otherwise));
    final NamedFunction notFunction = NamedFunction.getInstance("iif");
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> notFunction.invoke(iifInput));
    assertEquals("Paths cannot be merged into a collection together: someResource, 'foo'", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) UntypedResourcePath(au.csiro.pathling.fhirpath.UntypedResourcePath) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) UntypedResourcePathBuilder(au.csiro.pathling.test.builders.UntypedResourcePathBuilder) ResourcePathBuilder(au.csiro.pathling.test.builders.ResourcePathBuilder) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)24 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)23 Test (org.junit.jupiter.api.Test)22 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)22 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)17 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)14 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)13 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)12 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)11 Row (org.apache.spark.sql.Row)11 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)9 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)9 FhirPath (au.csiro.pathling.fhirpath.FhirPath)8 UntypedResourcePath (au.csiro.pathling.fhirpath.UntypedResourcePath)8 UntypedResourcePathBuilder (au.csiro.pathling.test.builders.UntypedResourcePathBuilder)8 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)6 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)2 Column (org.apache.spark.sql.Column)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)1