Skip to content

Commit

Permalink
feat(snapshot): Squash commit with duplicate column validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ariosramirez committed Sep 20, 2024
1 parent c6b8f7e commit b859dea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240228-192518.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Validation to detect duplicate column names in the check_cols configuration of a snapshot.
time: 2024-02-28T19:25:18.00447-03:00
custom:
Author: ariosramirez
Issue: "9656"
6 changes: 6 additions & 0 deletions core/dbt/artifacts/resources/v1/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def final_validate(self):
f"Invalid value for 'check_cols': {self.check_cols}. "
"Expected 'all' or a list of strings."
)
# Validate if there are duplicate column names in check_cols.
if isinstance(self.check_cols, list):
if len(self.check_cols) != len(set(self.check_cols)):
raise ValidationError(
f"Duplicate column names in 'check_cols':" f" {self.check_cols}."
)
elif self.strategy == "timestamp":
if not self.updated_at:
raise ValidationError(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/contracts/graph/test_nodes_parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,16 @@ def assert_snapshot_config_fails_validation(dct):
obj.final_validate()


def test_duplicate_check_cols(basic_check_snapshot_config_dict):
duplicate_cols = basic_check_snapshot_config_dict
# Introducing duplicate column names
duplicate_cols["check_cols"] = ["col1", "col2", "col2"]
with pytest.raises(ValidationError, match=r"Duplicate column names in 'check_cols'"):
SnapshotConfig.validate(duplicate_cols)
cfg = SnapshotConfig.from_dict(duplicate_cols)
cfg.final_validate()


def test_invalid_check_value(basic_check_snapshot_config_dict):
invalid_check_type = basic_check_snapshot_config_dict
invalid_check_type["check_cols"] = "some"
Expand Down

0 comments on commit b859dea

Please sign in to comment.