From b640ed2325065a09131a814c969fd1f5aa89ce8b Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Mon, 29 Apr 2024 12:32:50 -0700 Subject: [PATCH] Test that all available single-hop metrics don't error --- .../builder/test_dataflow_plan_builder.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests_metricflow/dataflow/builder/test_dataflow_plan_builder.py b/tests_metricflow/dataflow/builder/test_dataflow_plan_builder.py index 61541e08f1..8821bc9a1c 100644 --- a/tests_metricflow/dataflow/builder/test_dataflow_plan_builder.py +++ b/tests_metricflow/dataflow/builder/test_dataflow_plan_builder.py @@ -2,20 +2,24 @@ import datetime import logging +import string import pytest from _pytest.fixtures import FixtureRequest from dbt_semantic_interfaces.implementations.filters.where_filter import PydanticWhereFilter from dbt_semantic_interfaces.naming.keywords import METRIC_TIME_ELEMENT_NAME +from dbt_semantic_interfaces.references import MeasureReference from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity from metricflow_semantics.errors.error_classes import UnableToSatisfyQueryError from metricflow_semantics.filters.time_constraint import TimeRangeConstraint +from metricflow_semantics.model.linkable_element_property import LinkableElementProperty from metricflow_semantics.query.query_parser import MetricFlowQueryParser from metricflow_semantics.specs.column_assoc import ColumnAssociationResolver from metricflow_semantics.specs.query_spec import MetricFlowQuerySpec from metricflow_semantics.specs.spec_classes import ( DimensionSpec, EntityReference, + GroupByMetricSpec, MetricSpec, OrderBySpec, TimeDimensionSpec, @@ -1324,3 +1328,27 @@ def test_metric_in_metric_where_filter( mf_test_configuration=mf_test_configuration, dag_graph=dataflow_plan, ) + + +def test_all_available_single_join_metric_filters( + dataflow_plan_builder: DataflowPlanBuilder, query_parser: MetricFlowQueryParser +) -> None: + """Checks that all allowed metric filters do not error when used in dataflow plan (single-hop for both inner and out query).""" + for linkable_metric_tuple in dataflow_plan_builder._metric_lookup.linkable_elements_for_measure( + MeasureReference("listings"), without_any_of={LinkableElementProperty.MULTI_HOP} + ).path_key_to_linkable_metrics.values(): + for linkable_metric in linkable_metric_tuple: + group_by_metric_spec = linkable_metric.path_key.spec + assert isinstance(group_by_metric_spec, GroupByMetricSpec) + entity_spec = group_by_metric_spec.metric_subquery_entity_spec + if entity_spec.entity_links: # multi-hop for inner query + continue + query_spec = query_parser.parse_and_validate_query( + metric_names=("listings",), + where_constraint=PydanticWhereFilter( + where_sql_template=string.Template("{{ Metric('$metric_name', ['$entity_name']) }} > 2").substitute( + metric_name=linkable_metric.element_name, entity_name=entity_spec.element_name + ), + ), + ) + dataflow_plan_builder.build_plan(query_spec)