Skip to content

Commit

Permalink
Merge pull request #306 from godatadriven/nicer-error-message
Browse files Browse the repository at this point in the history
Nicer error message for non-valid top-level models
  • Loading branch information
pgoslatara authored Dec 7, 2024
2 parents d9a1216 + 09d56e3 commit 6ce6887
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/dbt_bouncer/checks/manifest/check_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class CheckModelDirectories(BaseCheck):
Receives:
model (DbtBouncerModelBase): The DbtBouncerModelBase object to check.
Raises:
AssertionError: If the model directory does not contain a permitted sub-directory.
Example(s):
```yaml
manifest_checks:
Expand Down Expand Up @@ -237,10 +240,16 @@ def execute(self) -> None:
path_after_match = clean_path_str(self.model.original_file_path)[
matched_path.end() + 1 :
]
directory_to_check = path_after_match.split("/")[0]

assert (
path_after_match.split("/")[0] in self.permitted_sub_directories
), f"`{self.model.name}` is located in `{self.model.original_file_path.split('/')[1]}`, this is not a valid sub-directory."
if directory_to_check.replace(".sql", "") == self.model.name:
raise AssertionError( # noqa: DOC501
f"`{self.model.name}` is not located in a valid sub-directory ({self.permitted_sub_directories})."
)
else:
assert (
directory_to_check in self.permitted_sub_directories
), f"`{self.model.name}` is located in the `{directory_to_check}` sub-directory, this is not a valid sub-directory ({self.permitted_sub_directories})."


class CheckModelDocumentedInSameDirectory(BaseCheck):
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/checks/manifest/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,32 @@ def test_check_model_code_does_not_contain_regexp_pattern(
["finance", "marketing"],
pytest.raises(AssertionError),
),
(
"models",
Nodes4(
**{
"alias": "model_1",
"checksum": {"name": "sha256", "checksum": ""},
"columns": {
"col_1": {
"index": 1,
"name": "col_1",
"type": "INTEGER",
},
},
"fqn": ["package_name", "model_1"],
"name": "model_1",
"original_file_path": "models/model_1.sql",
"package_name": "package_name",
"path": "marts/sales/model_1.sql",
"resource_type": "model",
"schema": "main",
"unique_id": "model.package_name.model_1",
},
),
["finance", "marketing"],
pytest.raises(AssertionError),
),
],
)
def test_check_model_directories(
Expand Down

0 comments on commit 6ce6887

Please sign in to comment.