def execute_test(tel_expression: str, taxon_type: TaxonTypeEnum, expected_result: Union[TelType, ExceptionResult]): if isinstance(expected_result, TelType): # Expecting success result = TaxonTelDialect().render(tel_expression, SNOWFLAKE_HUSKY_CONTEXT, taxon_map=TAXON_MAP, taxon_type=taxon_type) tel_type = result.return_type assert tel_type.data_type == expected_result.data_type, "TelType.data_type doesn't match" assert tel_type.is_constant == expected_result.is_constant, "TelType.is_constant doesn't match" elif isinstance(expected_result, ExceptionResult): # Expecting exception with pytest.raises(TelExpressionException) as exception_info: TaxonTelDialect().render(tel_expression, SNOWFLAKE_HUSKY_CONTEXT, taxon_map=TAXON_MAP, taxon_type=taxon_type) # Extract the first part of the error message data_type_error_message = exception_info.value.args[0].split('.')[0] assert data_type_error_message == expected_result.message else: raise AssertionError( f'Unknown type of the expected result: {type(expected_result)}')
def execute_test(test_input, expected): tel_dialect = TaxonTelDialect() context = TelRootContext(SNOWFLAKE_HUSKY_CONTEXT, tel_dialect, None, TAXON_MAP, node_id_maker()) result = tel_dialect.visit(test_input, context) assert expected == result.literal_value(context)
def test_detection(expression, expected): tel_dialect = TaxonTelDialect() context = TelRootContext(SNOWFLAKE_HUSKY_CONTEXT, tel_dialect, None, {}, next_node_id=node_id_maker()) node = tel_dialect.visit(expression, context, skip_root_node=True) res = can_become_comparison_metric(node) assert res == expected
def execute_test(test_input, expected): taxon_type = TaxonTypeEnum.metric if len(test_input) == 3: taxon_type = test_input[2] if isinstance(expected, ExprException): with pytest.raises(TelExpressionException) as exc_info: TaxonTelDialect().render( test_input[0], SNOWFLAKE_HUSKY_CONTEXT, data_sources=test_input[1], taxon_map=TAXON_MAP, taxon_type=taxon_type, ) assert exc_info.value.args[0] == expected.msg else: expected.phase = TelPhase.metric_post res = TaxonTelDialect().render( test_input[0], SNOWFLAKE_HUSKY_CONTEXT, data_sources=test_input[1], taxon_map=TAXON_MAP, taxon_type=taxon_type, ) assert_result_formulas(res, expected)
def execute_test(request, test_input): taxon_type = TaxonTypeEnum.metric taxon_slug = '' if len(test_input) == 3: taxon_type = test_input[2] if len(test_input) == 4: taxon_slug = test_input[3] asserter = ExprResultAsserter(request, test_description_generator=get_test_case_id) try: res = TaxonTelDialect().render( test_input[0], SNOWFLAKE_HUSKY_CONTEXT, data_sources=test_input[1], taxon_map=TAXON_MAP, taxon_type=taxon_type, taxon_slug=taxon_slug, ) asserter.eval(test_input, res) except TelExpressionException as e: asserter.eval(test_input, e)
def _parse_taxon_expr( ctx: HuskyQueryContext, taxon: Taxon, tel_prefix: str, data_sources: Iterable[str], all_taxons: TaxonMap, subrequest_only=False, ): taxon_type = EnumHelper.from_value(TaxonTypeEnum, taxon.taxon_type) try: return TaxonTelDialect().render( expr=cast(str, taxon.calculation), ctx=ctx, taxon_map=all_taxons, taxon_slug=tel_prefix, comparison=taxon.is_comparison_taxon, data_sources=data_sources, taxon_type=taxon_type, aggregation=taxon.aggregation, subrequest_only=subrequest_only, ) except TelExpressionException as error: raise HuskyInvalidTelException(error, taxon.slug)
def test_deep_advanced_render(request, test_input): res = TaxonTelDialect().render(test_input, SNOWFLAKE_HUSKY_CONTEXT, taxon_map=TAXON_MAP) ExprResultAsserter(request, test_description_generator=str).eval(test_input, res)
def test_errors(request, test_input): with pytest.raises(TelExpressionException) as exc_info: TaxonTelDialect().render(test_input, SNOWFLAKE_HUSKY_CONTEXT, taxon_map=TAXON_MAP) ExprResultAsserter(request, test_description_generator=str).eval(test_input, exc_info.value)
def test_transpile_to_comparison_snowflake_sql(request, test_input): res = TaxonTelDialect().render(test_input, SNOWFLAKE_HUSKY_CONTEXT, comparison=True, taxon_map=TAXON_MAP) ExprResultAsserter(request, test_description_generator=str).eval(test_input, res)