Merge remote-tracking branch 'origin/dev' into refactor/zmq-internal-socket-behaviour
# Conflicts: # src/control_backend/agents/ri_command_agent.py # src/control_backend/agents/ri_communication_agent.py # src/control_backend/api/v1/endpoints/command.py # src/control_backend/main.py # test/integration/api/endpoints/test_command_endpoint.py
This commit is contained in:
@@ -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.fixture
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -177,8 +179,8 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, 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:
|
||||
@@ -330,8 +332,8 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, 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:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
@@ -16,6 +16,7 @@ def app():
|
||||
"""
|
||||
app = FastAPI()
|
||||
app.include_router(command.router)
|
||||
app.state.internal_comm_socket = MagicMock() # mock ZMQ socket
|
||||
return app
|
||||
|
||||
|
||||
@@ -25,32 +26,32 @@ def client(app):
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@patch("control_backend.api.v1.endpoints.command.Context.instance")
|
||||
async def test_receive_command_success(mock_context_instance, client):
|
||||
def test_receive_command_endpoint(client, app):
|
||||
"""
|
||||
Test for successful reception of a command.
|
||||
Ensures the status code is 202 and the response body is correct.
|
||||
It also verifies that the ZeroMQ socket's send_multipart method is called with the expected data.
|
||||
Test that a POST to /command sends the right multipart message
|
||||
and returns a 202 with the expected JSON body.
|
||||
"""
|
||||
# Arrange
|
||||
mock_pub_socket = AsyncMock()
|
||||
client.app.state.endpoints_pub_socket = mock_pub_socket
|
||||
mock_socket = app.state.internal_comm_socket
|
||||
|
||||
command_data = {"endpoint": "actuate/speech", "data": "This is a test"}
|
||||
speech_command = SpeechCommand(**command_data)
|
||||
# Prepare test payload that matches SpeechCommand
|
||||
payload = {"endpoint": "actuate/speech", "data": "yooo"}
|
||||
|
||||
# Act
|
||||
response = client.post("/command", json=command_data)
|
||||
# Send POST request
|
||||
response = client.post("/command", json=payload)
|
||||
|
||||
# Assert
|
||||
# Check response
|
||||
assert response.status_code == 202
|
||||
assert response.json() == {"status": "Command received"}
|
||||
|
||||
# Verify that the ZMQ socket was used correctly
|
||||
mock_pub_socket.send_multipart.assert_awaited_once_with(
|
||||
[b"command", speech_command.model_dump_json().encode()]
|
||||
)
|
||||
# Verify that the socket was called with the correct data
|
||||
assert mock_socket.send_multipart.called, "Socket should be used to send data"
|
||||
|
||||
args, kwargs = mock_socket.send_multipart.call_args
|
||||
sent_data = args[0]
|
||||
|
||||
assert sent_data[0] == b"command"
|
||||
# Check JSON encoding roughly matches
|
||||
assert isinstance(SpeechCommand.model_validate_json(sent_data[1].decode()), SpeechCommand)
|
||||
|
||||
|
||||
def test_receive_command_invalid_payload(client):
|
||||
|
||||
@@ -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,13 @@ 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)
|
||||
|
||||
|
||||
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
|
||||
RIMessage.model_validate(command)
|
||||
|
||||
# Should fail.
|
||||
with pytest.raises(ValidationError):
|
||||
SpeechCommand.model_validate(command)
|
||||
assert False
|
||||
except ValidationError:
|
||||
assert passed_ri_message_validation
|
||||
|
||||
Reference in New Issue
Block a user