1
+ from unittest .mock import patch
2
+
1
3
import pytest
2
4
from fastapi .testclient import TestClient
3
5
@@ -15,41 +17,47 @@ def connection_manager():
15
17
16
18
17
19
def test_websocket_connection (test_client ):
18
- with test_client .websocket_connect ("/ws/chat/1" ) as websocket :
19
- websocket .send_text ("Hello!" )
20
- data = websocket .receive_text ()
20
+ manager = ConnectionManager ()
21
+ with patch ("http_app.routes.ws.chat.manager" , manager ):
22
+ with test_client .websocket_connect ("/ws/chat/1" ) as websocket :
23
+ websocket .send_text ("Hello!" )
24
+ data = websocket .receive_text ()
21
25
22
- assert data == "You wrote: Hello!"
23
- broadcast = websocket .receive_text ()
24
- assert broadcast == "Client #1 says: Hello!"
26
+ assert data == "You wrote: Hello!"
27
+ broadcast = websocket .receive_text ()
28
+ assert broadcast == "Client #1 says: Hello!"
25
29
26
30
27
31
def test_multiple_clients (test_client ):
28
- with test_client .websocket_connect ("/ws/chat/1" ) as websocket1 :
29
- with test_client .websocket_connect ("/ws/chat/2" ) as websocket2 :
30
- # Client 1 sends message
31
- websocket1 .send_text ("Hello from client 1" )
32
+ manager = ConnectionManager ()
33
+ with patch ("http_app.routes.ws.chat.manager" , manager ):
34
+ with test_client .websocket_connect ("/ws/chat/1" ) as websocket1 :
35
+ with test_client .websocket_connect ("/ws/chat/2" ) as websocket2 :
36
+ # Client 1 sends message
37
+ websocket1 .send_text ("Hello from client 1" )
32
38
33
- # Client 1 receives personal message
34
- data1 = websocket1 .receive_text ()
35
- assert data1 == "You wrote: Hello from client 1"
39
+ # Client 1 receives personal message
40
+ data1 = websocket1 .receive_text ()
41
+ assert data1 == "You wrote: Hello from client 1"
36
42
37
- # Both clients receive broadcast
38
- broadcast1 = websocket1 .receive_text ()
39
- broadcast2 = websocket2 .receive_text ()
40
- assert broadcast1 == "Client #1 says: Hello from client 1"
41
- assert broadcast2 == "Client #1 says: Hello from client 1"
43
+ # Both clients receive broadcast
44
+ broadcast1 = websocket1 .receive_text ()
45
+ broadcast2 = websocket2 .receive_text ()
46
+ assert broadcast1 == "Client #1 says: Hello from client 1"
47
+ assert broadcast2 == "Client #1 says: Hello from client 1"
42
48
43
49
44
50
def test_client_disconnect (test_client ):
45
- with test_client .websocket_connect ("/ws/chat/1" ) as websocket1 :
46
- with test_client .websocket_connect ("/ws/chat/2" ) as websocket2 :
47
- # Close first client
48
- websocket1 .close ()
49
-
50
- # Second client should receive disconnect message
51
- disconnect_message = websocket2 .receive_text ()
52
- assert disconnect_message == "Client #1 left the chat"
51
+ manager = ConnectionManager ()
52
+ with patch ("http_app.routes.ws.chat.manager" , manager ):
53
+ with test_client .websocket_connect ("/ws/chat/1" ) as websocket1 :
54
+ with test_client .websocket_connect ("/ws/chat/2" ) as websocket2 :
55
+ # Close first client
56
+ websocket1 .close ()
57
+
58
+ # Second client should receive disconnect message
59
+ disconnect_message = websocket2 .receive_text ()
60
+ assert disconnect_message == "Client #1 left the chat"
53
61
54
62
55
63
async def test_connection_manager ():
0 commit comments