test: convert to pytest

Instead of built-in `unittest`, now use `pytest`. Find versions that work, convert tests.

ref: N25B-168
This commit is contained in:
Twirre Meulenbelt
2025-10-21 13:55:06 +02:00
parent 45be0366ba
commit 5631a55697
5 changed files with 166 additions and 158 deletions

View File

@@ -1,79 +1,79 @@
import unittest
import mock
import pytest
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)
@pytest.fixture
def zmq_context():
context = zmq.Context()
yield context
if __name__ == "__main__":
unittest.main()
def test_handle_ping(zmq_context):
receiver = MainReceiver(zmq_context)
response = receiver.handle_message({"endpoint": "ping", "data": "pong"})
assert "endpoint" in response
assert response["endpoint"] == "ping"
assert "data" in response
assert response["data"] == "pong"
def test_handle_ping_none(zmq_context):
receiver = MainReceiver(zmq_context)
response = receiver.handle_message({"endpoint": "ping", "data": None})
assert "endpoint" in response
assert response["endpoint"] == "ping"
assert "data" in response
assert response["data"] == None
@mock.patch("robot_interface.endpoints.main_receiver.state")
def test_handle_negotiate_ports(mock_state, zmq_context):
receiver = MainReceiver(zmq_context)
mock_state.sockets = [receiver]
response = receiver.handle_message({"endpoint": "negotiate/ports", "data": None})
assert "endpoint" in response
assert response["endpoint"] == "negotiate/ports"
assert "data" in response
assert isinstance(response["data"], list)
for port in response["data"]:
assert "id" in port
assert isinstance(port["id"], str)
assert "port" in port
assert isinstance(port["port"], int)
assert "bind" in port
assert isinstance(port["bind"], bool)
assert any(port["id"] == "main" for port in response["data"])
def test_handle_unimplemented_endpoint(zmq_context):
receiver = MainReceiver(zmq_context)
response = receiver.handle_message({
"endpoint": "some_endpoint_that_definitely_does_not_exist",
"data": None,
})
assert "endpoint" in response
assert response["endpoint"] == "error"
assert "data" in response
assert isinstance(response["data"], str)
def test_handle_unimplemented_negotiation_endpoint(zmq_context):
receiver = MainReceiver(zmq_context)
response = receiver.handle_message({
"endpoint": "negotiate/but_some_subpath_that_definitely_does_not_exist",
"data": None,
})
assert "endpoint" in response
assert response["endpoint"] == "negotiate/error"
assert "data" in response
assert isinstance(response["data"], str)