Compare commits

...

6 Commits

Author SHA1 Message Date
jeremyyeo
41403936c1 remove Compilation Error messages 2022-06-22 22:37:00 +12:00
jeremyyeo
40d18a1658 move warn_or_raise to construct_mapping 2022-06-22 22:17:06 +12:00
jeremyyeo
6f846f7a85 try integration test 2022-06-16 21:01:49 +12:00
jeremyyeo
fb92371e77 fix assertion 2022-06-16 19:21:32 +12:00
Jeremy Cohen
f7705a3e4b Unit test for yaml loading 2022-06-14 11:39:01 +02:00
jeremyyeo
92847ce90f fix yaml parsing 2022-06-08 22:39:04 +12:00
4 changed files with 61 additions and 8 deletions

View File

@@ -31,11 +31,13 @@ class UniqueKeyLoader(SafeLoader):
def construct_mapping(self, node, deep=False):
mapping = set()
self.flatten_mapping(node) # This processes yaml anchors / merge keys (<<).
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
if key in mapping:
raise dbt.exceptions.DuplicateYamlKeyException(
f"Duplicate {key!r} key found in yaml file"
msg = f"Duplicate {key!r} key found in yaml file"
dbt.exceptions.warn_or_raise(
dbt.exceptions.DuplicateYamlKeyException(msg), log_fmt=warning_tag("{}")
)
mapping.add(key)
return super().construct_mapping(node, deep)
@@ -82,7 +84,3 @@ def load_yaml_text(contents, path=None):
error = str(e)
raise dbt.exceptions.ValidationException(error)
except dbt.exceptions.DuplicateYamlKeyException as e:
# TODO: We may want to raise an exception instead of a warning in the future.
e.msg = f"{e} {path.searched_path}/{path.relative_path}."
dbt.exceptions.warn_or_raise(e, log_fmt=warning_tag("{}"))

View File

@@ -438,7 +438,8 @@ class InvalidSelectorException(RuntimeException):
class DuplicateYamlKeyException(CompilationException):
pass
def __str__(self):
return self.msg
def raise_compiler_error(msg, node=None) -> NoReturn:

View File

@@ -0,0 +1,28 @@
from dbt.clients.yaml_helper import load_yaml_text
import unittest
profile_with_anchor = """
# profiles.yml
postgres:
outputs:
dev: &profile
type: postgres
host: localhost
user: root
password: password
schema: public
database: postgres
port: 5432
threads: 8
prod:
<<: *profile
uat: *profile
target: dev
"""
class YamlLoadingUnitTest(unittest.TestCase):
def test_load_yaml_anchors(self):
profile_yml = load_yaml_text(profile_with_anchor)
assert(profile_yml)

View File

@@ -2,6 +2,7 @@ import pytest
from dbt.tests.util import run_dbt_and_capture, run_dbt
from dbt.exceptions import DuplicateYamlKeyException
from dbt.clients.yaml_helper import load_yaml_text
duplicate_key_schema__schema_yml = """
version: 2
@@ -15,6 +16,18 @@ my_model_sql = """
select 1 as fun
"""
duplicate_vars__dbt_project_yml = """
# dbt_project.yml
vars:
foo: bar
foo: bar
"""
my_model_vars_sql = """
select '{{ var("foo") }}' as val
"""
class TestBasicDuplications:
@pytest.fixture(scope="class")
@@ -26,8 +39,21 @@ class TestBasicDuplications:
def test_warning_in_stdout(self, project):
results, stdout = run_dbt_and_capture(["run"])
assert "Duplicate 'models' key found in yaml file models/schema.yml" in stdout
assert "Duplicate 'models' key found in yaml" in stdout
def test_exception_is_raised_with_warn_error_flag(self, project):
with pytest.raises(DuplicateYamlKeyException):
run_dbt(["--warn-error", "run"])
class TestVarsDuplications:
@pytest.fixture(scope="class")
def project_config_update(self):
return load_yaml_text(duplicate_vars__dbt_project_yml)
@pytest.fixture(scope="class")
def models(self):
return {"my_model_vars.sql": my_model_vars_sql}
def test_duplicate_vars_suceeds(self, project):
run_dbt(["run"])