Revert "fix: unit test refactoring with conftest and more mocks"

This reverts commit 423309e063.
This commit is contained in:
Björn Otgaar
2025-10-28 14:16:39 +01:00
parent 437b21a6d6
commit fd11e63b78
7 changed files with 27 additions and 142 deletions

View File

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

View File

@@ -81,45 +81,47 @@ def fake_json_invalid_id_negototiate():
}
)
def mock_command_agent():
"""Fixture to create a mock BDIAgent."""
agent = MagicMock()
agent.bdi = MagicMock()
agent.jid = "ri_command_agent@test"
return agent
@pytest.mark.asyncio
async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
"""
Test the setup of the communication agent
"""
# --- Arrange ---
fake_socket = MagicMock()
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = AsyncMock(return_value={
"endpoint": "negotiate/ports",
"data": [
{"id": "main", "port": 5555, "bind": False},
{"id": "actuation", "port": 5556, "bind": True},
],
})
fake_socket.recv_json = fake_json_correct_negototiate_1()
# Mock context.socket to return our fake socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
)
with patch("control_backend.agents.ri_communication_agent.RICommandAgent") as MockCommandAgent:
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
agent = RICommunicationAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
await agent.setup()
# --- Assert ---
fake_socket.connect.assert_any_call("tcp://localhost:5555")
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
fake_socket.recv_json.assert_awaited()
fake_agent_instance.start.assert_awaited()
MockCommandAgent.assert_called_once_with(
ANY, ANY, address="tcp://*:5556", bind=True
ANY, # Server Name
ANY, # Server Password
address="tcp://*:5556", # derived from the 'port' value in negotiation
bind=True,
)
# Ensure the agent attached a ListenBehaviour
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
@@ -139,9 +141,6 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
)
# Mock RICommandAgent agent startup
patch("control_backend.agents.ri_communication_agent.RICommandAgent", mock_command_agent)
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
@@ -589,4 +588,4 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
fake_agent_instance.start.assert_not_awaited()
# Ensure no behaviour was attached
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)

View File

@@ -1,57 +0,0 @@
import sys
from unittest.mock import MagicMock, AsyncMock
class DummyCyclicBehaviour:
async def run(self):
pass
def kill(self):
self.is_killed = True
return None
class DummyAgent:
def __init__(self, jid=None, password=None, *_, **__):
self.jid = jid
self.password = password
self.behaviours = []
async def start(self):
return AsyncMock()
def add_behaviour(self, behaviour):
behaviour.agent = self
self.behaviours.append(behaviour)
async def stop(self):
pass
def pytest_configure(config):
"""
This hook runs at the start of the pytest session, before any tests are
collected. It mocks heavy or unavailable modules to prevent ImportErrors.
"""
# --- Mock spade and spade-bdi ---
mock_spade = MagicMock()
mock_spade.agent = MagicMock(Agent=DummyAgent)
mock_spade.behaviour = MagicMock(CyclicBehaviour=DummyCyclicBehaviour)
mock_spade_bdi = MagicMock()
mock_spade_bdi.bdi = MagicMock()
mock_spade.agent.Message = MagicMock()
sys.modules["spade"] = mock_spade
sys.modules["spade.agent"] = mock_spade.agent
sys.modules["spade.behaviour"] = mock_spade.behaviour
sys.modules["spade_bdi"] = mock_spade_bdi
sys.modules["spade_bdi.bdi"] = mock_spade_bdi.bdi
# --- Mock the config module to prevent Pydantic ImportError ---
mock_config_module = MagicMock()
# The code under test does `from ... import settings`, so our mock module
# must have a `settings` attribute. We'll make it a MagicMock so we can
# configure it later in our tests using mocker.patch.
mock_config_module.settings = MagicMock()
sys.modules["control_backend.core.config"] = mock_config_module