Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: --exclude in dbt sync #109

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/preset_cli/cli/superset/sync/dbt/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,21 @@ def apply_select(
"""
Apply dbt node selection (https://docs.getdbt.com/reference/node-selection/syntax).
"""
if not select:
return models
Comment on lines -389 to -390
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this was wrong. We should only return all models if exclude is also falsy.


model_ids = {model["unique_id"]: model for model in models}

selected: Dict[str, ModelSchema] = {}
for selection in select:
ids = set.intersection(
*[
{model["unique_id"] for model in filter_models(models, condition)}
for condition in selection.split(",")
]
)
selected.update({id_: model_ids[id_] for id_ in ids})
selected: Dict[str, ModelSchema]
if not select:
selected = {model["unique_id"]: model for model in models}
else:
selected = {}
for selection in select:
ids = set.intersection(
*[
{model["unique_id"] for model in filter_models(models, condition)}
for condition in selection.split(",")
]
)
selected.update({id_: model_ids[id_] for id_ in ids})

for selection in exclude:
for id_ in set.intersection(
Expand All @@ -408,6 +409,7 @@ def apply_select(
for condition in selection.split(",")
]
):
del selected[id_]
if id_ in selected:
del selected[id_]

return list(selected.values())
21 changes: 19 additions & 2 deletions tests/cli/superset/sync/dbt/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_build_snowflake_sqlalchemy_params_pk(fs: FakeFilesystem) -> None:
"privatekey_body": "-----BEGIN ENCRYPTED PRIVATE KEY",
"privatekey_pass": "XXX",
},
}
},
),
}

Expand Down Expand Up @@ -437,10 +437,27 @@ def test_apply_select() -> None:
"one",
}

# test exclude
assert {
model["name"]
for model in apply_select(models, ("+two+",), ("three", "tag:test"))
} == {
"two",
}


def test_apply_select_exclude() -> None:
"""
Custom tests for the ``exclude`` option.
"""
a = dict(name="a", tags=[], unique_id="a", depends_on=[], children=["b", "c"])
b = dict(name="b", tags=[], unique_id="b", depends_on=["a"], children=["d"])
c = dict(name="c", tags=[], unique_id="c", depends_on=["a"], children=["d"])
d = dict(name="d", tags=[], unique_id="d", depends_on=["b", "c"], children=[])
models: List[ModelSchema] = [a, b, c, d] # type: ignore

assert {model["name"] for model in apply_select(models, (), ("d",))} == {
"a",
"b",
"c",
}
assert {model["name"] for model in apply_select(models, (), ("b+", "c+"))} == {"a"}