fix: ruff checks is now in order:)

ref: N25B-205
This commit is contained in:
Björn Otgaar
2025-10-30 16:41:35 +01:00
parent af3e4ae56a
commit 30453be4b2
10 changed files with 117 additions and 84 deletions

View File

@@ -1,10 +1,10 @@
import asyncio
import zmq
import json
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import zmq
from control_backend.agents.ri_command_agent import RICommandAgent
from control_backend.schemas.ri_message import SpeechCommand
@pytest.mark.asyncio

View File

@@ -1,6 +1,8 @@
import asyncio
from unittest.mock import ANY, AsyncMock, MagicMock, patch
import pytest
from unittest.mock import AsyncMock, MagicMock, patch, ANY
from control_backend.agents.ri_communication_agent import RICommunicationAgent
@@ -109,7 +111,11 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup()
@@ -153,7 +159,11 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup()
@@ -189,8 +199,8 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
# Mock RICommandAgent agent startup
# We are sending wrong negotiation info to the communication agent, so we should retry and expect a
# better response, within a limited time.
# We are sending wrong negotiation info to the communication agent,
# so we should retry and expect a better response, within a limited time.
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
@@ -200,7 +210,11 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
# --- Act ---
with caplog.at_level("ERROR"):
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup(max_retries=1)
@@ -240,7 +254,11 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=True
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=True,
)
await agent.setup()
@@ -283,7 +301,11 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup()
@@ -326,7 +348,11 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup()
@@ -362,8 +388,8 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
# Mock RICommandAgent agent startup
# We are sending wrong negotiation info to the communication agent, so we should retry and expect a
# better response, within a limited time.
# We are sending wrong negotiation info to the communication agent,
# so we should retry and expect a etter response, within a limited time.
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
@@ -374,7 +400,11 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup(max_retries=1)
@@ -408,11 +438,15 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
await agent.setup(max_retries=1)
@@ -544,13 +578,16 @@ async def test_setup_unexpected_exception(monkeypatch, caplog):
# Simulate unexpected exception during recv_json()
fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!"))
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
)
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
with caplog.at_level("ERROR"):
@@ -587,7 +624,11 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
fake_pub_socket = AsyncMock()
agent = RICommunicationAgent(
"test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
# --- Act & Assert ---

View File

@@ -1,7 +1,8 @@
from unittest.mock import MagicMock
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from unittest.mock import MagicMock
from control_backend.api.v1.endpoints import robot
from control_backend.schemas.ri_message import SpeechCommand

View File

@@ -1,7 +1,8 @@
import pytest
from control_backend.schemas.ri_message import RIMessage, RIEndpoint, SpeechCommand
from pydantic import ValidationError
from control_backend.schemas.ri_message import RIEndpoint, RIMessage, SpeechCommand
def valid_command_1():
return SpeechCommand(data="Hallo?")
@@ -13,24 +14,14 @@ def invalid_command_1():
def test_valid_speech_command_1():
command = valid_command_1()
try:
RIMessage.model_validate(command)
SpeechCommand.model_validate(command)
assert True
except ValidationError:
assert False
RIMessage.model_validate(command)
SpeechCommand.model_validate(command)
assert True
def test_invalid_speech_command_1():
command = invalid_command_1()
passed_ri_message_validation = False
try:
# Should succeed, still.
RIMessage.model_validate(command)
passed_ri_message_validation = True
# Should fail.
RIMessage.model_validate(command)
with pytest.raises(ValidationError):
SpeechCommand.model_validate(command)
assert False
except ValidationError:
assert passed_ri_message_validation
assert True