Skip to content

Commit

Permalink
fix + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Apr 29, 2023
1 parent 78f9ab2 commit 1c3b5b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def resolve(
target_version: Optional[NodeVersion] = None,
) -> RelationProxy:
target_model = self.manifest.resolve_ref(
self.model,
target_name,
target_package,
target_version,
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,7 @@ def __init__(self, duped_name, unique_ids, node=None):
def get_message(self) -> str:
msg = (
f'dbt found multiple potential nodes when referencing "{self.duped_name}" - {self.unique_ids}.'
f"\nTo fix this, specify which package to use in 'ref' (one of: {self.packages})."
f"\nTo fix this, specify which package to use as the first argument to 'ref' (one of: {self.packages})."
)
return msg

Expand Down
46 changes: 46 additions & 0 deletions test/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)

from dbt.events.functions import reset_metadata_vars
from dbt.exceptions import AmbiguousResourceNameRefError
from dbt.flags import set_from_args

from dbt.node_types import NodeType
Expand Down Expand Up @@ -1443,6 +1444,36 @@ def _refable_parameter_sets():
version=None,
expected=None,
),
# duplicate node name across package
FindNodeSpec(
nodes=[MockNode("project_a", "my_model"), MockNode("project_b", "my_model")],
sources=[],
package="project_a",
version=None,
expected=("project_a", "my_model"),
),
# duplicate node name across package: root node preferred to package node
FindNodeSpec(
nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")],
sources=[],
package=None,
version=None,
expected=("root", "my_model"),
),
FindNodeSpec(
nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")],
sources=[],
package="root",
version=None,
expected=("root", "my_model"),
),
FindNodeSpec(
nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")],
sources=[],
package="project_a",
version=None,
expected=("project_a", "my_model"),
),
]
)
return sets
Expand Down Expand Up @@ -1486,6 +1517,21 @@ def test_resolve_ref(nodes, sources, package, version, expected):
assert result.package_name == expected_package


def test_resolve_ref_ambiguous_resource_name_across_packages():
manifest = make_manifest(
nodes=[MockNode("project_a", "my_model"), MockNode("project_b", "my_model")]
)
with pytest.raises(AmbiguousResourceNameRefError):
manifest.resolve_ref(
source_node=None,
target_model_name="my_model",
target_model_package=None,
target_model_version=None,
current_project="root",
node_package="root",
)


def _source_parameter_sets():
sets = [
# empties
Expand Down
10 changes: 5 additions & 5 deletions tests/functional/duplicates/test_duplicate_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from dbt.exceptions import CompilationError, DuplicateResourceNameError
from dbt.exceptions import CompilationError, AmbiguousAliasError
from dbt.tests.fixtures.project import write_project_files
from dbt.tests.util import run_dbt, get_manifest

Expand Down Expand Up @@ -88,7 +88,7 @@ def test_duplicate_model_disabled_partial_parsing(self, project):
assert len(results) == 1


class TestDuplicateModelEnabledAcrossPackages:
class TestDuplicateModelAliasEnabledAcrossPackages:
@pytest.fixture(scope="class")
def models(self):
return {"table_model.sql": enabled_model_sql}
Expand All @@ -105,10 +105,10 @@ def setUp(self, project_root):
def packages(self):
return {"packages": [{"local": "local_dependency"}]}

def test_duplicate_model_enabled_across_packages(self, project):
def test_duplicate_model_alias_enabled_across_packages(self, project):
run_dbt(["deps"])
message = "dbt found two models with the name"
with pytest.raises(DuplicateResourceNameError) as exc:
message = "dbt found two resources with the database representation"
with pytest.raises(AmbiguousAliasError) as exc:
run_dbt(["run"])
assert message in str(exc.value)

Expand Down

0 comments on commit 1c3b5b3

Please sign in to comment.