Skip to content

Commit

Permalink
Merge pull request #37
Browse files Browse the repository at this point in the history
Respect the dbt exit codes
  • Loading branch information
Bilbottom authored Oct 12, 2024
2 parents f8ab2a2 + 2ef47e4 commit edf43fa
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The existing Python modules are available in the dbt Jinja context under the `mo
While in preview, this package is only available from GitHub:

```
pip install git+https://github.com/Bilbottom/[email protected].2
pip install git+https://github.com/Bilbottom/[email protected].3
```

This will be made available on PyPI once it's ready for general use.
Expand Down Expand Up @@ -130,3 +130,14 @@ This is still in preview, and there are a few things to be added before it's rea
- Support for importing any number of packages (currently only one package is supported)
- Configuration via config files and CLI arguments (currently only environment variables are supported)
- More robust testing
## Contributing 🤝
Raise an issue, or fork the repo and open a pull request.
This project uses [Poetry](https://python-poetry.org/) for dependency management and [pre-commit](https://pre-commit.com/) for linting. After cloning the repo, install the dependencies and enable pre-commit:
```
poetry install --sync --with dev,test
pre-commit install --install-hooks
```
6 changes: 3 additions & 3 deletions coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion dbt_py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ def main() -> None:
- https://docs.getdbt.com/reference/programmatic-invocations
"""
dbt.context.base.get_context_modules = new_get_context_modules
dbt.cli.main.dbtRunner().invoke(sys.argv[1:])
result = dbt.cli.main.dbtRunner().invoke(sys.argv[1:])
if result.success:
sys.exit(0)
elif result.exception is None:
sys.exit(1)
else:
sys.exit(2)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "dbt-py-wrap" # Not `dbt-py` to avoid confusion with the DbtPy package (PyPI requirement)
version = "0.0.2"
version = "0.0.3"
description = "Python wrapper for dbt-core to extend dbt with custom Python."
readme = "README.md"
authors = ["Bilbottom"]
Expand Down
48 changes: 45 additions & 3 deletions tests/integration/test__integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
Integration tests for the package.
"""

import dataclasses
import pathlib
import shutil
import textwrap
import unittest.mock

import dbt.cli.main
import pytest

import dbt_py
Expand All @@ -33,7 +35,7 @@


@pytest.fixture
def mock_env(monkeypatch) -> None:
def mock_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""
Mock the environment variables used by dbt_py.
"""
Expand All @@ -50,7 +52,7 @@ def mock_env(monkeypatch) -> None:
@pytest.fixture
def teardown() -> None:
"""
Remove the compiled directory if it exists.
Remove the dbt target directory if it exists.
"""
yield
# TODO: I think this should be dynamic
Expand All @@ -64,6 +66,46 @@ def test__dbt_can_be_successfully_invoked(mock_env, teardown) -> None:
Test that dbt can be successfully invoked.
"""
with unittest.mock.patch("sys.argv", ["", "compile", *ARGS]):
dbt_py.main()
with pytest.raises(SystemExit) as exit_info:
dbt_py.main()

assert exit_info.value.code == 0
assert EXAMPLE_FILE.read_text(encoding="utf-8").strip() == EXAMPLE_COMPILED.strip()


@pytest.mark.parametrize(
"success, exception, expected_exit_code",
[
(True, None, 0),
(False, None, 1),
(False, Exception("something bad"), 2),
],
)
def test__errors_return_the_correct_exit_code(
monkeypatch: pytest.MonkeyPatch,
success: bool,
exception: BaseException | None,
expected_exit_code: int,
) -> None:
"""
Test that the correct exit code is returned, following the dbt docs:
- https://docs.getdbt.com/reference/programmatic-invocations#dbtrunnerresult
"""

@dataclasses.dataclass
class MockRunnerResult:
success: bool
exception: BaseException | None

class MockRunner:
def invoke(self, args): # noqa
return MockRunnerResult(success=success, exception=exception)

monkeypatch.setattr(dbt.cli.main, "dbtRunner", MockRunner)

with unittest.mock.patch("sys.argv", ["", "compile", *ARGS]):
with pytest.raises(SystemExit) as exit_info:
dbt_py.main()

assert exit_info.value.code == expected_exit_code

0 comments on commit edf43fa

Please sign in to comment.