-
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.
Call adapter.pre_model_hook + adapter.post_model_hook within tests (#…
…10258) * init push for issue 10198 * add changelog * add unit tests based on michelle example * add data_tests, and post_hook unit tests * pull creating macro_func out of try call * revert last commit * pull macro_func definition back out of try * update code formatting
- Loading branch information
1 parent
100352d
commit 27b2f96
Showing
5 changed files
with
222 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: add pre_model and post_model hook calls to data and unit tests to be able to provide extra config options | ||
time: 2024-06-06T11:23:34.758675-05:00 | ||
custom: | ||
Author: McKnight-42 | ||
Issue: "10198" |
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,111 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
from dbt_common.exceptions import CompilationError | ||
|
||
orders_csv = """order_id,order_date,customer_id | ||
1,2024-06-01,1001 | ||
2,2024-06-02,1002 | ||
3,2024-06-03,1003 | ||
4,2024-06-04,1004 | ||
""" | ||
|
||
|
||
orders_model_sql = """ | ||
with source as ( | ||
select | ||
order_id, | ||
order_date, | ||
customer_id | ||
from {{ ref('seed_orders') }} | ||
), | ||
final as ( | ||
select | ||
order_id, | ||
order_date, | ||
customer_id | ||
from source | ||
) | ||
select * from final | ||
""" | ||
|
||
|
||
orders_test_sql = """ | ||
select * | ||
from {{ ref('orders') }} | ||
where order_id is null | ||
""" | ||
|
||
|
||
class BaseSingularTestHooks: | ||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return {"seed_orders.csv": orders_csv} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"orders.sql": orders_model_sql} | ||
|
||
@pytest.fixture(scope="class") | ||
def tests(self): | ||
return {"orders_test.sql": orders_test_sql} | ||
|
||
|
||
class TestSingularTestPreHook(BaseSingularTestHooks): | ||
def test_data_test_runs_adapter_pre_hook_pass(self, project): | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 1 | ||
|
||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
mock_pre_model_hook = mock.Mock() | ||
with mock.patch.object(type(project.adapter), "pre_model_hook", mock_pre_model_hook): | ||
results = run_dbt(["test"], expect_pass=True) | ||
assert len(results) == 1 | ||
mock_pre_model_hook.assert_called_once() | ||
|
||
def test_data_test_runs_adapter_pre_hook_fails(self, project): | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 1 | ||
|
||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
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): | ||
(_, log_output) = run_dbt_and_capture(["test"], expect_pass=False) | ||
assert "exception from adapter.pre_model_hook" in log_output | ||
|
||
|
||
class TestSingularTestPostHook(BaseSingularTestHooks): | ||
def test_data_test_runs_adapter_post_hook_pass(self, project): | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 1 | ||
|
||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
mock_post_model_hook = mock.Mock() | ||
with mock.patch.object(type(project.adapter), "post_model_hook", mock_post_model_hook): | ||
results = run_dbt(["test"], expect_pass=True) | ||
assert len(results) == 1 | ||
mock_post_model_hook.assert_called_once() | ||
|
||
def test_data_test_runs_adapter_post_hook_fails(self, project): | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 1 | ||
|
||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
mock_post_model_hook = mock.Mock() | ||
mock_post_model_hook.side_effect = CompilationError( | ||
"exception from adapter.post_model_hook" | ||
) | ||
with mock.patch.object(type(project.adapter), "post_model_hook", mock_post_model_hook): | ||
(_, log_output) = run_dbt_and_capture(["test"], expect_pass=False) | ||
assert "exception from adapter.post_model_hook" in log_output |
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,75 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
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 TestUnitTestAdapterPreHook(BaseUnitTestAdapterHook): | ||
def test_unit_test_runs_adapter_pre_hook_passes(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() | ||
|
||
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): | ||
(_, log_output) = run_dbt_and_capture( | ||
["test", "--select", "test_name:test_my_model"], expect_pass=False | ||
) | ||
assert "exception from adapter.pre_model_hook" in log_output | ||
|
||
|
||
class TestUnitTestAdapterPostHook(BaseUnitTestAdapterHook): | ||
def test_unit_test_runs_adapter_post_hook_pass(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 3 | ||
|
||
mock_post_model_hook = mock.Mock() | ||
with mock.patch.object(type(project.adapter), "post_model_hook", mock_post_model_hook): | ||
results = run_dbt(["test", "--select", "test_name:test_my_model"], expect_pass=True) | ||
|
||
assert len(results) == 1 | ||
mock_post_model_hook.assert_called_once() | ||
|
||
def test_unit_test_runs_adapter_post_hook_fails(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 3 | ||
|
||
mock_post_model_hook = mock.Mock() | ||
mock_post_model_hook.side_effect = CompilationError( | ||
"exception from adapter.post_model_hook" | ||
) | ||
with mock.patch.object(type(project.adapter), "post_model_hook", mock_post_model_hook): | ||
(_, log_output) = run_dbt_and_capture( | ||
["test", "--select", "test_name:test_my_model"], expect_pass=False | ||
) | ||
assert "exception from adapter.post_model_hook" in log_output |