From 890a1b03aafc1f1e81714e43b7c1ba5297c7c96a Mon Sep 17 00:00:00 2001 From: Will Deng Date: Tue, 12 Nov 2024 18:45:42 -0500 Subject: [PATCH] add more tests --- tests/functional/metrics/fixtures.py | 36 ++++++++++++++++++++++++ tests/functional/metrics/test_metrics.py | 31 ++++++++++++++++---- tests/functional/time_spines/fixtures.py | 18 ++++++++++++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/tests/functional/metrics/fixtures.py b/tests/functional/metrics/fixtures.py index 5614fab8c63..bfd75fd4a2d 100644 --- a/tests/functional/metrics/fixtures.py +++ b/tests/functional/metrics/fixtures.py @@ -462,6 +462,8 @@ dimensions: - name: purchased_at type: TIME + type_params: + time_granularity: day entities: - name: purchase type: primary @@ -736,6 +738,8 @@ dimensions: - name: purchased_at type: TIME + type_params: + time_granularity: day entities: - name: purchase type: primary @@ -796,6 +800,20 @@ type: cumulative type_params: measure: num_visits + - name: visits_martian_day + label: Visits since start of martian_day + type: cumulative + type_params: + measure: num_visits + cumulative_type_params: + grain_to_date: martian_day + - name: visits_martian_day_window + label: Visits since start of martian_day window + type: cumulative + type_params: + measure: num_visits + cumulative_type_params: + window: 1 martian_day """ conversion_metric_yml = """ @@ -809,6 +827,24 @@ base_measure: num_visits conversion_measure: num_orders entity: purchase + - name: converted_orders_over_visits_with_window + label: Number of orders converted from visits with window + type: conversion + type_params: + conversion_type_params: + base_measure: num_visits + conversion_measure: num_orders + entity: purchase + window: 4 day + - name: converted_orders_over_visits_with_custom_window + label: Number of orders converted from visits with custom window + type: conversion + type_params: + conversion_type_params: + base_measure: num_visits + conversion_measure: num_orders + entity: purchase + window: 4 martian_day """ filtered_metrics_yml = """ diff --git a/tests/functional/metrics/test_metrics.py b/tests/functional/metrics/test_metrics.py index 7c0842a7414..9905cdfa385 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -34,6 +34,7 @@ semantic_model_people_yml, semantic_model_purchasing_yml, ) +from tests.functional.time_spines.fixtures import time_spine_yml class TestSimpleMetrics: @@ -361,6 +362,7 @@ def models(self): "metricflow_time_spine.sql": metricflow_time_spine_sql, "semantic_models.yml": conversion_semantic_model_purchasing_yml, "conversion_metric.yml": conversion_metric_yml, + "time_spine.yml": time_spine_yml, } @pytest.fixture(scope="class") @@ -382,10 +384,16 @@ def test_conversion_metric( # make sure the metric is in the manifest manifest = get_manifest(project.project_root) metric_ids = list(manifest.metrics.keys()) - expected_metric_ids = [ - "metric.test.converted_orders_over_visits", - ] - assert metric_ids == expected_metric_ids + expected_metric_ids = { + "metric.test.converted_orders_over_visits": None, + "metric.test.converted_orders_over_visits_with_window": MetricTimeWindow( + count=4, granularity=TimeGranularity.DAY.value + ), + "metric.test.converted_orders_over_visits_with_custom_window": MetricTimeWindow( + count=4, granularity="martian_day" + ), + } + assert set(metric_ids) == set(expected_metric_ids.keys()) assert manifest.metrics[ "metric.test.converted_orders_over_visits" ].type_params.conversion_type_params @@ -409,6 +417,14 @@ def test_conversion_metric( ].type_params.conversion_type_params.entity == "purchase" ) + for ( + metric_id, + expected_window, + ) in expected_metric_ids.items(): + assert ( + manifest.metrics[metric_id].type_params.conversion_type_params.window + == expected_window + ), f"Found unexpected conversion window for {metric_id}" class TestCumulativeMetric: @@ -418,7 +434,8 @@ def models(self): "purchasing.sql": purchasing_model_sql, "metricflow_time_spine.sql": metricflow_time_spine_sql, "semantic_models.yml": conversion_semantic_model_purchasing_yml, - "conversion_metric.yml": cumulative_metric_yml, + "cumulative_metric.yml": cumulative_metric_yml, + "time_spine.yml": time_spine_yml, } @pytest.fixture(scope="class") @@ -459,6 +476,10 @@ def test_cumulative_metric(self, project): "metric.test.cumulative_visits": CumulativeTypeParams( period_agg=PeriodAggregation.FIRST ), + "metric.test.visits_martian_day": CumulativeTypeParams(grain_to_date="martian_day"), + "metric.test.visits_martian_day_window": CumulativeTypeParams( + window=MetricTimeWindow(count=1, granularity="martian_day"), + ), } assert metric_ids == set(expected_metric_ids_to_cumulative_type_params.keys()) for ( diff --git a/tests/functional/time_spines/fixtures.py b/tests/functional/time_spines/fixtures.py index 0f67488ff11..e6012cb7154 100644 --- a/tests/functional/time_spines/fixtures.py +++ b/tests/functional/time_spines/fixtures.py @@ -103,3 +103,21 @@ - name: date_day granularity: day """ + +time_spine_yml = """ +version: 2 + +models: + - name: metricflow_time_spine + time_spine: + standard_granularity_column: date_day + custom_granularities: + - name: retail_month + - name: martian_day + column_name: martian_day + columns: + - name: date_day + granularity: day + - name: retail_month + - name: martian_day +"""