Search in sources :

Example 1 with ParserContextBuilder

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

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

the class CountFunctionTest method countsByResourceIdentity.

@Test
void countsByResourceIdentity() {
    final Dataset<Row> patientDataset = new ResourceDatasetBuilder(spark).withIdColumn().withColumn("gender", DataTypes.StringType).withColumn("active", DataTypes.BooleanType).withRow("patient-1", "female", true).withRow("patient-2", "female", false).withRow("patient-3", "male", true).build();
    when(database.read(ResourceType.PATIENT)).thenReturn(patientDataset);
    final ResourcePath inputPath = ResourcePath.build(fhirContext, database, ResourceType.PATIENT, "Patient", false);
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputPath.getIdColumn()).groupingColumns(Collections.singletonList(inputPath.getIdColumn())).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).withIdColumn().withColumn(DataTypes.LongType).withRow("patient-1", 1L).withRow("patient-2", 1L).withRow("patient-3", 1L).build();
    assertThat(result).hasExpression("count()").isSingular().isElementPath(IntegerPath.class).hasFhirType(FHIRDefinedType.UNSIGNEDINT).selectOrderedResult().hasRows(expectedDataset);
}
Also used : ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceDatasetBuilder(au.csiro.pathling.test.builders.ResourceDatasetBuilder) 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) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ParserContextBuilder

use of au.csiro.pathling.test.builders.ParserContextBuilder 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 4 with ParserContextBuilder

use of au.csiro.pathling.test.builders.ParserContextBuilder 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 5 with ParserContextBuilder

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

Aggregations

ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)80 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)63 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)61 Test (org.junit.jupiter.api.Test)60 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)58 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)57 FhirPath (au.csiro.pathling.fhirpath.FhirPath)40 Row (org.apache.spark.sql.Row)38 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)35 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)31 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)28 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)20 NamedFunctionInput (au.csiro.pathling.fhirpath.function.NamedFunctionInput)17 ResourcePathBuilder (au.csiro.pathling.test.builders.ResourcePathBuilder)17 ResourceDatasetBuilder (au.csiro.pathling.test.builders.ResourceDatasetBuilder)16 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)6 UntypedResourcePath (au.csiro.pathling.fhirpath.UntypedResourcePath)6