Skip to content

Fix deprecations #249

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

Merged
merged 8 commits into from
Feb 6, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 5 additions & 1 deletion src/http_app/routes/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
19 changes: 12 additions & 7 deletions tests/common/test_asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def reset_asyncapi_state():


# Test message models
class TestMessage(BaseModel):
class SomeTestMessage(BaseModel):
content: str
timestamp: int

Expand Down Expand Up @@ -103,31 +103,36 @@ 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):
"""Test registration of multiple messages for an operation"""
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
2 changes: 0 additions & 2 deletions tests/common/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
59 changes: 33 additions & 26 deletions tests/http_app/routes/ws/test_chat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

Expand All @@ -15,44 +17,49 @@ 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()
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"
# Second client should receive disconnect message
disconnect_message = websocket2.receive_text()
assert disconnect_message == "Client #1 left the chat"


@pytest.mark.asyncio
async def test_connection_manager():
manager = ConnectionManager()

Expand Down