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

Migrate to marks and checks #40

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
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
def test_populated_model_description(request, model=None):
import pytest


@pytest.mark.iterate_over_models
def check_populated_model_description(request, model=None):
"""
Models must have a populated description.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_top_level_directories(models):
def check_top_level_directories(models):
for model in models:
top_level_dir = model["path"].split("/")[0]
assert top_level_dir in [
Expand Down
12 changes: 12 additions & 0 deletions dbt_bouncer/checks/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def pytest_configure(config):
config.addinivalue_line("markers", "iterate_over_models: Tests that should run once per model")

ini_values = {
"python_classes": ["check_", "Check"],
"python_files": ["check_*.py"],
"python_functions": ["check_*"],
}

for name, values in ini_values.items():
current = config.getini(name)
current[:] = values
2 changes: 1 addition & 1 deletion dbt_bouncer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cli(dbt_artifacts_dir):
f"Parsed `{manifest_obj.metadata.project_name}` project, found {len(project_models)} nodes, {len(project_sources)} sources and {len(project_tests)} tests."
)

logger.info("Running tests...")
logger.info("Running checks...")
runner(
models=project_models,
sources=project_sources,
Expand Down
20 changes: 13 additions & 7 deletions dbt_bouncer/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __init__(self, model=None, *args, **kwargs):

class GenerateTestsPlugin:
"""
For fixtures that are lists (e.g. `models`) this plugin generates a test for each item in the list.
Using alternaticve approaches like parametrize or fixture_params do not work, generating tests using
For fixtures that are lists (e.g. `models`) this plugin generates a check for each item in the list.
Using alternaticve approaches like parametrize or fixture_params do not work, generating checks using
`pytest_pycollect_makeitem` is one way to get this to work.
"""

Expand All @@ -43,12 +43,13 @@ def __init__(self, models):

def pytest_pycollect_makeitem(self, collector, name, obj):
items = []
if (inspect.isfunction(obj) or inspect.ismethod(obj)) and (name.startswith("test_")):
if (inspect.isfunction(obj) or inspect.ismethod(obj)) and (name.startswith("check_")):
fixture_info = pytest.Function.from_parent(
collector, name=name, callobj=obj
)._fixtureinfo

if "request" in fixture_info.argnames:
markers = pytest.Function.from_parent(collector, name=name).keywords._markers.keys()
if "iterate_over_models" in markers:
for model in self.models:
item = MyFunctionItem.from_parent(
parent=collector,
Expand Down Expand Up @@ -77,13 +78,18 @@ def runner(
Run pytest using fixtures from artifacts.
"""

# Create a fixture plugin that can be used to inject the manifest into the tests
# Create a fixture plugin that can be used to inject the manifest into the checks
fixtures = FixturePlugin()
for att in ["models", "sources", "tests"]:
setattr(fixtures, att + "_", locals()[att])

# Run the tests, if one fails then pytest will raise an exception
# Run the checks, if one fails then pytest will raise an exception
pytest.main(
[(Path(__file__).parent / "tests").__str__(), "-s"],
[
"-c",
(Path(__file__).parent / "checks").__str__(),
(Path(__file__).parent / "checks").__str__(),
"-s",
],
plugins=[fixtures, GenerateTestsPlugin(models)],
)
Binary file modified dist/dbt-bouncer.pex
Binary file not shown.
6 changes: 3 additions & 3 deletions tests/tests/test_models.py → tests/checks/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from dbt_bouncer.tests.test_models import test_populated_model_description
from dbt_bouncer.checks.check_models import check_populated_model_description


@pytest.mark.parametrize(
Expand Down Expand Up @@ -63,6 +63,6 @@
),
],
)
def test_test_populated_model_description(model, expectation):
def test_check_populated_model_description(model, expectation):
with expectation:
test_populated_model_description(model=model, request=None)
check_populated_model_description(model=model, request=None)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from dbt_bouncer.tests.test_project_directories import test_top_level_directories
from dbt_bouncer.checks.check_project_directories import check_top_level_directories


@pytest.mark.parametrize(
Expand Down Expand Up @@ -55,6 +55,6 @@
),
],
)
def test_test_top_level_directories(models, expectation):
def test_check_top_level_directories(models, expectation):
with expectation:
test_top_level_directories(models=models)
check_top_level_directories(models=models)
23 changes: 2 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
import importlib
from pathlib import Path


def pytest_collection_modifyitems(session, config, items):
"""
Pytest runs all functions that start with "test_". This is an issue as out functions in
`./dbt_bouncer/tests` also have this prefix and therefore pytest tries to test those.
This hook is used to dynamically retrieve the names of these functions and remove them
from the list of functions that pytest will run.
"""

dbt_bouncer_tests: list[str] = []
for f in Path("./dbt_bouncer/tests").glob("**/*"):
if f.is_file() and f.name.startswith("test_") and f.name.endswith(".py"):
test_file = importlib.import_module(f"dbt_bouncer.tests.{f.stem}")
dbt_bouncer_tests.extend(
i for i in dir(test_file) if not i.startswith("_") and i != "logger"
)

items[:] = [item for item in items if item.name not in dbt_bouncer_tests]
def pytest_configure(config):
config.addinivalue_line("markers", "iterate_over_models: Tests that should run once per model")