Skip to content

Commit

Permalink
Rendering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 14, 2024
1 parent 9891bbf commit 21eda37
Show file tree
Hide file tree
Showing 15 changed files with 1,945 additions and 12 deletions.
238 changes: 226 additions & 12 deletions tests_metricflow/query_rendering/test_cumulative_metric_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from metricflow_semantics.specs.query_spec import MetricFlowQuerySpec
from metricflow_semantics.specs.spec_classes import EntityReference, MetricSpec, TimeDimensionSpec
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration
from metricflow_semantics.test_helpers.metric_time_dimension import MTD_SPEC_DAY, MTD_SPEC_MONTH
from metricflow_semantics.test_helpers.metric_time_dimension import (
MTD_SPEC_DAY,
MTD_SPEC_MONTH,
MTD_SPEC_WEEK,
MTD_SPEC_QUARTER,
MTD_SPEC_YEAR,
)

from metricflow.dataflow.builder.dataflow_plan_builder import DataflowPlanBuilder
from metricflow.plan_conversion.dataflow_to_sql import DataflowToSqlQueryPlanConverter
Expand All @@ -38,7 +44,6 @@ def test_cumulative_metric(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
Expand Down Expand Up @@ -76,7 +81,6 @@ def test_cumulative_metric_with_time_constraint(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="metric_time",
Expand Down Expand Up @@ -150,7 +154,6 @@ def test_cumulative_metric_no_ds(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(),
)
)
Expand All @@ -177,7 +180,6 @@ def test_cumulative_metric_no_window(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_all_time"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
Expand Down Expand Up @@ -210,7 +212,6 @@ def test_cumulative_metric_no_window_with_time_constraint(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_all_time"),),
dimension_specs=(),
time_dimension_specs=(MTD_SPEC_DAY,),
time_range_constraint=TimeRangeConstraint(
start_time=as_datetime("2020-01-01"), end_time=as_datetime("2020-01-01")
Expand Down Expand Up @@ -240,7 +241,6 @@ def test_cumulative_metric_grain_to_date(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_mtd"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
Expand Down Expand Up @@ -273,7 +273,6 @@ def test_cumulative_metric_month(
dataflow_plan = extended_date_dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_3_months_bookings"),),
dimension_specs=(),
time_dimension_specs=(MTD_SPEC_MONTH,),
time_range_constraint=TimeRangeConstraint(
start_time=as_datetime("2020-03-05"), end_time=as_datetime("2021-01-04")
Expand Down Expand Up @@ -303,7 +302,6 @@ def test_cumulative_metric_with_agg_time_dimension(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(element_name="ds", entity_links=(EntityReference("revenue_instance"),)),
),
Expand Down Expand Up @@ -332,7 +330,6 @@ def test_cumulative_metric_with_multiple_agg_time_dimensions(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
Expand Down Expand Up @@ -370,7 +367,6 @@ def test_cumulative_metric_with_multiple_metric_time_dimensions(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(MTD_SPEC_DAY, MTD_SPEC_MONTH),
)
)
Expand All @@ -397,7 +393,6 @@ def test_cumulative_metric_with_agg_time_and_metric_time(
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(
MTD_SPEC_DAY,
TimeDimensionSpec(
Expand All @@ -416,3 +411,222 @@ def test_cumulative_metric_with_agg_time_and_metric_time(
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_with_non_default_grain(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative all-time metric queried with non-default grain."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_all_time"),),
time_dimension_specs=(MTD_SPEC_WEEK,),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_window_metric_with_non_default_grain(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative window metric queried with non-default grain."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
time_dimension_specs=(MTD_SPEC_YEAR,),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_grain_to_date_metric_with_non_default_grain(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative grain to date metric queried with non-default grain."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_mtd"),),
time_dimension_specs=(MTD_SPEC_MONTH,),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_window_metric_with_multiple_grains(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative window metric queried with multiple grains.
Uses both metric_time and agg_time_dimension. Includes default grain.
"""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="every_two_days_bookers_fill_nulls_with_0"),),
time_dimension_specs=(
MTD_SPEC_DAY,
TimeDimensionSpec(
element_name="ds",
entity_links=(EntityReference("booking"),),
time_granularity=TimeGranularity.MONTH,
),
),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_grain_to_date_metric_with_multiple_grains(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative grain to date metric queried with multiple grains.
Uses agg time dimension instead of metric_time. Excludes default grain.
"""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_mtd"),),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
entity_links=(EntityReference("revenue_instance"),),
time_granularity=TimeGranularity.QUARTER,
),
TimeDimensionSpec(
element_name="ds",
entity_links=(EntityReference("revenue_instance"),),
time_granularity=TimeGranularity.YEAR,
),
),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_all_time_metric_with_multiple_grains(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative all-time metric queried with non-default grain.
Uses only metric_time. Excludes default grain.
"""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_mtd"),),
time_dimension_specs=(MTD_SPEC_WEEK, MTD_SPEC_QUARTER),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


@pytest.mark.sql_engine_snapshot
def test_derived_cumulative_metric_with_non_default_grain(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
mf_engine_test_fixture_mapping: Mapping[SemanticManifestSetup, MetricFlowEngineTestFixture],
sql_client: SqlClient,
) -> None:
"""Test querying a derived metric with a cumulative input metric using non-default granularity."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue_sub_10"),),
time_dimension_specs=(MTD_SPEC_WEEK,),
)
)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_node,
)


# Query 2 cumulative metrics with different agg_time_dimensions - what will happen??

# TODO: write the following tests when unblocked
# - Render each of the allowed period_aggs (both set in YAML & default)
# - Query cumulative metric with non-day default_grain (using default grain and non-default grain)
# - Query 2 metrics with different default_grains using metric_time (no grain specified)
# - If default grain is WEEK, query with a higher grain (check that we still get correct values)
# - Query cumulative metric with sub-daily grain
Loading

0 comments on commit 21eda37

Please sign in to comment.