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

test adapter pre hooks for unit testing #10273

Closed
Closed
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
17 changes: 17 additions & 0 deletions tests/functional/unit_testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@
tags: test_this
"""

test_my_model_pass_yml = """
unit_tests:
- name: test_my_model
model: my_model
given:
- input: ref('my_model_a')
rows:
- {id: 1, a: 1}
- input: ref('my_model_b')
rows:
- {id: 1, b: 2}
- {id: 2, b: 2}
expect:
rows:
- {c: 3}
"""


test_my_model_simple_fixture_yml = """
unit_tests:
Expand Down
49 changes: 49 additions & 0 deletions tests/functional/unit_testing/test_ut_adapter_hooks.py
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably want to use capture_and_run_dbt here and inspect the logs instead


mock_pre_model_hook.assert_called_once()
Loading