Skip to content

Commit

Permalink
Rendering & check query tests for custom grain with offset metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Oct 18, 2024
1 parent 3b6fa94 commit e3bcc0c
Show file tree
Hide file tree
Showing 7 changed files with 846 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests_metricflow/integration/test_cases/itest_granularity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,21 @@ integration_test:
AND {{ render_date_trunc("b.ds_partitioned", TimeGranularity.DAY) }} = {{ render_date_trunc("u.ds_partitioned", TimeGranularity.DAY) }}
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON {{ render_date_trunc("u.ds", TimeGranularity.DAY) }} = ts.ds
GROUP BY ts.martian_day
---
integration_test:
name: test_offset_metric_with_custom_granularity_filter_not_in_group_by
description: Test querying a simple metric with a custom grain on a multi-hop dimension
model: SIMPLE_MODEL
metrics: ["bookings_5_day_lag"]
group_bys: ["metric_time__day"]
where_filter: |
{{ render_time_constraint(render_time_dimension_template('metric_time', 'martian_day'), start_time="2020-01-01") }}
check_query: |
SELECT
ts.ds AS metric_time__day
, SUM(1) AS bookings_5_day_lag
FROM {{ source_schema }}.mf_time_spine ts
INNER JOIN {{ source_schema }}.fct_bookings b ON {{ render_date_sub("ts", "ds", 5, TimeGranularity.DAY) }} = {{ render_date_trunc("b.ds", TimeGranularity.DAY) }}
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts1 ON ts.ds = ts1.ds
WHERE {{ render_time_constraint("ts1.martian_day", "2020-01-01") }}
GROUP BY ts.ds
2 changes: 1 addition & 1 deletion tests_metricflow/integration/test_configured_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def filter_not_supported_features(
@pytest.mark.parametrize(
"name",
# CONFIGURED_INTEGRATION_TESTS_REPOSITORY.all_test_case_names,
["itest_granularity.yaml/test_simple_metric_with_multi_hop_custom_granularity"],
["itest_granularity.yaml/test_offset_metric_with_custom_granularity_filter_not_in_group_by"],
ids=lambda name: f"name={name}",
)
def test_case(
Expand Down
55 changes: 55 additions & 0 deletions tests_metricflow/query_rendering/test_custom_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,58 @@ def test_no_metrics_with_custom_granularity_in_filter_and_group_by(
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_offset_metric_with_custom_granularity( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("bookings_5_day_lag"),),
time_dimension_specs=(normal_time_dim_with_custom_grain1,),
)

render_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_offset_metric_with_custom_granularity_filter_not_in_group_by( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
query_parser: MetricFlowQueryParser,
) -> None:
query_spec = query_parser.parse_and_validate_query(
metric_names=("bookings_5_day_lag",),
group_by_names=("metric_time__day",),
where_constraints=[
PydanticWhereFilter(where_sql_template=("{{ TimeDimension('metric_time', 'martian_day') }} = '2020-01-01'"))
],
).query_spec

render_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


# Tests TODO:
# - join_to_timespine metric
# - join_to_timespine metric with filter on custom grain that's not in the group by (dun dun dun)
Loading

0 comments on commit e3bcc0c

Please sign in to comment.