|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from logic_processes_layer.extensions import ProcessAttr |
| 8 | +from logic_processes_layer.extensions.conditions import AttrCondition, FunctionCondition |
| 9 | + |
| 10 | + |
| 11 | +_TWO = 2 |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_process_attr(): |
| 16 | + return Mock(spec=ProcessAttr) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_context(): |
| 21 | + return Mock() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def attr_condition(mock_process_attr): |
| 26 | + return AttrCondition(mock_process_attr) |
| 27 | + |
| 28 | + |
| 29 | +def test_attr_condition_positive(attr_condition, mock_process_attr, mock_context): |
| 30 | + mock_process_attr.get_value.return_value = True |
| 31 | + assert attr_condition(mock_context) is True |
| 32 | + mock_process_attr.get_value.assert_called_once_with(mock_context) |
| 33 | + |
| 34 | + |
| 35 | +def test_attr_condition_negative(attr_condition, mock_process_attr, mock_context): |
| 36 | + mock_process_attr.get_value.return_value = False |
| 37 | + assert attr_condition(mock_context) is False |
| 38 | + mock_process_attr.get_value.assert_called_once_with(mock_context) |
| 39 | + |
| 40 | + |
| 41 | +def test_attr_condition_negated(attr_condition, mock_process_attr, mock_context): |
| 42 | + mock_process_attr.get_value.return_value = True |
| 43 | + condition = ~attr_condition |
| 44 | + |
| 45 | + assert condition(mock_context) is False |
| 46 | + mock_process_attr.get_value.assert_called_once_with(mock_context) |
| 47 | + |
| 48 | + |
| 49 | +def test_function_condition_positive(mock_context): |
| 50 | + mock_func = Mock(return_value=True) |
| 51 | + condition: FunctionCondition = FunctionCondition(mock_func) |
| 52 | + |
| 53 | + assert condition(mock_context) is True |
| 54 | + mock_func.assert_called_once_with(mock_context) |
| 55 | + |
| 56 | + |
| 57 | +def test_function_condition_negative(mock_context): |
| 58 | + mock_func = Mock(return_value=False) |
| 59 | + condition: FunctionCondition = FunctionCondition(mock_func) |
| 60 | + |
| 61 | + assert condition(mock_context) is False |
| 62 | + mock_func.assert_called_once_with(mock_context) |
| 63 | + |
| 64 | + |
| 65 | +def test_function_condition_negated(mock_context): |
| 66 | + mock_func = Mock(return_value=True) |
| 67 | + condition = ~FunctionCondition(mock_func) |
| 68 | + |
| 69 | + assert condition(mock_context) is False |
| 70 | + mock_func.assert_called_once_with(mock_context) |
| 71 | + |
| 72 | + |
| 73 | +def test_operator_condition_and(attr_condition, mock_process_attr, mock_context): |
| 74 | + mock_process_attr.get_value.side_effect = (True, True) |
| 75 | + condition1 = attr_condition |
| 76 | + condition2 = attr_condition |
| 77 | + combined_condition = condition1 & condition2 |
| 78 | + |
| 79 | + assert combined_condition(mock_context) is True |
| 80 | + assert mock_process_attr.get_value.call_count == _TWO |
| 81 | + |
| 82 | + |
| 83 | +def test_operator_condition_or(attr_condition, mock_process_attr, mock_context): |
| 84 | + mock_process_attr.get_value.side_effect = (False, True) |
| 85 | + condition1 = attr_condition |
| 86 | + condition2 = attr_condition |
| 87 | + combined_condition = condition1 | condition2 |
| 88 | + |
| 89 | + assert combined_condition(mock_context) is True |
| 90 | + assert mock_process_attr.get_value.call_count == _TWO |
| 91 | + |
| 92 | + |
| 93 | +def test_operator_condition_complex(attr_condition, mock_process_attr, mock_context): |
| 94 | + mock_process_attr.get_value.side_effect = (True, False, True) |
| 95 | + condition1 = attr_condition |
| 96 | + condition2 = ~attr_condition |
| 97 | + condition3 = attr_condition |
| 98 | + condition4: FunctionCondition = FunctionCondition(lambda _: True) |
| 99 | + combined_condition = condition1 & (condition2 | condition3) & condition4 |
| 100 | + |
| 101 | + assert combined_condition(mock_context) is True |
| 102 | + assert mock_process_attr.get_value.call_count == _TWO |
0 commit comments