Files
pepperplus-ri/test/unit/test_main_receiver.py
Twirre Meulenbelt 56c804b7eb test: add unit tests for main and actuation receivers
Exhaustive test cases for both classes, with 100% coverage. Adds `mock` dependency. Tests for actuation receiver do not yet pass.

ref: N25B-168
2025-10-16 21:43:24 +02:00

80 lines
2.7 KiB
Python

import unittest
import mock
import zmq
from robot_interface.endpoints.main_receiver import MainReceiver
class TestMainReceiver(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.context = zmq.Context()
def test_handle_ping(self):
receiver = MainReceiver(self.context)
response = receiver.handle_message({"endpoint": "ping", "data": "pong"})
self.assertIn("endpoint", response)
self.assertEqual(response["endpoint"], "ping")
self.assertIn("data", response)
self.assertEqual(response["data"], "pong")
def test_handle_ping_none(self):
receiver = MainReceiver(self.context)
response = receiver.handle_message({"endpoint": "ping", "data": None})
self.assertIn("endpoint", response)
self.assertEqual(response["endpoint"], "ping")
self.assertIn("data", response)
self.assertEqual(response["data"], None)
@mock.patch("robot_interface.endpoints.main_receiver.state")
def test_handle_negotiate_ports(self, mock_state):
receiver = MainReceiver(self.context)
mock_state.sockets = [receiver]
response = receiver.handle_message({"endpoint": "negotiate/ports", "data": None})
self.assertIn("endpoint", response)
self.assertEqual(response["endpoint"], "negotiate/ports")
self.assertIn("data", response)
self.assertIsInstance(response["data"], list)
for port in response["data"]:
self.assertIn("id", port)
self.assertIsInstance(port["id"], str)
self.assertIn("port", port)
self.assertIsInstance(port["port"], int)
self.assertIn("bind", port)
self.assertIsInstance(port["bind"], bool)
self.assertTrue(any(port["id"] == "main" for port in response["data"]))
def test_handle_unimplemented_endpoint(self):
receiver = MainReceiver(self.context)
response = receiver.handle_message({
"endpoint": "some_endpoint_that_definitely_does_not_exist",
"data": None,
})
self.assertIn("endpoint", response)
self.assertEqual(response["endpoint"], "error")
self.assertIn("data", response)
self.assertIsInstance(response["data"], str)
def test_handle_unimplemented_negotiation_endpoint(self):
receiver = MainReceiver(self.context)
response = receiver.handle_message({
"endpoint": "negotiate/but_some_subpath_that_definitely_does_not_exist",
"data": None,
})
self.assertIn("endpoint", response)
self.assertEqual(response["endpoint"], "negotiate/error")
self.assertIn("data", response)
self.assertIsInstance(response["data"], str)
if __name__ == "__main__":
unittest.main()