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

Issue 9656 Raise an error when check_cols has duplicated values #9698

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
4 changes: 4 additions & 0 deletions core/dbt/artifacts/resources/v1/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def validate(cls, data):
if data.get("materialized") and data.get("materialized") != "snapshot":
raise ValidationError("A snapshot must have a materialized value of 'snapshot'")

# Validate if there are duplicate column names in check_cols.
if len(data.get("check_cols")) != len(set(data.get("check_cols"))):
raise ValidationError(f"Duplicate column names in 'check_cols': {data['check_cols']}.")

# Called by "calculate_node_config_dict" in ContextConfigGenerator
def finalize_and_validate(self):
data = self.to_dict(omit_none=True)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_contracts_graph_parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,14 @@ def test_missing_snapshot_configs(basic_check_snapshot_config_dict):
SnapshotConfig.validate(wrong_fields)


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)


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