Skip to content

Commit

Permalink
Fix adding test nodes to DAGs built using LoadMethod.DBT_MANIFEST a…
Browse files Browse the repository at this point in the history
…nd `LoadMethod.CUSTOM` (#615)

Resolving issues with the DBT_MANIFEST/CUSTOM load methods when the
has_test attribute is not assigned to the node correctly.

## Description

When a tag selector is used, all tests are filtered out because of the
DbtResourceType.TEST node does not have any information about tags. To
bypass this limitation - tags are assigned to tests based on their
parent model.

## Related Issue(s)

Closes: #580

Co-authored-by: edgarasnavickas <[email protected]>
(cherry picked from commit 58de67e)
  • Loading branch information
navedgaras authored and tatiana committed Nov 6, 2023
1 parent 635fb7b commit 68f7b61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cosmos/dbt/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def should_include_node(node_id: str, node: DbtNode) -> bool:

visited_nodes.add(node_id)

if node.resource_type == DbtResourceType.TEST:
node.tags = getattr(nodes.get(node.depends_on[0]), "tags", [])

if config.tags:
if not (set(config.tags) <= set(node.tags)):
return False
Expand Down
20 changes: 20 additions & 0 deletions tests/dbt/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,26 @@ def test_update_node_dependency_test_not_exist():
assert nodes.has_test is False


def test_tag_selected_node_test_exist():
project_config = ProjectConfig(
dbt_project_path=DBT_PROJECTS_ROOT_DIR / DBT_PROJECT_NAME, manifest_path=SAMPLE_MANIFEST
)
profile_config = ProfileConfig(
profile_name="test",
target_name="test",
profiles_yml_filepath=DBT_PROJECTS_ROOT_DIR / DBT_PROJECT_NAME / "profiles.yml",
)
dbt_graph = DbtGraph(project=project_config, profile_config=profile_config, select=["tag:test_tag"])
dbt_graph.load_from_dbt_manifest()

assert len(dbt_graph.filtered_nodes) > 0

for _, node in dbt_graph.filtered_nodes.items():
assert node.tags == ["test_tag"]
if node.resource_type == DbtResourceType.MODEL:
assert node.has_test is True


@pytest.mark.integration
@pytest.mark.parametrize("load_method", ["load_via_dbt_ls", "load_from_dbt_manifest"])
def test_load_dbt_ls_and_manifest_with_model_version(load_method):
Expand Down
8 changes: 6 additions & 2 deletions tests/sample/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7576,7 +7576,9 @@
"resource_type": "model",
"schema": "public",
"sources": [],
"tags": [],
"tags": [
"test_tag"
],
"unique_id": "model.jaffle_shop.customers",
"unrendered_config": {
"materialized": "table"
Expand Down Expand Up @@ -7754,7 +7756,9 @@
"resource_type": "model",
"schema": "public",
"sources": [],
"tags": [],
"tags": [
"test_tag"
],
"unique_id": "model.jaffle_shop.orders",
"unrendered_config": {
"materialized": "table"
Expand Down

0 comments on commit 68f7b61

Please sign in to comment.