Skip to content

Commit

Permalink
Adding metric expression validation (#5873)
Browse files Browse the repository at this point in the history
* adding validation

* changie

* code formatting

* updating for review

* updating tests
  • Loading branch information
callum-mcdata authored Sep 27, 2022
1 parent 59744f1 commit 17aca39
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20220916-154712.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,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]
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,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
Expand Down
36 changes: 36 additions & 0 deletions tests/functional/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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
Expand Down

0 comments on commit 17aca39

Please sign in to comment.