|
| 1 | +import asyncio |
| 2 | +import uuid |
| 3 | +from unittest.mock import AsyncMock, patch |
| 4 | + |
| 5 | +from api import status_updates |
| 6 | + |
| 7 | +from common.models.api import AgentType, FileProcessUpdate, FileResult, ProcessStatus |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def file_process_update(): |
| 14 | + return FileProcessUpdate( |
| 15 | + batch_id=uuid.uuid4(), |
| 16 | + file_id=uuid.uuid4(), |
| 17 | + process_status=ProcessStatus.IN_PROGRESS, |
| 18 | + agent_type=AgentType.MIGRATOR, |
| 19 | + agent_message="Processing in progress", |
| 20 | + file_result=FileResult.INFO |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_websocket(): |
| 26 | + return AsyncMock() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_send_status_update_async_success(file_process_update): |
| 31 | + mock_websocket = AsyncMock() |
| 32 | + status_updates.app_connection_manager.add_connection(file_process_update.batch_id, mock_websocket) |
| 33 | + |
| 34 | + with patch("api.status_updates.json.dumps", return_value='{"batch_id": "test_batch", "status": "Processing", "progress": 50}'): |
| 35 | + await status_updates.send_status_update_async(file_process_update) |
| 36 | + |
| 37 | + mock_websocket.send_text.assert_awaited_once() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_send_status_update_async_no_connection(file_process_update): |
| 42 | + # No connection added |
| 43 | + with patch("api.status_updates.logger") as mock_logger: |
| 44 | + await status_updates.send_status_update_async(file_process_update) |
| 45 | + mock_logger.warning.assert_called_once_with( |
| 46 | + "No connection found for batch ID: %s", file_process_update.batch_id |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_send_status_update_success(file_process_update): |
| 51 | + mock_websocket = AsyncMock() |
| 52 | + loop = asyncio.new_event_loop() |
| 53 | + |
| 54 | + with patch("api.status_updates.asyncio.get_event_loop", return_value=loop): |
| 55 | + with patch("api.status_updates.asyncio.run_coroutine_threadsafe") as mock_run: |
| 56 | + status_updates.app_connection_manager.add_connection(str(file_process_update.batch_id), mock_websocket) |
| 57 | + |
| 58 | + with patch("api.status_updates.json.dumps", return_value='{}'): |
| 59 | + status_updates.send_status_update(file_process_update) |
| 60 | + |
| 61 | + mock_run.assert_called_once() |
| 62 | + |
| 63 | + |
| 64 | +def test_send_status_update_no_connection(file_process_update): |
| 65 | + with patch("api.status_updates.logger") as mock_logger: |
| 66 | + status_updates.send_status_update(file_process_update) |
| 67 | + |
| 68 | + mock_logger.warning.assert_called() |
| 69 | + args, kwargs = mock_logger.warning.call_args |
| 70 | + assert "No connection found for batch ID" in args[0] |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_close_connection_success(file_process_update, mock_websocket): |
| 75 | + status_updates.app_connection_manager.add_connection(file_process_update.batch_id, mock_websocket) |
| 76 | + loop = asyncio.new_event_loop() |
| 77 | + |
| 78 | + with patch("api.status_updates.asyncio.get_event_loop", return_value=loop): |
| 79 | + with patch("api.status_updates.asyncio.run_coroutine_threadsafe") as mock_run: |
| 80 | + with patch("api.status_updates.logger") as mock_logger: |
| 81 | + await status_updates.close_connection(file_process_update.batch_id) |
| 82 | + |
| 83 | + mock_run.assert_called_once() |
| 84 | + mock_logger.info.assert_any_call("Connection closed for batch ID: %s", file_process_update.batch_id) |
| 85 | + mock_logger.info.assert_any_call("Connection removed for batch ID: %s", file_process_update.batch_id) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_close_connection_no_connection(file_process_update): |
| 90 | + with patch("api.status_updates.logger") as mock_logger: |
| 91 | + await status_updates.close_connection(file_process_update.batch_id) |
| 92 | + |
| 93 | + mock_logger.warning.assert_called_once_with( |
| 94 | + "No connection found for batch ID: %s", file_process_update.batch_id |
| 95 | + ) |
| 96 | + mock_logger.info.assert_called_once_with( |
| 97 | + "Connection removed for batch ID: %s", file_process_update.batch_id |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +# Test the connection manager directly |
| 102 | +def test_connection_manager_methods(): |
| 103 | + # Get the actual connection manager instance |
| 104 | + manager = status_updates.app_connection_manager |
| 105 | + |
| 106 | + # Test the get_connection method |
| 107 | + batch_id = uuid.uuid4() |
| 108 | + assert manager.get_connection(batch_id) is None |
| 109 | + |
| 110 | + # Test add_connection method |
| 111 | + mock_websocket = AsyncMock() |
| 112 | + manager.add_connection(batch_id, mock_websocket) |
| 113 | + assert manager.get_connection(batch_id) == mock_websocket |
| 114 | + |
| 115 | + # Test overwriting an existing connection |
| 116 | + new_mock_websocket = AsyncMock() |
| 117 | + manager.add_connection(batch_id, new_mock_websocket) |
| 118 | + assert manager.get_connection(batch_id) == new_mock_websocket |
| 119 | + |
| 120 | + # Test remove_connection method |
| 121 | + manager.remove_connection(batch_id) |
| 122 | + assert manager.get_connection(batch_id) is None |
| 123 | + |
| 124 | + # Test removing a non-existent connection (should not raise an error) |
| 125 | + manager.remove_connection(uuid.uuid4()) |
| 126 | + |
| 127 | + |
| 128 | +def test_send_status_update_exception(file_process_update): |
| 129 | + mock_websocket = AsyncMock() |
| 130 | + status_updates.app_connection_manager.add_connection(str(file_process_update.batch_id), mock_websocket) |
| 131 | + |
| 132 | + with patch("api.status_updates.asyncio.get_event_loop") as mock_loop: |
| 133 | + mock_loop.return_value = asyncio.new_event_loop() |
| 134 | + with patch("api.status_updates.json.dumps", return_value='{}'): |
| 135 | + with patch("api.status_updates.asyncio.run_coroutine_threadsafe", side_effect=Exception("send error")): |
| 136 | + with patch("api.status_updates.logger") as mock_logger: |
| 137 | + status_updates.send_status_update(file_process_update) |
| 138 | + mock_logger.error.assert_called_once() |
| 139 | + assert "Failed to send message" in mock_logger.error.call_args[0][0] |
0 commit comments