-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
32 lines (25 loc) · 958 Bytes
/
test_main.py
File metadata and controls
32 lines (25 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pytest
from fastapi.testclient import TestClient
from main import app
import uuid
client = TestClient(app)
@pytest.fixture
def websocket_client():
client_id = str(uuid.uuid4())
with client.websocket_connect(f"/ws/{client_id}") as websocket:
yield websocket
@pytest.mark.asyncio
async def test_websocket_connection():
client_id = str(uuid.uuid4())
# Use regular websocket test client instead of async context
with client.websocket_connect(f"/ws/{client_id}") as websocket:
data = {"type": "test", "message": "Hello"}
websocket.send_json(data)
response = websocket.receive_json()
assert response["type"] == "test_response"
assert response["message"] == "Test received"
def test_websocket_disconnect():
client_id = str(uuid.uuid4())
with client.websocket_connect(f"/ws/{client_id}") as websocket:
websocket.close()
# Test passes if no exception is raised