diff --git a/.changes/unreleased/Under the Hood-20220916-154712.yaml b/.changes/unreleased/Under the Hood-20220916-154712.yaml new file mode 100644 index 00000000000..1d425067d3e --- /dev/null +++ b/.changes/unreleased/Under the Hood-20220916-154712.yaml @@ -0,0 +1,7 @@ +kind: Under the Hood +body: Adding validation for metric expression attribute +time: 2022-09-16T15:47:12.799002-05:00 +custom: + Author: callum-mcdata + Issue: "5871" + PR: "5873" diff --git a/core/dbt/contracts/graph/parsed.py b/core/dbt/contracts/graph/parsed.py index 3eee514ed00..772d2abc08c 100644 --- a/core/dbt/contracts/graph/parsed.py +++ b/core/dbt/contracts/graph/parsed.py @@ -819,8 +819,8 @@ class ParsedMetric(UnparsedBaseNode, HasUniqueID, HasFqn): description: str label: str calculation_method: str - expression: str timestamp: str + expression: str filters: List[MetricFilter] time_grains: List[str] dimensions: List[str] diff --git a/core/dbt/contracts/graph/unparsed.py b/core/dbt/contracts/graph/unparsed.py index 167612b3517..a53ba0a45ad 100644 --- a/core/dbt/contracts/graph/unparsed.py +++ b/core/dbt/contracts/graph/unparsed.py @@ -475,8 +475,8 @@ class UnparsedMetric(dbtClassMixin, Replaceable): label: str calculation_method: str timestamp: str + expression: str description: str = "" - expression: Union[str, int] = "" time_grains: List[str] = field(default_factory=list) dimensions: List[str] = field(default_factory=list) window: Optional[MetricTime] = None diff --git a/tests/functional/metrics/test_metrics.py b/tests/functional/metrics/test_metrics.py index 933f205c3f6..c6f398350f3 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -196,6 +196,42 @@ def test_simple_metric( run_dbt(["run"]) +invalid_metrics__missing_expression_yml = """ +version: 2 +metrics: + - name: number_of_people + label: "Number of people" + model: "ref(people)" + description: Total count of people + calculation_method: count + timestamp: created_at + time_grains: [day, week, month] + dimensions: + - favorite_color + - loves_dbt + meta: + my_meta: 'testing' +""" + + +class TestInvalidMetricMissingExpression: + @pytest.fixture(scope="class") + def models(self): + return { + "people_metrics.yml": invalid_metrics__missing_expression_yml, + "people.sql": models__people_sql, + } + + # tests that we get a ParsingException with a missing expression + def test_simple_metric( + self, + project, + ): + # initial run + with pytest.raises(ParsingException): + run_dbt(["run"]) + + names_with_spaces_metrics_yml = """ version: 2