Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 25, 2024
1 parent c9507c1 commit 4a5bddf
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
42 changes: 42 additions & 0 deletions tests/functional/metrics/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions tests/functional/metrics/test_metrics.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_semantic_layer_nodes_satisfy_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dbt.artifacts.resources import (
ConstantPropertyInput,
ConversionTypeParams,
CumulativeTypeParams,
Defaults,
Dimension,
DimensionTypeParams,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)


Expand Down

0 comments on commit 4a5bddf

Please sign in to comment.