diff --git a/src/dbt_bouncer/checks/manifest/check_models.py b/src/dbt_bouncer/checks/manifest/check_models.py index d089ec5e..e6c28dec 100644 --- a/src/dbt_bouncer/checks/manifest/check_models.py +++ b/src/dbt_bouncer/checks/manifest/check_models.py @@ -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: @@ -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): diff --git a/tests/unit/checks/manifest/test_models.py b/tests/unit/checks/manifest/test_models.py index 184c88e9..1c34c99b 100644 --- a/tests/unit/checks/manifest/test_models.py +++ b/tests/unit/checks/manifest/test_models.py @@ -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(