-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test adapter pre hooks for unit testing
- Loading branch information
1 parent
ef67cff
commit 52e2e85
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from dbt.tests.util import run_dbt | ||
from dbt_common.exceptions import CompilationError | ||
from tests.functional.unit_testing.fixtures import ( | ||
my_model_a_sql, | ||
my_model_b_sql, | ||
my_model_sql, | ||
test_my_model_pass_yml, | ||
) | ||
|
||
|
||
class BaseUnitTestAdapterHook: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_sql, | ||
"my_model_a.sql": my_model_a_sql, | ||
"my_model_b.sql": my_model_b_sql, | ||
"test_my_model.yml": test_my_model_pass_yml, | ||
} | ||
|
||
|
||
class TestUnitTestAdapterHookPasses(BaseUnitTestAdapterHook): | ||
def test_unit_test_runs_adapter_pre_hook(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 3 | ||
|
||
mock_pre_model_hook = mock.Mock() | ||
with mock.patch.object(type(project.adapter), "pre_model_hook", mock_pre_model_hook): | ||
results = run_dbt(["test", "--select", "test_name:test_my_model"], expect_pass=True) | ||
|
||
assert len(results) == 1 | ||
mock_pre_model_hook.assert_called_once() | ||
|
||
|
||
class TestUnitTestAdapterHookFails(BaseUnitTestAdapterHook): | ||
def test_unit_test_runs_adapter_pre_hook_fails(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 3 | ||
|
||
mock_pre_model_hook = mock.Mock() | ||
mock_pre_model_hook.side_effect = CompilationError("exception from adapter.pre_model_hook") | ||
with mock.patch.object(type(project.adapter), "pre_model_hook", mock_pre_model_hook): | ||
run_dbt(["test", "--select", "test_name:test_my_model"], expect_pass=False) | ||
|
||
mock_pre_model_hook.assert_called_once() |