Skip to content

Commit

Permalink
added some improvements to safe decorators tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davyd_davyd committed May 18, 2024
1 parent e531f62 commit cd5a099
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,75 @@
from dddesign.utils.safe_decorators import retry_once_after_exception


# Define some test functions to use with the decorator
@retry_once_after_exception
def func_success(a: Any, b: Any): # noqa: ARG001
return a + b


@retry_once_after_exception
def func_raises_once_then_succeeds(a: Any, b: Any, state: dict): # noqa: ARG001
if not state.get('retried', False):
state['retried'] = True
raise ValueError('Test exception')
return a + b


@retry_once_after_exception
def func_raises_always(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@retry_once_after_exception(capture_exception=False)
def func_no_capture_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@retry_once_after_exception(exceptions=(ValueError,))
def func_specific_exception_handling(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@retry_once_after_exception(exceptions=(ValueError,))
def func_specific_exception_not_handling(a: Any, b: Any): # noqa: ARG001
raise TypeError('Test exception')


class TestRetryOnceAfterExceptionDecorator(TestCase):
def test_successful_call(self):
def test_func_success(self):
# Arrange
@retry_once_after_exception
def func_success(a: Any, b: Any): # noqa: ARG001
return a + b

# Act & Assert
self.assertEqual(func_success(2, 3), 5)

@patch('dddesign.utils.safe_decorators.logger')
def test_retry_once_after_exception(self, mock_logger):
def test_func_raises_once_then_succeeds(self, mock_logger):
# Arrange
state = {}
@retry_once_after_exception
def func_raises_once_then_succeeds(a: Any, b: Any, state: dict): # noqa: ARG001
if not state.get('retried', False):
state['retried'] = True
raise ValueError('Test exception')
return a + b

_state = {}

# Act & Assert
self.assertEqual(func_raises_once_then_succeeds(2, 3, state), 5)
self.assertEqual(func_raises_once_then_succeeds(2, 3, _state), 5)
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_always_raises_exception(self, mock_logger):
def test_func_raises_always(self, mock_logger):
# Arrange
@retry_once_after_exception
def func_raises_always(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
with self.assertRaises(ValueError):
func_raises_always(2, 3)
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_no_capture_exception(self, mock_logger):
# Arrange
@retry_once_after_exception(capture_exception=False)
def func_no_capture_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
with self.assertRaises(ValueError):
func_no_capture_exception(2, 3)
mock_logger.exception.assert_not_called()

@patch('dddesign.utils.safe_decorators.logger')
def test_specific_exception_handling(self, mock_logger):
# Arrange
@retry_once_after_exception(exceptions=(ValueError,))
def func_specific_exception_handling(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
with self.assertRaises(ValueError):
func_specific_exception_handling(2, 3)
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_specific_exception_not_handling(self, mock_logger):
# Arrange
@retry_once_after_exception(exceptions=(ValueError,))
def func_specific_exception_not_handling(a: Any, b: Any): # noqa: ARG001
raise TypeError('Test exception')

# Act & Assert
with self.assertRaises(TypeError):
func_specific_exception_not_handling(2, 3)
Expand Down
73 changes: 36 additions & 37 deletions tests/utils/safe_decorators/test_safe_call_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,67 @@
from dddesign.utils.safe_decorators import safe_call


# Define some test functions to use with the decorator
@safe_call
def func_success(a: Any, b: Any): # noqa: ARG001
return a + b


@safe_call
def func_raises_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@safe_call(default_result='default')
def func_with_default(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@safe_call(capture_exception=False)
def func_no_capture_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@safe_call(exceptions=(ValueError,))
def func_specific_exception_handling(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')


@safe_call(exceptions=(ValueError,))
def func_specific_exception_not_handling(a: Any, b: Any): # noqa: ARG001
raise TypeError('Test exception')


class TestSafeCallDecorator(TestCase):
def test_successful_call(self):
def test_func_success(self):
# Arrange
@safe_call
def func_success(a: Any, b: Any): # noqa: ARG001
return a + b

# Act & Assert
self.assertEqual(func_success(2, 3), 5)

@patch('dddesign.utils.safe_decorators.logger')
def test_exception_handling_with_capture(self, mock_logger):
def test_func_raises_exception(self, mock_logger):
# Arrange
@safe_call
def func_raises_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
self.assertIsNone(func_raises_exception(2, 3))
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_exception_handling_with_default_result(self, mock_logger):
def test_func_with_default(self, mock_logger):
# Arrange
@safe_call(default_result='default')
def func_with_default(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
self.assertEqual(func_with_default(2, 3), 'default')
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_no_capture_exception(self, mock_logger):
def test_func_no_capture_exception(self, mock_logger):
# Arrange
@safe_call(capture_exception=False)
def func_no_capture_exception(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
self.assertIsNone(func_no_capture_exception(2, 3))
mock_logger.exception.assert_not_called()

@patch('dddesign.utils.safe_decorators.logger')
def test_specific_exception_handling(self, mock_logger):
def test_func_specific_exception_handling(self, mock_logger):
# Arrange
@safe_call(exceptions=(ValueError,))
def func_specific_exception_handling(a: Any, b: Any): # noqa: ARG001
raise ValueError('Test exception')

# Act & Assert
self.assertIsNone(func_specific_exception_handling(2, 3))
mock_logger.exception.assert_called_once()

@patch('dddesign.utils.safe_decorators.logger')
def test_specific_exception_not_handling(self, mock_logger):
def test_func_specific_exception_not_handling(self, mock_logger):
# Arrange
@safe_call(exceptions=(ValueError,))
def func_specific_exception_not_handling(a: Any, b: Any): # noqa: ARG001
raise TypeError('Test exception')

# Act & Assert
with self.assertRaises(TypeError):
func_specific_exception_not_handling(2, 3)
Expand Down

0 comments on commit cd5a099

Please sign in to comment.