From 87e808575d59305ebc4134cceb9d6e806ccdf9c5 Mon Sep 17 00:00:00 2001 From: Julian LaNeve Date: Fri, 10 May 2024 10:58:39 -0400 Subject: [PATCH] Ensure tags don't run into index errors when there are no upstream nodes (#933) This PR ensures we only try to access `node.depends_on[0]` if it is an iterable with items. Co-authored-by: Tatiana Al-Chueyr --- cosmos/dbt/selector.py | 2 +- tests/dbt/test_selector.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cosmos/dbt/selector.py b/cosmos/dbt/selector.py index 61458c4aa..47c118b28 100644 --- a/cosmos/dbt/selector.py +++ b/cosmos/dbt/selector.py @@ -296,7 +296,7 @@ def _should_include_node(self, node_id: str, node: DbtNode) -> bool: self.visited_nodes.add(node_id) - if node.resource_type == DbtResourceType.TEST and node.depends_on: + if node.resource_type == DbtResourceType.TEST and node.depends_on and len(node.depends_on) > 0: node.tags = getattr(self.nodes.get(node.depends_on[0]), "tags", []) logger.debug( "The test node <%s> inherited these tags from the parent node <%s>: %s", diff --git a/tests/dbt/test_selector.py b/tests/dbt/test_selector.py index bfb6d7d4e..ece32ac95 100644 --- a/tests/dbt/test_selector.py +++ b/tests/dbt/test_selector.py @@ -4,7 +4,7 @@ from cosmos.constants import DbtResourceType from cosmos.dbt.graph import DbtNode -from cosmos.dbt.selector import SelectorConfig, select_nodes +from cosmos.dbt.selector import NodeSelector, SelectorConfig, select_nodes from cosmos.exceptions import CosmosValueError SAMPLE_PROJ_PATH = Path("/home/user/path/dbt-proj/") @@ -418,3 +418,17 @@ def test_node_without_depends_on_with_tag_selector_should_not_raise_exception(): ) nodes = {standalone_test_node.unique_id: standalone_test_node} assert not select_nodes(project_dir=SAMPLE_PROJ_PATH, nodes=nodes, select=["tag:some-tag"]) + + +def test_should_include_node_without_depends_on(selector_config): + node = DbtNode( + unique_id=f"{DbtResourceType.TEST.value}.{SAMPLE_PROJ_PATH.stem}.standalone", + resource_type=DbtResourceType.TEST, + depends_on=None, + tags=[], + config={}, + file_path=SAMPLE_PROJ_PATH / "tests/generic/builtin.sql", + ) + selector = NodeSelector({}, selector_config) + selector.visited_nodes = set() + selector._should_include_node(node.unique_id, node)