Search in sources :

Example 1 with ElementPathBuilder

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

the class CountFunctionTest method doesNotCountNullElements.

@Test
void doesNotCountNullElements() {
    final Dataset<Row> dataset = new DatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withRow("patient-1", "female").withRow("patient-2", null).withRow("patient-3", "male").build();
    final ElementPath inputPath = new ElementPathBuilder(spark).expression("gender").fhirType(FHIRDefinedType.CODE).dataset(dataset).idAndValueColumns().build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).groupingColumns(Collections.emptyList()).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).withIdColumn().withColumn(DataTypes.LongType).withRow("patient-1", 2L).build();
    assertThat(result).hasExpression("gender.count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectOrderedResult().hasRows(expectedDataset);
}
Also used : ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) IntegerPath(au.csiro.pathling.fhirpath.element.IntegerPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) 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) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ElementPathBuilder

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

the class CountFunctionTest method inputMustNotContainArguments.

@Test
void inputMustNotContainArguments() {
    final ElementPath inputPath = new ElementPathBuilder(spark).build();
    final ElementPath argumentPath = new ElementPathBuilder(spark).build();
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    final NamedFunctionInput countInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentPath));
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> NamedFunction.getInstance("count").invoke(countInput));
    assertEquals("Arguments can not be passed to count function", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ElementPathBuilder

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

the class ExtensionFunctionTest method throwsErrorIfArgumentIsNotString.

@Test
public void throwsErrorIfArgumentIsNotString() {
    final ElementPath input = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.CODEABLECONCEPT).build();
    final IntegerLiteralPath argument = IntegerLiteralPath.fromString("4", input);
    final ParserContext context = new ParserContextBuilder(spark, fhirContext).build();
    final NamedFunctionInput extensionInput = new NamedFunctionInput(context, input, Collections.singletonList(argument));
    final InvalidUserInputError error = assertThrows(InvalidUserInputError.class, () -> new ExtensionFunction().invoke(extensionInput));
    assertEquals("extension function must have argument of type String literal: .extension(4)", error.getMessage());
}
Also used : InvalidUserInputError(au.csiro.pathling.errors.InvalidUserInputError) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) 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 4 with ElementPathBuilder

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

the class ExtensionFunctionTest method testExtensionOnElements.

@Test
public void testExtensionOnElements() {
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).build();
    // Construct element dataset from the resource dataset so that the resource path
    // can be used as the current resource for this element path
    // Note: this resource path is not singular as this will be a base for elements.
    final Dataset<Row> resourceLikeDataset = new ResourceDatasetBuilder(spark).withIdColumn().withEidColumn().withStructColumn("name", DataTypes.StringType).withStructColumn("_fid", DataTypes.IntegerType).withStructValueColumn().withExtensionColumn().withRow("observation-1", makeEid(0), RowFactory.create("name1", 0), oneEntryMap(0, MANY_MY_EXTENSIONS)).withRow("observation-2", makeEid(0), RowFactory.create("name2", 1), oneEntryMap(1, ONE_MY_EXTENSION)).withRow("observation-3", makeEid(0), RowFactory.create("name3", 2), oneEntryMap(2, NO_MY_EXTENSIONS)).withRow("observation-4", makeEid(0), RowFactory.create("name4", 3), oneEntryMap(3, ONE_MY_EXTENSION)).withRow("observation-4", makeEid(1), RowFactory.create("name5", 4), oneEntryMap(3, ONE_MY_EXTENSION)).withRow("observation-5", makeEid(0), null, null).withRow("observation-5", makeEid(1), null, null).build();
    when(database.read(ResourceType.OBSERVATION)).thenReturn(resourceLikeDataset);
    final ResourcePath baseResourcePath = ResourcePath.build(fhirContext, database, ResourceType.OBSERVATION, "Observation", false);
    final Dataset<Row> elementDataset = toElementDataset(resourceLikeDataset, baseResourcePath);
    final ElementDefinition codeDefinition = checkPresent(FhirHelpers.getChildOfResource(fhirContext, "Observation", "code"));
    final ElementPath inputPath = new ElementPathBuilder(spark).fhirType(FHIRDefinedType.CODEABLECONCEPT).definition(codeDefinition).dataset(elementDataset).idAndEidAndValueColumns().expression("code").singular(false).currentResource(baseResourcePath).buildDefined();
    final StringLiteralPath argumentExpression = StringLiteralPath.fromString("'" + "uuid:myExtension" + "'", inputPath);
    final NamedFunctionInput extensionInput = new NamedFunctionInput(parserContext, inputPath, Collections.singletonList(argumentExpression));
    final NamedFunction extension = NamedFunction.getInstance("extension");
    final FhirPath result = extension.invoke(extensionInput);
    final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(DatasetBuilder.SIMPLE_EXTENSION_TYPE).withRow("observation-1", makeEid(0, 0), null).withRow("observation-1", makeEid(0, 1), null).withRow("observation-1", makeEid(0, 2), MANY_EXT_ROW_1).withRow("observation-1", makeEid(0, 3), MANY_EXT_ROW_2).withRow("observation-2", makeEid(0, 0), null).withRow("observation-2", makeEid(0, 1), ONE_EXT_ROW_1).withRow("observation-3", makeEid(0, 0), null).withRow("observation-3", makeEid(0, 1), null).withRow("observation-4", makeEid(0, 0), null).withRow("observation-4", makeEid(0, 1), ONE_EXT_ROW_1).withRow("observation-4", makeEid(1, 0), null).withRow("observation-5", makeEid(0, 0), null).withRow("observation-5", makeEid(1, 0), null).buildWithStructValue();
    assertThat(result).hasExpression("code.extension('uuid:myExtension')").isNotSingular().isElementPath(ElementPath.class).hasFhirType(FHIRDefinedType.EXTENSION).selectOrderedResultWithEid().hasRows(expectedResult);
}
Also used : StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) Row(org.apache.spark.sql.Row) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ElementPathBuilder

use of au.csiro.pathling.test.builders.ElementPathBuilder 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)

Aggregations

ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)103 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)93 Test (org.junit.jupiter.api.Test)80 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)80 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)63 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)53 Row (org.apache.spark.sql.Row)53 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)51 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)47 FhirPath (au.csiro.pathling.fhirpath.FhirPath)41 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)27 NamedFunctionInput (au.csiro.pathling.fhirpath.function.NamedFunctionInput)18 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)16 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)16 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)14 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)14 ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 UntypedResourcePath (au.csiro.pathling.fhirpath.UntypedResourcePath)9