Skip to content

Commit

Permalink
Merge pull request #36 from godatadriven/test-proj-dirs
Browse files Browse the repository at this point in the history
Adding test for project dirs
  • Loading branch information
pgoslatara authored Jul 19, 2024
2 parents db0db71 + e2cf7af commit cebccaf
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 6 deletions.
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."
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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)

0 comments on commit cebccaf

Please sign in to comment.