From 4a5bddf8ed976a7fa04f19f03f0bbe3643cf7343 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Tue, 25 Jun 2024 10:48:13 -0700 Subject: [PATCH] Tests --- tests/functional/metrics/fixtures.py | 42 +++++++++++++ tests/functional/metrics/test_metrics.py | 61 +++++++++++++++++++ ..._semantic_layer_nodes_satisfy_protocols.py | 13 +++- 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/tests/functional/metrics/fixtures.py b/tests/functional/metrics/fixtures.py index c9d2d0ab190..3a08b47e159 100644 --- a/tests/functional/metrics/fixtures.py +++ b/tests/functional/metrics/fixtures.py @@ -744,6 +744,48 @@ """ +cumulative_metric_yml = """ +version: 2 +metrics: + - name: weekly_visits + label: Rolling sum of visits over the last 7 days + type: cumulative + type_params: + cumulative_type_params: + window: 7 days + period_agg: average + - name: cumulative_orders + label: Rolling total of orders (all time) + type: cumulative + type_params: + cumulative_type_params: + period_agg: last + - name: orders_ytd + label: Total orders since the start of the year + type: cumulative + type_params: + cumulative_type_params: + grain_to_date: year + period_agg: first + - name: quarterly_orders + label: Rolling count of orders over the last quarter + type: cumulative + type_params: + window: 1 quarter + - name: visits_mtd + label: Count of visits since start of month + type: cumulative + type_params: + grain_to_date: month + - name: monthly_visits + label: Rolling sum of visits over the last month + type: cumulative + type_params: + window: 1 month + cumulative_type_params: + period_agg: last +""" + conversion_metric_yml = """ version: 2 metrics: diff --git a/tests/functional/metrics/test_metrics.py b/tests/functional/metrics/test_metrics.py index b640c90c199..4c5a0e26c16 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -1,5 +1,8 @@ import pytest +from dbt_semantic_interfaces.type_enums.period_agg import PeriodAggregation +from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity +from dbt.artifacts.resources.v1.metric import CumulativeTypeParams, MetricTimeWindow from dbt.cli.main import dbtRunner from dbt.contracts.graph.manifest import Manifest from dbt.exceptions import ParsingError @@ -8,6 +11,7 @@ basic_metrics_yml, conversion_metric_yml, conversion_semantic_model_purchasing_yml, + cumulative_metric_yml, derived_metric_yml, downstream_model_sql, duplicate_measure_metric_yml, @@ -402,6 +406,63 @@ def test_conversion_metric( ) +class TestCumulativeMetric: + @pytest.fixture(scope="class") + def models(self): + return { + "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, + } + + @pytest.fixture(scope="class") + def seeds(self): + return {"mock_purchase_data.csv": mock_purchase_data_csv} + + def test_cumulative_metric(self, project): + # initial parse + runner = dbtRunner() + result = runner.invoke(["parse"]) + assert result.success + assert isinstance(result.result, Manifest) + + manifest = get_manifest(project.project_root) + metric_ids = set(manifest.metrics.keys()) + expected_metric_ids_to_cumulative_type_params = { + "metric.test.weekly_visits": CumulativeTypeParams( + window=MetricTimeWindow(count=7, granularity=TimeGranularity.WEEK), + period_agg=PeriodAggregation.AVERAGE, + ), + "metric.test.cumulative_orders": CumulativeTypeParams( + period_agg=PeriodAggregation.LAST + ), + "metric.test.orders_ytd": CumulativeTypeParams( + grain_to_date=TimeGranularity.YEAR, period_agg=PeriodAggregation.FIRST + ), + "metric.test.quarterly_orders": CumulativeTypeParams( + window=MetricTimeWindow(count=1, granularity=TimeGranularity.QUARTER), + period_agg=PeriodAggregation.FIRST, + ), + "metric.test.visits_mtd": CumulativeTypeParams( + grain_to_date=TimeGranularity.MONTH, period_agg=PeriodAggregation.FIRST + ), + "metric.test.monthly_visits": CumulativeTypeParams( + window=MetricTimeWindow(count=1, granularity=TimeGranularity.MONTH), + period_agg=PeriodAggregation.FIRST, + ), + } + assert metric_ids == set(expected_metric_ids_to_cumulative_type_params.keys()) + for ( + metric_id, + expected_cumulative_type_params, + ) in expected_metric_ids_to_cumulative_type_params.items(): + assert ( + manifest.metrics[metric_id].type_params.cumulative_type_params + == expected_cumulative_type_params + ) + + class TestFilterParsing: @pytest.fixture(scope="class") def models(self): diff --git a/tests/unit/test_semantic_layer_nodes_satisfy_protocols.py b/tests/unit/test_semantic_layer_nodes_satisfy_protocols.py index 379ae46e7a9..b78c254f78d 100644 --- a/tests/unit/test_semantic_layer_nodes_satisfy_protocols.py +++ b/tests/unit/test_semantic_layer_nodes_satisfy_protocols.py @@ -23,6 +23,7 @@ from dbt.artifacts.resources import ( ConstantPropertyInput, ConversionTypeParams, + CumulativeTypeParams, Defaults, Dimension, DimensionTypeParams, @@ -245,9 +246,18 @@ def conversion_type_params( ) +@pytest.fixture(scope="session") +def cumulative_type_params() -> CumulativeTypeParams: + return CumulativeTypeParams() + + @pytest.fixture(scope="session") def complex_metric_type_params( - metric_time_window, simple_metric_input, simple_metric_input_measure + metric_time_window, + simple_metric_input, + simple_metric_input_measure, + conversion_type_params, + cumulative_type_params, ) -> MetricTypeParams: return MetricTypeParams( measure=simple_metric_input_measure, @@ -258,6 +268,7 @@ def complex_metric_type_params( grain_to_date=TimeGranularity.DAY, metrics=[simple_metric_input], conversion_type_params=conversion_type_params, + cumulative_type_params=cumulative_type_params, )