|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from sqlalchemy.orm import Session |
| 5 | + |
| 6 | +from cads_worker import worker |
| 7 | + |
| 8 | + |
| 9 | +def mock_session_maker(): |
| 10 | + return Session() |
| 11 | + |
| 12 | + |
| 13 | +def test_ensure_session_decorator(): |
| 14 | + # Test case 1: when session is None |
| 15 | + @worker.ensure_session |
| 16 | + def sample_function(self, session=None): |
| 17 | + assert isinstance(session, Session) |
| 18 | + return session |
| 19 | + |
| 20 | + with patch( |
| 21 | + "cads_worker.worker.create_session_maker", return_value=mock_session_maker |
| 22 | + ): |
| 23 | + result = sample_function(self=None) |
| 24 | + assert isinstance(result, Session) |
| 25 | + |
| 26 | + # Test case 2: when session is provided |
| 27 | + mock_session = Session() |
| 28 | + result = sample_function(self=None, session=mock_session) |
| 29 | + assert result is mock_session |
| 30 | + |
| 31 | + # Clean up |
| 32 | + mock_session.close() |
| 33 | + |
| 34 | + |
| 35 | +def test_ensure_session_decorator_nested(): |
| 36 | + # Test nested function calls with session |
| 37 | + @worker.ensure_session |
| 38 | + def outer_function(self, session=None): |
| 39 | + @worker.ensure_session |
| 40 | + def inner_function(self, session=None): |
| 41 | + return session |
| 42 | + |
| 43 | + return inner_function(self=None, session=session) |
| 44 | + |
| 45 | + with patch( |
| 46 | + "cads_worker.worker.create_session_maker", return_value=mock_session_maker |
| 47 | + ): |
| 48 | + result = outer_function(self=None) |
| 49 | + assert isinstance(result, Session) |
| 50 | + result.close() |
| 51 | + |
| 52 | + |
| 53 | +def test_ensure_session_decorator_error(): |
| 54 | + # Test error handling |
| 55 | + @worker.ensure_session |
| 56 | + def failing_function(self, session=None): |
| 57 | + raise ValueError("Test error") |
| 58 | + |
| 59 | + with pytest.raises(ValueError): |
| 60 | + failing_function(self=None) |
| 61 | + |
| 62 | + |
| 63 | +call_count = 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_ensure_session_retry(): |
| 67 | + # Test retries |
| 68 | + |
| 69 | + context = worker.Context() |
| 70 | + |
| 71 | + @worker.ensure_session |
| 72 | + def failing_function(self, session=None): |
| 73 | + print("failing function called") |
| 74 | + raise worker.cads_broker.database.sa.exc.OperationalError( |
| 75 | + "Simulated DB error", None, None |
| 76 | + ) |
| 77 | + |
| 78 | + with patch( |
| 79 | + "cads_worker.worker.create_session_maker", return_value=mock_session_maker |
| 80 | + ): |
| 81 | + with pytest.raises(worker.cads_broker.database.sa.exc.OperationalError): |
| 82 | + # This should raise after max retries |
| 83 | + failing_function(self=context) |
| 84 | + |
| 85 | + global call_count |
| 86 | + |
| 87 | + @worker.ensure_session |
| 88 | + def successful_function(self, session=None): |
| 89 | + global call_count |
| 90 | + if call_count < 3: |
| 91 | + print("failing function called - retries:", call_count) |
| 92 | + call_count += 1 |
| 93 | + raise worker.cads_broker.database.sa.exc.OperationalError( |
| 94 | + "Simulated DB error", None, None |
| 95 | + ) |
| 96 | + else: |
| 97 | + print("successful function called - retries:", call_count) |
| 98 | + return session |
| 99 | + |
| 100 | + with patch( |
| 101 | + "cads_worker.worker.create_session_maker", return_value=mock_session_maker |
| 102 | + ): |
| 103 | + result = successful_function(self=context) |
| 104 | + assert isinstance(result, Session) |
0 commit comments