|
| 1 | +""" |
| 2 | +Tests for individual expense activities. |
| 3 | +Focuses on activity behavior, parameters, error handling, and HTTP interactions. |
| 4 | +""" |
| 5 | + |
| 6 | +import uuid |
| 7 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 8 | + |
| 9 | +import httpx |
| 10 | +import pytest |
| 11 | +from temporalio import activity |
| 12 | +from temporalio.activity import _CompleteAsyncError |
| 13 | +from temporalio.testing import ActivityEnvironment |
| 14 | + |
| 15 | +from expense import EXPENSE_SERVER_HOST_PORT |
| 16 | +from expense.activities import ( |
| 17 | + create_expense_activity, |
| 18 | + payment_activity, |
| 19 | + wait_for_decision_activity, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class TestCreateExpenseActivity: |
| 24 | + """Test create_expense_activity individual behavior""" |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def activity_env(self): |
| 28 | + return ActivityEnvironment() |
| 29 | + |
| 30 | + async def test_create_expense_activity_success(self, activity_env): |
| 31 | + """Test successful expense creation""" |
| 32 | + with patch("httpx.AsyncClient") as mock_client: |
| 33 | + # Mock successful HTTP response |
| 34 | + mock_response = AsyncMock() |
| 35 | + mock_response.text = "SUCCEED" |
| 36 | + mock_response.raise_for_status = AsyncMock() |
| 37 | + |
| 38 | + mock_client_instance = AsyncMock() |
| 39 | + mock_client_instance.get.return_value = mock_response |
| 40 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 41 | + |
| 42 | + # Execute activity |
| 43 | + result = await activity_env.run(create_expense_activity, "test-expense-123") |
| 44 | + |
| 45 | + # Verify HTTP call |
| 46 | + mock_client_instance.get.assert_called_once_with( |
| 47 | + f"{EXPENSE_SERVER_HOST_PORT}/create", |
| 48 | + params={"is_api_call": "true", "id": "test-expense-123"}, |
| 49 | + ) |
| 50 | + mock_response.raise_for_status.assert_called_once() |
| 51 | + |
| 52 | + # Activity should return None on success |
| 53 | + assert result is None |
| 54 | + |
| 55 | + async def test_create_expense_activity_empty_id(self, activity_env): |
| 56 | + """Test create expense activity with empty expense ID""" |
| 57 | + with pytest.raises(ValueError, match="expense id is empty"): |
| 58 | + await activity_env.run(create_expense_activity, "") |
| 59 | + |
| 60 | + async def test_create_expense_activity_http_error(self, activity_env): |
| 61 | + """Test create expense activity with HTTP error""" |
| 62 | + with patch("httpx.AsyncClient") as mock_client: |
| 63 | + # Mock HTTP error - use MagicMock for raise_for_status to avoid async issues |
| 64 | + mock_response = MagicMock() |
| 65 | + mock_response.raise_for_status.side_effect = httpx.HTTPStatusError( |
| 66 | + "Server Error", request=MagicMock(), response=MagicMock() |
| 67 | + ) |
| 68 | + |
| 69 | + mock_client_instance = AsyncMock() |
| 70 | + mock_client_instance.get.return_value = mock_response |
| 71 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 72 | + |
| 73 | + with pytest.raises(httpx.HTTPStatusError): |
| 74 | + await activity_env.run(create_expense_activity, "test-expense-123") |
| 75 | + |
| 76 | + async def test_create_expense_activity_server_error_response(self, activity_env): |
| 77 | + """Test create expense activity with server error response""" |
| 78 | + with patch("httpx.AsyncClient") as mock_client: |
| 79 | + # Mock error response |
| 80 | + mock_response = AsyncMock() |
| 81 | + mock_response.text = "ERROR:ID_ALREADY_EXISTS" |
| 82 | + mock_response.raise_for_status = AsyncMock() |
| 83 | + |
| 84 | + mock_client_instance = AsyncMock() |
| 85 | + mock_client_instance.get.return_value = mock_response |
| 86 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 87 | + |
| 88 | + with pytest.raises(Exception, match="ERROR:ID_ALREADY_EXISTS"): |
| 89 | + await activity_env.run(create_expense_activity, "test-expense-123") |
| 90 | + |
| 91 | + |
| 92 | +class TestWaitForDecisionActivity: |
| 93 | + """Test wait_for_decision_activity individual behavior""" |
| 94 | + |
| 95 | + @pytest.fixture |
| 96 | + def activity_env(self): |
| 97 | + return ActivityEnvironment() |
| 98 | + |
| 99 | + async def test_wait_for_decision_activity_empty_id(self, activity_env): |
| 100 | + """Test wait for decision activity with empty expense ID""" |
| 101 | + with pytest.raises(ValueError, match="expense id is empty"): |
| 102 | + await activity_env.run(wait_for_decision_activity, "") |
| 103 | + |
| 104 | + async def test_wait_for_decision_activity_callback_registration_success( |
| 105 | + self, activity_env |
| 106 | + ): |
| 107 | + """Test successful callback registration behavior""" |
| 108 | + with patch("httpx.AsyncClient") as mock_client: |
| 109 | + # Mock successful callback registration |
| 110 | + mock_response = AsyncMock() |
| 111 | + mock_response.text = "SUCCEED" |
| 112 | + mock_response.raise_for_status = AsyncMock() |
| 113 | + |
| 114 | + mock_client_instance = AsyncMock() |
| 115 | + mock_client_instance.post.return_value = mock_response |
| 116 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 117 | + |
| 118 | + # The activity should raise _CompleteAsyncError when it calls activity.raise_complete_async() |
| 119 | + # This is expected behavior - the activity registers the callback then signals async completion |
| 120 | + with pytest.raises(_CompleteAsyncError): |
| 121 | + await activity_env.run(wait_for_decision_activity, "test-expense-123") |
| 122 | + |
| 123 | + # Verify callback registration call was made |
| 124 | + mock_client_instance.post.assert_called_once() |
| 125 | + call_args = mock_client_instance.post.call_args |
| 126 | + assert f"{EXPENSE_SERVER_HOST_PORT}/registerCallback" in call_args[0][0] |
| 127 | + |
| 128 | + # Verify task token in form data |
| 129 | + assert "task_token" in call_args[1]["data"] |
| 130 | + |
| 131 | + async def test_wait_for_decision_activity_callback_registration_failure( |
| 132 | + self, activity_env |
| 133 | + ): |
| 134 | + """Test callback registration failure""" |
| 135 | + with patch("httpx.AsyncClient") as mock_client: |
| 136 | + # Mock failed callback registration |
| 137 | + mock_response = AsyncMock() |
| 138 | + mock_response.text = "ERROR:INVALID_ID" |
| 139 | + mock_response.raise_for_status = AsyncMock() |
| 140 | + |
| 141 | + mock_client_instance = AsyncMock() |
| 142 | + mock_client_instance.post.return_value = mock_response |
| 143 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 144 | + |
| 145 | + with pytest.raises( |
| 146 | + Exception, match="register callback failed status: ERROR:INVALID_ID" |
| 147 | + ): |
| 148 | + await activity_env.run(wait_for_decision_activity, "test-expense-123") |
| 149 | + |
| 150 | + |
| 151 | +class TestPaymentActivity: |
| 152 | + """Test payment_activity individual behavior""" |
| 153 | + |
| 154 | + @pytest.fixture |
| 155 | + def activity_env(self): |
| 156 | + return ActivityEnvironment() |
| 157 | + |
| 158 | + async def test_payment_activity_success(self, activity_env): |
| 159 | + """Test successful payment processing""" |
| 160 | + with patch("httpx.AsyncClient") as mock_client: |
| 161 | + # Mock successful payment response |
| 162 | + mock_response = AsyncMock() |
| 163 | + mock_response.text = "SUCCEED" |
| 164 | + mock_response.raise_for_status = AsyncMock() |
| 165 | + |
| 166 | + mock_client_instance = AsyncMock() |
| 167 | + mock_client_instance.get.return_value = mock_response |
| 168 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 169 | + |
| 170 | + # Execute activity |
| 171 | + result = await activity_env.run(payment_activity, "test-expense-123") |
| 172 | + |
| 173 | + # Verify HTTP call |
| 174 | + mock_client_instance.get.assert_called_once_with( |
| 175 | + f"{EXPENSE_SERVER_HOST_PORT}/action", |
| 176 | + params={ |
| 177 | + "is_api_call": "true", |
| 178 | + "type": "payment", |
| 179 | + "id": "test-expense-123", |
| 180 | + }, |
| 181 | + ) |
| 182 | + |
| 183 | + # Activity should return None on success |
| 184 | + assert result is None |
| 185 | + |
| 186 | + async def test_payment_activity_empty_id(self, activity_env): |
| 187 | + """Test payment activity with empty expense ID""" |
| 188 | + with pytest.raises(ValueError, match="expense id is empty"): |
| 189 | + await activity_env.run(payment_activity, "") |
| 190 | + |
| 191 | + async def test_payment_activity_payment_failure(self, activity_env): |
| 192 | + """Test payment activity with payment failure""" |
| 193 | + with patch("httpx.AsyncClient") as mock_client: |
| 194 | + # Mock payment failure response |
| 195 | + mock_response = AsyncMock() |
| 196 | + mock_response.text = "ERROR:INSUFFICIENT_FUNDS" |
| 197 | + mock_response.raise_for_status = AsyncMock() |
| 198 | + |
| 199 | + mock_client_instance = AsyncMock() |
| 200 | + mock_client_instance.get.return_value = mock_response |
| 201 | + mock_client.return_value.__aenter__.return_value = mock_client_instance |
| 202 | + |
| 203 | + with pytest.raises(Exception, match="ERROR:INSUFFICIENT_FUNDS"): |
| 204 | + await activity_env.run(payment_activity, "test-expense-123") |
0 commit comments