-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model contracts: raise warning for numeric types without specified sc…
…ale (#8721) * add warning when contracting fields don't have precision * rename files * changelog * move tests out of adapter zone * Update core/dbt/include/global_project/macros/adapters/columns.sql Co-authored-by: colin-rogers-dbt <[email protected]> * Apply suggestions from code review --------- Co-authored-by: colin-rogers-dbt <[email protected]>
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Raise a warning when a contracted model has a numeric field without scale defined | ||
time: 2023-09-26T13:47:28.645383-05:00 | ||
custom: | ||
Author: emmyoop | ||
Issue: "8183" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import pytest | ||
from dbt.tests.util import run_dbt_and_capture | ||
|
||
|
||
my_numeric_model_sql = """ | ||
select | ||
1.234 as non_integer | ||
""" | ||
|
||
model_schema_numerics_yml = """ | ||
version: 2 | ||
models: | ||
- name: my_numeric_model | ||
config: | ||
contract: | ||
enforced: true | ||
columns: | ||
- name: non_integer | ||
data_type: numeric | ||
""" | ||
|
||
model_schema_numerics_precision_yml = """ | ||
version: 2 | ||
models: | ||
- name: my_numeric_model | ||
config: | ||
contract: | ||
enforced: true | ||
columns: | ||
- name: non_integer | ||
data_type: numeric(38,3) | ||
""" | ||
|
||
|
||
class TestModelContractNumericNoPrecision: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_numeric_model.sql": my_numeric_model_sql, | ||
"schema.yml": model_schema_numerics_yml, | ||
} | ||
|
||
def test_contracted_numeric_without_precision(self, project): | ||
expected_msg = "Detected columns with numeric type and unspecified precision/scale, this can lead to unintended rounding: ['non_integer']" | ||
_, logs = run_dbt_and_capture(["run"], expect_pass=True) | ||
assert expected_msg in logs | ||
_, logs = run_dbt_and_capture(["--warn-error", "run"], expect_pass=False) | ||
assert "Compilation Error in model my_numeric_model" in logs | ||
assert expected_msg in logs | ||
|
||
|
||
class TestModelContractNumericPrecision: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_numeric_model.sql": my_numeric_model_sql, | ||
"schema.yml": model_schema_numerics_precision_yml, | ||
} | ||
|
||
def test_contracted_numeric_with_precision(self, project): | ||
expected_msg = "Detected columns with numeric type and unspecified precision/scale, this can lead to unintended rounding: ['non_integer']" | ||
_, logs = run_dbt_and_capture(["run"], expect_pass=True) | ||
assert expected_msg not in logs |