Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: add more websocket tests #18

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from tests import TEST_ROOMS, TEST_RULES


def test_rooms() -> Generator[list[str], None, None]:
yield from TEST_ROOMS
def test_rooms() -> list[str]:
return TEST_ROOMS


@pytest.fixture
Expand Down
21 changes: 21 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import pytest
from fastapi.testclient import TestClient

Expand All @@ -23,3 +25,22 @@ def test_websocket_send_message(client: TestClient, room: str):
websocket.send_json({"message": TEST_MESSAGE, "author": f"ws-tester-{room}"})
data = websocket.receive_json()
assert data == {"message": TEST_MESSAGE, "author": f"ws-tester-{room}"}


def nb_connected_users(json: dict[str, Any], room: str) -> bool:
"""
Utils function to get number of connected users in a room from a status dict
"""
rooms_dict = {r["name"]: r["nb_connected_users"] for r in json["rooms"]}
return rooms_dict[room]


@pytest.mark.parametrize("room", test_rooms())
def test_websocket_nb_users_connected(client: TestClient, room: str):
assert nb_connected_users(client.get("/status").json(), room) == 0
with client.websocket_connect(f"/room/{room}/ws"):
assert nb_connected_users(client.get("/status").json(), room) == 1
with client.websocket_connect(f"/room/{room}/ws"):
assert nb_connected_users(client.get("/status").json(), room) == 2
assert nb_connected_users(client.get("/status").json(), room) == 1
assert nb_connected_users(client.get("/status").json(), room) == 0