11"""Tests for status_updates module."""
22
3- import asyncio
43from unittest .mock import AsyncMock , MagicMock , patch
54
6- import pytest
7-
85from backend .api .status_updates import (
96 ConnectionManager ,
107 app_connection_manager ,
11- send_status_update_async ,
12- send_status_update ,
138 close_connection ,
9+ send_status_update ,
10+ send_status_update_async ,
1411)
15- from backend .common .models .api import FileProcessUpdate , ProcessStatus , FileResult
12+
13+ import pytest
1614
1715
1816class TestConnectionManager :
@@ -21,15 +19,15 @@ class TestConnectionManager:
2119 def test_init (self ):
2220 """Test ConnectionManager initialization."""
2321 manager = ConnectionManager ()
24- assert manager .connections == {}
22+ assert not manager .connections
2523
2624 def test_add_connection (self ):
2725 """Test adding a connection."""
2826 manager = ConnectionManager ()
2927 mock_websocket = MagicMock ()
30-
28+
3129 manager .add_connection ("batch-1" , mock_websocket )
32-
30+
3331 assert "batch-1" in manager .connections
3432 assert manager .connections ["batch-1" ] == mock_websocket
3533
@@ -38,15 +36,15 @@ def test_remove_connection(self):
3836 manager = ConnectionManager ()
3937 mock_websocket = MagicMock ()
4038 manager .add_connection ("batch-1" , mock_websocket )
41-
39+
4240 manager .remove_connection ("batch-1" )
43-
41+
4442 assert "batch-1" not in manager .connections
4543
4644 def test_remove_nonexistent_connection (self ):
4745 """Test removing a connection that doesn't exist."""
4846 manager = ConnectionManager ()
49-
47+
5048 # Should not raise an error
5149 manager .remove_connection ("nonexistent" )
5250
@@ -55,17 +53,17 @@ def test_get_connection(self):
5553 manager = ConnectionManager ()
5654 mock_websocket = MagicMock ()
5755 manager .add_connection ("batch-1" , mock_websocket )
58-
56+
5957 result = manager .get_connection ("batch-1" )
60-
58+
6159 assert result == mock_websocket
6260
6361 def test_get_nonexistent_connection (self ):
6462 """Test getting a connection that doesn't exist."""
6563 manager = ConnectionManager ()
66-
64+
6765 result = manager .get_connection ("nonexistent" )
68-
66+
6967 assert result is None
7068
7169
@@ -76,22 +74,22 @@ class TestSendStatusUpdateAsync:
7674 async def test_send_status_update_async_with_connection (self ):
7775 """Test sending status update when connection exists."""
7876 mock_websocket = AsyncMock ()
79-
77+
8078 status = MagicMock ()
8179 status .batch_id = "batch-1"
82-
80+
8381 with patch .object (app_connection_manager , 'get_connection' , return_value = mock_websocket ):
8482 with patch ("json.dumps" , return_value = '{"batch_id": "batch-1"}' ):
8583 await send_status_update_async (status )
86-
84+
8785 mock_websocket .send_text .assert_called_once ()
8886
8987 @pytest .mark .asyncio
9088 async def test_send_status_update_async_no_connection (self ):
9189 """Test sending status update when no connection exists."""
9290 status = MagicMock ()
9391 status .batch_id = "batch-1"
94-
92+
9593 with patch .object (app_connection_manager , 'get_connection' , return_value = None ):
9694 # Should not raise an error
9795 await send_status_update_async (status )
@@ -104,34 +102,34 @@ def test_send_status_update_with_connection(self):
104102 """Test sending status update synchronously when connection exists."""
105103 mock_websocket = MagicMock ()
106104 mock_loop = MagicMock ()
107-
105+
108106 status = MagicMock ()
109107 status .batch_id = "batch-1"
110-
108+
111109 with patch .object (app_connection_manager , 'get_connection' , return_value = mock_websocket ):
112110 with patch ('backend.api.status_updates.asyncio.get_event_loop' , return_value = mock_loop ):
113111 with patch ('backend.api.status_updates.asyncio.run_coroutine_threadsafe' ) as mock_run :
114112 with patch ('backend.api.status_updates.json.dumps' , return_value = '{"batch_id": "batch-1"}' ):
115113 send_status_update (status )
116-
114+
117115 mock_run .assert_called_once ()
118116
119117 def test_send_status_update_no_connection (self ):
120118 """Test sending status update when no connection exists."""
121119 status = MagicMock ()
122120 status .batch_id = "batch-1"
123-
121+
124122 with patch .object (app_connection_manager , 'get_connection' , return_value = None ):
125123 # Should not raise an error
126124 send_status_update (status )
127125
128126 def test_send_status_update_exception (self ):
129127 """Test sending status update when exception occurs."""
130128 mock_websocket = MagicMock ()
131-
129+
132130 status = MagicMock ()
133131 status .batch_id = "batch-1"
134-
132+
135133 with patch .object (app_connection_manager , 'get_connection' , return_value = mock_websocket ):
136134 with patch ('backend.api.status_updates.asyncio.get_event_loop' , side_effect = RuntimeError ("No event loop" )):
137135 # Should handle exception gracefully
@@ -146,13 +144,13 @@ async def test_close_connection_with_connection(self):
146144 """Test closing a connection that exists."""
147145 mock_websocket = MagicMock ()
148146 mock_loop = MagicMock ()
149-
147+
150148 with patch .object (app_connection_manager , 'get_connection' , return_value = mock_websocket ):
151149 with patch .object (app_connection_manager , 'remove_connection' ) as mock_remove :
152150 with patch ('asyncio.get_event_loop' , return_value = mock_loop ):
153151 with patch ('asyncio.run_coroutine_threadsafe' ):
154152 await close_connection ("batch-1" )
155-
153+
156154 mock_remove .assert_called_once_with ("batch-1" )
157155
158156 @pytest .mark .asyncio
@@ -161,6 +159,6 @@ async def test_close_connection_no_connection(self):
161159 with patch .object (app_connection_manager , 'get_connection' , return_value = None ):
162160 with patch .object (app_connection_manager , 'remove_connection' ) as mock_remove :
163161 await close_connection ("batch-1" )
164-
162+
165163 # Should still call remove_connection
166164 mock_remove .assert_called_once_with ("batch-1" )
0 commit comments