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);
}
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);
}
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());
}
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);
}
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());
}
Aggregations