diff --git a/tests_metricflow/integration/query_output/test_offset_metrics.py b/tests_metricflow/integration/query_output/test_offset_metrics.py index d51b3f76b..72e9075e5 100644 --- a/tests_metricflow/integration/query_output/test_offset_metrics.py +++ b/tests_metricflow/integration/query_output/test_offset_metrics.py @@ -2,6 +2,9 @@ import pytest from _pytest.fixtures import FixtureRequest +from dbt_semantic_interfaces.type_enums.date_part import DatePart +from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity +from metricflow_semantics.specs.query_param_implementations import OrderByParameter, TimeDimensionParameter from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration from metricflow.engine.metricflow_engine import MetricFlowQueryRequest @@ -58,3 +61,88 @@ def test_offset_to_grain_with_multiple_granularities( # noqa: D103 snapshot_str=query_result.result_df.text_format(), sql_engine=sql_client.sql_engine_type, ) + + +@pytest.mark.sql_engine_snapshot +def test_custom_offset_window_with_base_grain( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Gives a side by side comparison of bookings and bookings_offset_one_martian_day.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=["bookings", "bookings_offset_one_martian_day"], + group_by_names=["metric_time__day", "metric_time__martian_day"], + order_by_names=["metric_time__day", "metric_time__martian_day"], + ) + ) + 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_custom_offset_window_with_grains_and_date_part( # 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( + metric_names=["bookings_offset_one_martian_day"], + group_by=( + TimeDimensionParameter(name="booking__ds", grain=TimeGranularity.MONTH.name), + TimeDimensionParameter(name="metric_time", date_part=DatePart.YEAR), + TimeDimensionParameter(name="metric_time", grain="martian_day"), + ), + order_by=( + OrderByParameter(TimeDimensionParameter(name="booking__ds", grain=TimeGranularity.MONTH.name)), + OrderByParameter(TimeDimensionParameter(name="metric_time", date_part=DatePart.YEAR)), + OrderByParameter(TimeDimensionParameter(name="metric_time", grain="martian_day")), + ), + ) + ) + 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, + ) + + +# TODO: add test data to bookings source that spans multiple martian days +@pytest.mark.sql_engine_snapshot +def test_custom_offset_window_period_over_period( # 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( + metric_names=["bookings_martian_day_over_martian_day"], + group_by_names=["metric_time__day"], + order_by_names=["metric_time__day"], + ) + ) + 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, + ) diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt new file mode 100644 index 000000000..29924960b --- /dev/null +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt @@ -0,0 +1,12 @@ +test_name: test_custom_offset_window_period_over_period +test_filename: test_offset_metrics.py +--- +metric_time__day bookings_martian_day_over_martian_day +------------------- --------------------------------------- +2019-12-01T00:00:00 0 +2019-12-18T00:00:00 9 +2019-12-19T00:00:00 17 +2019-12-20T00:00:00 1 +2020-01-01T00:00:00 4 +2020-01-02T00:00:00 8 +2020-01-03T00:00:00 0 diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt new file mode 100644 index 000000000..6be3b6d43 --- /dev/null +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt @@ -0,0 +1,12 @@ +test_name: test_custom_offset_window_with_base_grain +test_filename: test_offset_metrics.py +--- +metric_time__day bookings_offset_one_martian_day +------------------- --------------------------------- +2019-12-01T00:00:00 1 +2019-12-18T00:00:00 10 +2019-12-19T00:00:00 18 +2019-12-20T00:00:00 2 +2020-01-01T00:00:00 5 +2020-01-02T00:00:00 9 +2020-01-03T00:00:00 1 diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt new file mode 100644 index 000000000..9f3776943 --- /dev/null +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt @@ -0,0 +1,7 @@ +test_name: test_custom_offset_window_with_grains_and_date_part +test_filename: test_offset_metrics.py +--- +metric_time__martian_day booking__ds__month metric_time__extract_year bookings_offset_one_martian_day +-------------------------- -------------------- --------------------------- --------------------------------- +2020-01-08T00:00:00 2019-12-01T00:00:00 2019 31 +2020-01-08T00:00:00 2020-01-01T00:00:00 2020 15