From 08b7deba9c9debe7a904016d8f3a69c754c0fba8 Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:52:35 +0000 Subject: [PATCH 1/8] Address TemplateResponse deprecation --- src/http_app/routes/hello.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/http_app/routes/hello.py b/src/http_app/routes/hello.py index 678b92e..975b4ed 100644 --- a/src/http_app/routes/hello.py +++ b/src/http_app/routes/hello.py @@ -10,4 +10,8 @@ @router.get("/", response_class=HTMLResponse, include_in_schema=True) async def hello(request: Request, jwt_token=Security(decode_jwt)): - return templates.TemplateResponse("hello.html", {"request": request, "token_payload": jwt_token}) + return templates.TemplateResponse( + name="hello.html", + request=request, + context={"token_payload": jwt_token}, + ) From 19d306842e87cb17c744236999c0e04b66aeafc5 Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:53:53 +0000 Subject: [PATCH 2/8] Remove unneeded asyncio marks --- tests/common/test_tracing.py | 2 -- tests/http_app/routes/ws/test_chat.py | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/common/test_tracing.py b/tests/common/test_tracing.py index 91f67d7..4009c81 100644 --- a/tests/common/test_tracing.py +++ b/tests/common/test_tracing.py @@ -43,7 +43,6 @@ def add_nums(a, b): mock_span.set_attribute.assert_any_call("function.result", "5") -@pytest.mark.asyncio async def test_async_function_default_params(mock_tracer): """ Test an asynchronous function with default decorator parameters. @@ -129,7 +128,6 @@ def sync_func(a, b): assert call("function.result") not in mock_span.set_attribute.call_args_list -@pytest.mark.asyncio async def test_disable_result_in_span(mock_tracer): """ Test an asynchronous function with `add_result_to_span` set to False. diff --git a/tests/http_app/routes/ws/test_chat.py b/tests/http_app/routes/ws/test_chat.py index 209a31e..88dc158 100644 --- a/tests/http_app/routes/ws/test_chat.py +++ b/tests/http_app/routes/ws/test_chat.py @@ -52,7 +52,6 @@ def test_client_disconnect(test_client): assert disconnect_message == "Client #1 left the chat" -@pytest.mark.asyncio async def test_connection_manager(): manager = ConnectionManager() From ef92807278451d0c13162c76e30847df48b9b76f Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:55:46 +0000 Subject: [PATCH 3/8] Rename class to not appear as unittest class --- tests/common/test_asyncapi.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/common/test_asyncapi.py b/tests/common/test_asyncapi.py index fdb7bc9..da0697e 100644 --- a/tests/common/test_asyncapi.py +++ b/tests/common/test_asyncapi.py @@ -28,7 +28,7 @@ def reset_asyncapi_state(): # Test message models -class TestMessage(BaseModel): +class SomeTestMessage(BaseModel): content: str timestamp: int @@ -103,20 +103,20 @@ def test_register_channel_operation(reset_asyncapi_state): register_channel(address="test/topic", id=channel_id) register_channel_operation( - channel_id=channel_id, operation_type=operation_type, messages=[TestMessage], operation_name="test-operation" + channel_id=channel_id, operation_type=operation_type, messages=[SomeTestMessage], operation_name="test-operation" ) schema = get_schema() assert "test-operation" in schema.operations assert schema.operations["test-operation"].action == operation_type assert schema.operations["test-operation"].channel.ref == f"#/channels/{channel_id}" - assert TestMessage.__name__ in schema.components.schemas + assert SomeTestMessage.__name__ in schema.components.schemas def test_register_channel_operation_invalid_channel(reset_asyncapi_state): """Test channel operation registration with invalid channel""" with pytest.raises(ValueError, match="Channel non-existent does not exist"): - register_channel_operation(channel_id="non-existent", operation_type="receive", messages=[TestMessage]) + register_channel_operation(channel_id="non-existent", operation_type="receive", messages=[SomeTestMessage]) def test_multiple_messages_registration(reset_asyncapi_state): @@ -124,10 +124,10 @@ def test_multiple_messages_registration(reset_asyncapi_state): channel_id = "test-channel" register_channel(address="test/topic", id=channel_id) - register_channel_operation(channel_id=channel_id, operation_type="send", messages=[TestMessage, AnotherTestMessage]) + register_channel_operation(channel_id=channel_id, operation_type="send", messages=[SomeTestMessage, AnotherTestMessage]) schema = get_schema() - assert TestMessage.__name__ in schema.components.schemas + assert SomeTestMessage.__name__ in schema.components.schemas assert AnotherTestMessage.__name__ in schema.components.schemas - assert TestMessage.__name__ in schema.channels[channel_id].messages + assert SomeTestMessage.__name__ in schema.channels[channel_id].messages assert AnotherTestMessage.__name__ in schema.channels[channel_id].messages From bfa9bbb821ebf87920a6b82e453146149c5ac43d Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:03:34 +0000 Subject: [PATCH 4/8] Try to patch the connection manager so tests don't conflict with each other --- tests/http_app/routes/ws/test_chat.py | 60 +++++++++++++++------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/tests/http_app/routes/ws/test_chat.py b/tests/http_app/routes/ws/test_chat.py index 88dc158..fc3bb76 100644 --- a/tests/http_app/routes/ws/test_chat.py +++ b/tests/http_app/routes/ws/test_chat.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import pytest from fastapi.testclient import TestClient @@ -15,41 +17,47 @@ def connection_manager(): def test_websocket_connection(test_client): - with test_client.websocket_connect("/ws/chat/1") as websocket: - websocket.send_text("Hello!") - data = websocket.receive_text() + manager = ConnectionManager() + with patch("http_app.routes.ws.chat.manager", manager): + with test_client.websocket_connect("/ws/chat/1") as websocket: + websocket.send_text("Hello!") + data = websocket.receive_text() - assert data == "You wrote: Hello!" - broadcast = websocket.receive_text() - assert broadcast == "Client #1 says: Hello!" + assert data == "You wrote: Hello!" + broadcast = websocket.receive_text() + assert broadcast == "Client #1 says: Hello!" def test_multiple_clients(test_client): - with test_client.websocket_connect("/ws/chat/1") as websocket1: - with test_client.websocket_connect("/ws/chat/2") as websocket2: - # Client 1 sends message - websocket1.send_text("Hello from client 1") + manager = ConnectionManager() + with patch("http_app.routes.ws.chat.manager", manager): + with test_client.websocket_connect("/ws/chat/1") as websocket1: + with test_client.websocket_connect("/ws/chat/2") as websocket2: + # Client 1 sends message + websocket1.send_text("Hello from client 1") - # Client 1 receives personal message - data1 = websocket1.receive_text() - assert data1 == "You wrote: Hello from client 1" + # Client 1 receives personal message + data1 = websocket1.receive_text() + assert data1 == "You wrote: Hello from client 1" - # Both clients receive broadcast - broadcast1 = websocket1.receive_text() - broadcast2 = websocket2.receive_text() - assert broadcast1 == "Client #1 says: Hello from client 1" - assert broadcast2 == "Client #1 says: Hello from client 1" + # Both clients receive broadcast + broadcast1 = websocket1.receive_text() + broadcast2 = websocket2.receive_text() + assert broadcast1 == "Client #1 says: Hello from client 1" + assert broadcast2 == "Client #1 says: Hello from client 1" def test_client_disconnect(test_client): - with test_client.websocket_connect("/ws/chat/1") as websocket1: - with test_client.websocket_connect("/ws/chat/2") as websocket2: - # Close first client - websocket1.close() - - # Second client should receive disconnect message - disconnect_message = websocket2.receive_text() - assert disconnect_message == "Client #1 left the chat" + manager = ConnectionManager() + with patch("http_app.routes.ws.chat.manager", manager): + with test_client.websocket_connect("/ws/chat/1") as websocket1: + with test_client.websocket_connect("/ws/chat/2") as websocket2: + # Close first client + websocket1.close() + + # Second client should receive disconnect message + disconnect_message = websocket2.receive_text() + assert disconnect_message == "Client #1 left the chat" async def test_connection_manager(): From 1792bf5e22ece9d25c183a656898bd0cedff95e7 Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:56:56 +0000 Subject: [PATCH 5/8] Restore xdist --- Makefile | 4 ++-- pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 95f185a..8d5abab 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,10 @@ test: uv run pytest -n auto --cov ci-test: - uv run pytest -n 0 + uv run pytest ci-coverage: - uv run pytest -n 0 --cov --cov-report lcov + uv run pytest --cov --cov-report lcov typing: uv run mypy diff --git a/pyproject.toml b/pyproject.toml index 66d7c51..3b7d935 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -109,7 +109,7 @@ ignore_missing_imports = true [tool.pytest.ini_options] minversion = "6.0" -addopts = "-n auto --cov-report=term-missing" +addopts = "-n auto --dist loadscope --cov-report=term-missing" testpaths = [ "tests", ] From a3fe10da449589760627a9d29ebd210418cbea2b Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:58:49 +0000 Subject: [PATCH 6/8] CS --- tests/common/test_asyncapi.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/common/test_asyncapi.py b/tests/common/test_asyncapi.py index da0697e..90048f9 100644 --- a/tests/common/test_asyncapi.py +++ b/tests/common/test_asyncapi.py @@ -103,7 +103,10 @@ def test_register_channel_operation(reset_asyncapi_state): register_channel(address="test/topic", id=channel_id) register_channel_operation( - channel_id=channel_id, operation_type=operation_type, messages=[SomeTestMessage], operation_name="test-operation" + channel_id=channel_id, + operation_type=operation_type, + messages=[SomeTestMessage], + operation_name="test-operation", ) schema = get_schema() @@ -124,7 +127,9 @@ def test_multiple_messages_registration(reset_asyncapi_state): channel_id = "test-channel" register_channel(address="test/topic", id=channel_id) - register_channel_operation(channel_id=channel_id, operation_type="send", messages=[SomeTestMessage, AnotherTestMessage]) + register_channel_operation( + channel_id=channel_id, operation_type="send", messages=[SomeTestMessage, AnotherTestMessage] + ) schema = get_schema() assert SomeTestMessage.__name__ in schema.components.schemas From 6cb866ebce9c5364c04aa4cda04adb0dc8a65ddc Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 19:02:46 +0000 Subject: [PATCH 7/8] Remove python 3.9 leftovers --- .github/workflows/python-tests.yml | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 3553fac..221f477 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -15,7 +15,7 @@ jobs: test: strategy: matrix: - version: ["3.9", "3.10", "3.11", "3.12", "3.13"] + version: ["3.10", "3.11", "3.12", "3.13"] os: [ubuntu-latest] runs-on: ${{ matrix.os }} steps: diff --git a/pyproject.toml b/pyproject.toml index 3b7d935..fb1e8a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,7 +99,7 @@ exclude = ["migrations"] # Pydantic plugin causes some issues: https://github.com/pydantic/pydantic-settings/issues/403 #plugins = "pydantic.mypy,strawberry.ext.mypy_plugin" plugins = "strawberry.ext.mypy_plugin" -python_version = "3.9" +python_version = "3.10" [[tool.mypy.overrides]] module = [ From 844fceee0b0704e1e09a82f15094475a6fcf21d5 Mon Sep 17 00:00:00 2001 From: Federico Busetti <729029+febus982@users.noreply.github.com> Date: Thu, 6 Feb 2025 19:08:38 +0000 Subject: [PATCH 8/8] Revert "Restore xdist" This reverts commit 1792bf5e22ece9d25c183a656898bd0cedff95e7. --- Makefile | 4 ++-- pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8d5abab..95f185a 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,10 @@ test: uv run pytest -n auto --cov ci-test: - uv run pytest + uv run pytest -n 0 ci-coverage: - uv run pytest --cov --cov-report lcov + uv run pytest -n 0 --cov --cov-report lcov typing: uv run mypy diff --git a/pyproject.toml b/pyproject.toml index fb1e8a1..d41ac1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -109,7 +109,7 @@ ignore_missing_imports = true [tool.pytest.ini_options] minversion = "6.0" -addopts = "-n auto --dist loadscope --cov-report=term-missing" +addopts = "-n auto --cov-report=term-missing" testpaths = [ "tests", ]