def patient_query_factory( runner: Runner, data_source: str, code_system: tp.Optional[str] = None, **kwargs: tp.Dict[tp.Any, tp.Any], ) -> base.PatientQuery: """Returns the right instance of `PatientQuery` based on `data_source`. Args: runner: The runner to use for making data queries data_source: The definition of the source, e.g., directory containing Parquet files or a BigQuery dataset. kwargs: A dictionary of runner specific parameters. Returns: The created instance. Raises: ValueError: When the input `data_source` is malformed or not implemented. """ if runner == Runner.SPARK: return query_lib_spark.SparkPatientQuery(data_source, code_system) if runner == Runner.BIG_QUERY: if "project_name" not in kwargs: raise ValueError( "'project_name' should be provided in kwargs while using " "a BigQuery data source") return query_lib_big_query.BigQueryPatientQuery( project_name=kwargs['project_name'], bq_dataset=data_source, code_system=code_system, ) raise ValueError("Query engine {} is not supported yet.".format(runner))
def test_single_code_with_values(self): patient_query = query_lib_spark.SparkPatientQuery("", "") patient_query.include_obs_values_in_time_range('TEST_CODE', ['VAL1', 'VAL2'], '2021-06-01', '2021-07-10') sql_constraint = patient_query._all_constraints_sql() self.assertEqual( sql_constraint, ('((dateTime >= "2021-06-01" AND dateTime <= "2021-07-10" AND ' 'coding.code="TEST_CODE" AND ' 'valueCoding.code IN ("VAL1","VAL2") AND ' 'valueCoding.system IS NULL))' ' AND TRUE AND TRUE AND TRUE'))
def test_two_codes_with_values_and_range(self): patient_query = query_lib_spark.SparkPatientQuery("", "") patient_query.include_obs_values_in_time_range('TEST_CODE1', ['VAL1', 'VAL2'], '2021-06-01', '2021-07-10') patient_query.include_obs_in_value_and_time_range( 'TEST_CODE2', 0.1, None, '2021-07-09', '2021-07-10') sql_constraint = patient_query._all_constraints_sql() self.assertEqual( sql_constraint, ('((dateTime >= "2021-06-01" AND dateTime <= "2021-07-10" AND ' 'coding.code="TEST_CODE1" AND ' 'valueCoding.code IN ("VAL1","VAL2") AND ' 'valueCoding.system IS NULL) ' 'OR (dateTime >= "2021-07-09" AND dateTime <= "2021-07-10" AND ' 'coding.code="TEST_CODE2" AND value.quantity.value >= 0.1 ))' ' AND TRUE AND TRUE AND TRUE'))
def test_single_code_with_values_and_encounter(self): patient_query = query_lib_spark.SparkPatientQuery("", "") patient_query.include_obs_values_in_time_range('TEST_CODE', ['VAL1', 'VAL2'], '2021-06-01', '2021-07-10') patient_query.encounter_constraints(locationId=['L1', 'L2'], typeCode=['TC1', 'TC2'], typeSystem='TS') sql_constraint = patient_query._all_constraints_sql().strip() self.assertEqual( sql_constraint, ('((dateTime >= "2021-06-01" AND dateTime <= "2021-07-10" AND ' 'coding.code="TEST_CODE" AND ' 'valueCoding.code IN ("VAL1","VAL2") AND ' 'valueCoding.system IS NULL)) ' 'AND locationId IN ("L1","L2") AND ' 'arrays_overlap(encTypeCode, array("TC1","TC2")) AND ' 'array_contains(encTypeSystem, "TS")'))