Skip to content

Commit

Permalink
Write rendering & output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Dec 20, 2024
1 parent 21f738b commit 94e03dd
Show file tree
Hide file tree
Showing 9 changed files with 1,626 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests_metricflow/integration/query_output/test_query_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations

import pytest
from _pytest.fixtures import FixtureRequest
from metricflow_semantics.specs.query_param_implementations import MetricParameter
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration

from metricflow.engine.metricflow_engine import MetricFlowQueryRequest
from metricflow.protocols.sql_client import SqlClient
from tests_metricflow.integration.conftest import IntegrationTestHelpers
from tests_metricflow.snapshot_utils import assert_str_snapshot_equal


@pytest.mark.sql_engine_snapshot
def test_metric_alias( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metrics=(MetricParameter(name="bookings", alias="bookings_alias"),),
group_by_names=["metric_time__day"],
order_by_names=["metric_time__day"],
where_constraints=("{{ Metric('bookings', ['listing']) }} > 2",),
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_str_snapshot_equal(
request=request,
mf_test_configuration=mf_test_configuration,
snapshot_id="query_output",
snapshot_str=query_result.result_df.text_format(),
sql_engine=sql_client.sql_engine_type,
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_alias( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metrics=(MetricParameter(name="booking_fees", alias="bookings_alias"),),
group_by_names=["metric_time__day"],
order_by_names=["metric_time__day"],
where_constraints=("{{ Metric('bookings', ['listing']) }} > 2",),
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_str_snapshot_equal(
request=request,
mf_test_configuration=mf_test_configuration,
snapshot_id="query_output",
snapshot_str=query_result.result_df.text_format(),
sql_engine=sql_client.sql_engine_type,
)
62 changes: 62 additions & 0 deletions tests_metricflow/query_rendering/test_query_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
from metricflow_semantics.specs.column_assoc import ColumnAssociationResolver
from metricflow_semantics.specs.dimension_spec import DimensionSpec
from metricflow_semantics.specs.metric_spec import MetricSpec
from metricflow_semantics.specs.query_param_implementations import (
MetricParameter,
OrderByParameter,
)
from metricflow_semantics.specs.query_spec import MetricFlowQuerySpec
from metricflow_semantics.specs.time_dimension_spec import TimeDimensionSpec
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration
Expand Down Expand Up @@ -603,3 +607,61 @@ def test_non_additive_dimension_with_non_default_grain(
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_metric_alias(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
sql_client: SqlClient,
query_parser: MetricFlowQueryParser,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
) -> None:
"""Tests a plan with an aliased metric."""
metric = MetricParameter(name="bookings", alias="bookings_alias")

query_spec = query_parser.parse_and_validate_query(
metrics=(metric,),
group_by_names=("metric_time__month",),
order_by=(OrderByParameter(metric),),
where_constraint_strs=("{{ Metric('bookings', ['listing']) }} > 2",),
).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,
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_alias(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
sql_client: SqlClient,
query_parser: MetricFlowQueryParser,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
) -> None:
"""Tests a plan with an aliased metric."""
metric = MetricParameter(name="booking_fees", alias="bookings_alias")

query_spec = query_parser.parse_and_validate_query(
metrics=(metric,),
group_by_names=("metric_time__day",),
order_by=(OrderByParameter(metric),),
where_constraint_strs=("{{ Metric('booking_fees', ['listing']) }} > 2",),
).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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_name: test_add_time_expr
test_filename: test_engine_specific_rendering.py
docstring:
Tests rendering of the SqlAddTimeExpr in a query.
sql_engine: DuckDB
---
-- Test Add Time Expression
SELECT
'2020-01-01' + INTERVAL (1 * 3) month AS add_time
FROM foo.bar a
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_name: test_derived_metric_alias
test_filename: test_query_output.py
---
bookings_alias metric_time__day
---------------- -------------------
47.56 2019-12-01T00:00:00
284.93 2019-12-18T00:00:00
360.5 2019-12-19T00:00:00
0 2019-12-20T00:00:00
136.16 2020-01-01T00:00:00
132.78 2020-01-02T00:00:00
0 2020-01-03T00:00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_name: test_metric_alias
test_filename: test_query_output.py
---
bookings_alias metric_time__day
---------------- -------------------
1 2019-12-01T00:00:00
10 2019-12-18T00:00:00
18 2019-12-19T00:00:00
2 2019-12-20T00:00:00
5 2020-01-01T00:00:00
9 2020-01-02T00:00:00
1 2020-01-03T00:00:00
Loading

0 comments on commit 94e03dd

Please sign in to comment.