Skip to content

Commit

Permalink
WIP - more tests (no metric queries, etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Sep 18, 2024
1 parent ebb64c0 commit 46f982b
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions tests_metricflow/query_rendering/test_custom_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,193 @@ def test_metric_custom_granularity_joined_to_non_default_grain( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_no_metric_custom_granularity_non_metric_time( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
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_no_metric_custom_granularity_joined_to_non_default_grain( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
time_dimension_specs=(
MTD_SPEC_DAY,
metric_time_with_custom_grain,
normal_time_dim_with_custom_grain2,
TimeDimensionSpec(
element_name="bio_added_ts",
time_granularity=ExpandedTimeGranularity.from_time_granularity(TimeGranularity.MONTH),
entity_links=(EntityReference("user"),),
),
),
)

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 with issues below! See if these are still issues


# - Bug: subq_3 alias is used but never defined. Appears to be happening in optimization.
# - Optimization: we could just select directly from the time spine, but istead we join one time spine to another.
# - Optimization: we start by selecting from the time spine with second grain, when it would be more efficient to select
# from the time spine with day. Not related to this feature, but should fix.
@pytest.mark.sql_engine_snapshot
def test_no_metric_custom_granularity_metric_time( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
time_dimension_specs=(metric_time_with_custom_grain,),
)

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,
)


# TODO: add more tests
# - with multiple custom granularities
# - with custom granularity that has a different name than its column name
# - with every type of dataflow plan that uses join to time spine node
# - with cumulative metrics
# - query parsing tests - mapping from custom granularity name to time dimension spec not handled yet
# - check query tests

# Filter tests, when supported:
# @pytest.mark.sql_engine_snapshot
# def test_no_metric_query_with_custom_granularity_filters_included_in_group_by( # noqa: D103
# request: FixtureRequest,
# mf_test_configuration: MetricFlowTestConfiguration,
# dataflow_plan_builder: DataflowPlanBuilder,
# dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
# sql_client: SqlClient,
# ) -> None:
# query_spec = MetricFlowQuerySpec(
# # A no-metric query, where filter on metric_time with custom grain, included in group by
# # Also a time constraint on the same dimension
# time_dimension_specs=(metric_time_with_custom_grain,),
# where_constraint=PydanticWhereFilter(
# where_sql_template="{{ TimeDimension('metric_time') }} > '2020-01-01'",
# ),
# time_constraint_start=datetime.datetime(2020, 1, 3),
# time_constraint_end=datetime.datetime(2020, 1, 5),
# )

# 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_no_metric_query_with_custom_granularity_filters_not_included_in_group_by( # noqa: D103
# request: FixtureRequest,
# mf_test_configuration: MetricFlowTestConfiguration,
# dataflow_plan_builder: DataflowPlanBuilder,
# dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
# sql_client: SqlClient,
# ) -> None:
# query_spec = MetricFlowQuerySpec(
# # TODO: no-metric query, where filter on non-metric_time with custom grain, not included in group by, also another time dim with standard grain
# # Also a time constraint not included in group by
# time_dimension_specs=(MTD_SPEC_DAY,),
# )

# 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_metric_query_with_custom_granularity_filters_included_in_group_by( # noqa: D103
# request: FixtureRequest,
# mf_test_configuration: MetricFlowTestConfiguration,
# dataflow_plan_builder: DataflowPlanBuilder,
# dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
# sql_client: SqlClient,
# ) -> None:
# query_spec = MetricFlowQuerySpec(
# # TODO: metric query, where filter on metric_time with custom grain, included in group by
# # Also a time constraint on the same dimension
# time_dimension_specs=(MTD_SPEC_DAY,),
# )

# 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_metric_query_with_custom_granularity_filters_not_included_in_group_by( # noqa: D103
# request: FixtureRequest,
# mf_test_configuration: MetricFlowTestConfiguration,
# dataflow_plan_builder: DataflowPlanBuilder,
# dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
# sql_client: SqlClient,
# ) -> None:
# query_spec = MetricFlowQuerySpec(
# # TODO: metric query, where filter on non-metric_time with custom grain, not included in group by, also another time dim with standard grain
# # Also a time constraint not included in group by
# time_dimension_specs=(MTD_SPEC_DAY,),
# )

# 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,
# )

0 comments on commit 46f982b

Please sign in to comment.