From 2c602632ceb5a850c5089c05d4f193c70c2a6be4 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 16 Sep 2022 15:45:13 -0500 Subject: [PATCH 1/5] adding validation --- core/dbt/contracts/graph/unparsed.py | 2 +- tests/functional/metrics/test_metrics.py | 35 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/dbt/contracts/graph/unparsed.py b/core/dbt/contracts/graph/unparsed.py index 167612b3517..7048574dd75 100644 --- a/core/dbt/contracts/graph/unparsed.py +++ b/core/dbt/contracts/graph/unparsed.py @@ -476,7 +476,7 @@ class UnparsedMetric(dbtClassMixin, Replaceable): calculation_method: str timestamp: str description: str = "" - expression: Union[str, int] = "" + expression: Union[str, int] = None 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..c13e7d49868 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -195,6 +195,41 @@ def test_simple_metric( with pytest.raises(ParsingException): run_dbt(["run"]) +invalid_metrics__missing_expression_yml = """ +version: 2 +metrics: + - name: number_of_people + label: "Number of 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 an invalid model ref, where + # the model name does not have quotes + def test_simple_metric( + self, + project, + ): + # initial run + with pytest.raises(ParsingException): + run_dbt(["run"]) + names_with_spaces_metrics_yml = """ version: 2 From c1ffd547d0aeb2bcfafd94c67627925adff80175 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 16 Sep 2022 15:47:25 -0500 Subject: [PATCH 2/5] changie --- .changes/unreleased/Under the Hood-20220916-154712.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/unreleased/Under the Hood-20220916-154712.yaml 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" From 76eae58cc6e78b31f52790c13058cca314e741f7 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 16 Sep 2022 16:03:05 -0500 Subject: [PATCH 3/5] code formatting --- tests/functional/metrics/test_metrics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/functional/metrics/test_metrics.py b/tests/functional/metrics/test_metrics.py index c13e7d49868..17798894f7f 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -195,6 +195,7 @@ def test_simple_metric( with pytest.raises(ParsingException): run_dbt(["run"]) + invalid_metrics__missing_expression_yml = """ version: 2 metrics: From ab34ec2de46bfd6b7a0a2345149c2fe34e74429b Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Mon, 26 Sep 2022 11:35:37 -0500 Subject: [PATCH 4/5] updating for review --- core/dbt/contracts/graph/parsed.py | 2 +- core/dbt/contracts/graph/unparsed.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 7048574dd75..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] = None time_grains: List[str] = field(default_factory=list) dimensions: List[str] = field(default_factory=list) window: Optional[MetricTime] = None From 50938067d42677e2190ea4cc596d85ddc6565524 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Tue, 27 Sep 2022 10:50:28 -0500 Subject: [PATCH 5/5] updating tests --- tests/functional/metrics/test_metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/metrics/test_metrics.py b/tests/functional/metrics/test_metrics.py index 17798894f7f..c6f398350f3 100644 --- a/tests/functional/metrics/test_metrics.py +++ b/tests/functional/metrics/test_metrics.py @@ -201,6 +201,7 @@ def test_simple_metric( metrics: - name: number_of_people label: "Number of people" + model: "ref(people)" description: Total count of people calculation_method: count timestamp: created_at @@ -221,8 +222,7 @@ def models(self): "people.sql": models__people_sql, } - # tests that we get a ParsingException with an invalid model ref, where - # the model name does not have quotes + # tests that we get a ParsingException with a missing expression def test_simple_metric( self, project,