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

Adding test for project dirs #36

Merged
merged 1 commit into from
Jul 19, 2024
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
File renamed without changes.
20 changes: 15 additions & 5 deletions dbt_bouncer/runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from pathlib import Path
from typing import Dict, List

import pytest
Expand All @@ -25,8 +26,8 @@ def tests(self):

# Inspiration: https://github.com/pytest-dev/pytest-xdist/discussions/957#discussioncomment-7335007
class MyFunctionItem(pytest.Function):
def __init__(self, model, *args, **kwargs):
self.model: Dict[str, str] = model
def __init__(self, model=None, *args, **kwargs):
self.model: Dict[str, str] | None = model
super().__init__(*args, **kwargs)


Expand All @@ -46,12 +47,21 @@ def pytest_pycollect_makeitem(self, collector, name, obj):
fixture_info = pytest.Function.from_parent(
collector, name=name, callobj=obj
)._fixtureinfo
for model in self.models:

if "request" in fixture_info.argnames:
for model in self.models:
item = MyFunctionItem.from_parent(
parent=collector,
name=name,
fixtureinfo=fixture_info,
model=model,
)
items.append(item)
else:
item = MyFunctionItem.from_parent(
parent=collector,
name=name,
fixtureinfo=fixture_info,
model=model,
)
items.append(item)

Expand All @@ -74,6 +84,6 @@ def runner(

# Run the tests, if one fails then pytest will raise an exception
pytest.main(
["dbt_bouncer/tests"],
[(Path(__file__).parent / "tests").__str__(), "-s"],
plugins=[fixtures, GenerateTestsPlugin(models)],
)
8 changes: 8 additions & 0 deletions dbt_bouncer/tests/test_project_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def test_top_level_directories(models):
for model in models:
top_level_dir = model["path"].split("/")[0]
assert top_level_dir in [
"staging",
"intermediate",
"marts",
], f"{model['unique_id']} is located in `{top_level_dir}`, this is not a valid top-level directory."
2 changes: 1 addition & 1 deletion dbt_project/target/manifest.json

Large diffs are not rendered by default.

Binary file modified dist/dbt-bouncer.pex
Binary file not shown.
60 changes: 60 additions & 0 deletions tests/tests/test_project_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from contextlib import nullcontext as does_not_raise

import pytest

from dbt_bouncer.tests.test_project_directories import test_top_level_directories


@pytest.mark.parametrize(
"models, expectation",
[
(
[
{
"path": "staging/model_1.sql",
"unique_id": "model.package_name.model_1",
}
],
does_not_raise(),
),
(
[
{
"path": "intermediate/model_1.sql",
"unique_id": "model.package_name.model_1",
}
],
does_not_raise(),
),
(
[
{
"path": "marts/model_1.sql",
"unique_id": "model.package_name.model_1",
}
],
does_not_raise(),
),
(
[
{
"path": "model_1.sql",
"unique_id": "model.package_name.model_1",
}
],
pytest.raises(AssertionError),
),
(
[
{
"path": "aggregation/model_1.sql",
"unique_id": "model.package_name.model_1",
}
],
pytest.raises(AssertionError),
),
],
)
def test_test_top_level_directories(models, expectation):
with expectation:
test_top_level_directories(models=models)