Skip to content

Commit

Permalink
Merge pull request #837 from dbt-labs/move-cumulative-metric-renderin…
Browse files Browse the repository at this point in the history
…g-tests

Move cumulative metric query rendering tests to new module
  • Loading branch information
tlento authored Nov 3, 2023
2 parents 6f6de91 + 967431c commit dad961f
Show file tree
Hide file tree
Showing 80 changed files with 219 additions and 2,350 deletions.
198 changes: 0 additions & 198 deletions metricflow/test/plan_conversion/test_dataflow_to_sql_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,204 +1011,6 @@ def test_compute_metrics_node_ratio_from_multiple_semantic_models(
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where there is a cumulative metric to compute."""
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=(),
time_granularity=TimeGranularity.MONTH,
),
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_with_time_constraint(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where there is a cumulative metric to compute."""
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=(),
time_granularity=TimeGranularity.MONTH,
),
),
time_range_constraint=TimeRangeConstraint(
start_time=as_datetime("2020-01-01"), end_time=as_datetime("2020-01-01")
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_no_ds(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where there is a cumulative metric to compute."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_2_months_revenue"),),
dimension_specs=(),
time_dimension_specs=(),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_no_window(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where there is a windowless cumulative metric to compute."""
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",
entity_links=(),
time_granularity=TimeGranularity.MONTH,
),
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_no_window_with_time_constraint(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where there is a windowless cumulative metric to compute."""
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",
entity_links=(),
time_granularity=TimeGranularity.MONTH,
),
),
time_range_constraint=TimeRangeConstraint(
start_time=as_datetime("2020-01-01"), end_time=as_datetime("2020-01-01")
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_grain_to_date(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests converting a dataflow plan to a SQL query plan where grain_to_date cumulative metric to compute."""
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="revenue_mtd"),),
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="ds",
entity_links=(),
time_granularity=TimeGranularity.MONTH,
),
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric( # noqa: D
request: FixtureRequest,
Expand Down
Loading

0 comments on commit dad961f

Please sign in to comment.