Skip to content

Commit

Permalink
Move to test concrete class over abstract class (#10243)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx authored Jun 4, 2024
1 parent 8c850b5 commit a677abd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 152 deletions.
5 changes: 4 additions & 1 deletion tests/unit/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Unit test README

## test_contracts_graph_parsed.py

### The Why
We need to ensure that we can go from objects to dictionaries and back without any
Expand All @@ -16,3 +15,7 @@ versions of the object we're interested in testing, and run the different genera
of the object through the test. This gives us confidence that for any allowable configuration
of an object, state is not changed when moving back and forth betweeen the python object
version and the seralized version.

### The What

- We test concrete classes in the codebase and do not test abstract classes as they are implementation details. [reference](https://enterprisecraftsmanship.com/posts/how-to-unit-test-an-abstract-class/)
16 changes: 16 additions & 0 deletions tests/unit/task/test_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from unittest.mock import MagicMock, patch

from dbt.flags import get_flags
from dbt.task.clone import CloneTask


def test_clone_task_not_preserve_edges():
mock_node_selector = MagicMock()
mock_spec = MagicMock()
with patch.object(
CloneTask, "get_node_selector", return_value=mock_node_selector
), patch.object(CloneTask, "get_selection_spec", return_value=mock_spec):
task = CloneTask(get_flags(), None, None)
task.get_graph_queue()
# when we get the graph queue, preserve_edges is False
mock_node_selector.get_graph_queue.assert_called_with(mock_spec, False)
52 changes: 52 additions & 0 deletions tests/unit/task/test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from argparse import Namespace
from unittest.mock import MagicMock, patch

import pytest

from dbt.config.runtime import RuntimeConfig
from dbt.flags import get_flags, set_from_args
from dbt.task.run import RunTask
from dbt.tests.util import safe_set_invocation_context


@pytest.mark.parametrize(
"exception_to_raise, expected_cancel_connections",
[
(SystemExit, True),
(KeyboardInterrupt, True),
(Exception, False),
],
)
def test_run_task_cancel_connections(
exception_to_raise, expected_cancel_connections, runtime_config: RuntimeConfig
):
safe_set_invocation_context()

def mock_run_queue(*args, **kwargs):
raise exception_to_raise("Test exception")

with patch.object(RunTask, "run_queue", mock_run_queue), patch.object(
RunTask, "_cancel_connections"
) as mock_cancel_connections:

set_from_args(Namespace(write_json=False), None)
task = RunTask(
get_flags(),
runtime_config,
None,
)
with pytest.raises(exception_to_raise):
task.execute_nodes()
assert mock_cancel_connections.called == expected_cancel_connections


def test_run_task_preserve_edges():
mock_node_selector = MagicMock()
mock_spec = MagicMock()
with patch.object(RunTask, "get_node_selector", return_value=mock_node_selector), patch.object(
RunTask, "get_selection_spec", return_value=mock_spec
):
task = RunTask(get_flags(), None, None)
task.get_graph_queue()
# when we get the graph queue, preserve_edges is True
mock_node_selector.get_graph_queue.assert_called_with(mock_spec, True)
151 changes: 0 additions & 151 deletions tests/unit/task/test_runnable.py

This file was deleted.

0 comments on commit a677abd

Please sign in to comment.