From 1d260ee8214ab16e3d968a7a7cf6d51019e0c844 Mon Sep 17 00:00:00 2001 From: Devarsh Shroff <79686864+DevarshShroff@users.noreply.github.com> Date: Sat, 15 Mar 2025 22:58:50 -0700 Subject: [PATCH 1/3] Unit tests for Remote Control --- tests/test_remote_control.py | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/test_remote_control.py diff --git a/tests/test_remote_control.py b/tests/test_remote_control.py new file mode 100644 index 000000000..471164e19 --- /dev/null +++ b/tests/test_remote_control.py @@ -0,0 +1,79 @@ +import pytest +from unittest.mock import patch, MagicMock +import socketio +from socketio import client +import wx +from invesalius.net.remote_control import RemoteControl + +@pytest.fixture +def setup_remote_control(): + with patch("socketio.Client") as mock_socketio, patch("wx.CallAfter") as mock_callafter: + mock_sio_instance = MagicMock() + mock_socketio.return_value = mock_sio_instance + + remote_host = "http://localhost:5000" + remote_control = RemoteControl(remote_host) + yield remote_control, mock_socketio, mock_callafter + + +def test_initialization(setup_remote_control): + remote_control, _, _ = setup_remote_control + + assert remote_control._remote_host == "http://localhost:5000" + assert remote_control._connected is False + assert remote_control._sio is None + + +def test_on_connect(setup_remote_control): + remote_control, _, _ = setup_remote_control + + remote_control._on_connect() + + assert remote_control._connected is True + + +def test_on_disconnect(setup_remote_control): + remote_control,_, _ = setup_remote_control + + remote_control._on_disconnect() + + assert remote_control._connected is False + + +def test_to_neuronavigation(setup_remote_control): + remote_control, _, _ = setup_remote_control + + with patch("invesalius.pubsub.pub.sendMessage_no_hook") as mock_publisher: + msg = {"topic": "test_topic", "data": {"key": "value"}} + remote_control._to_neuronavigation(msg) + mock_publisher.assert_called_once_with(topicName="test_topic", key="value") + + +def test_to_neuronavigation_wrapper(setup_remote_control): + remote_control, _, mock_callafter = setup_remote_control + + msg = {"topic": "test_topic", "data": {"key": "value"}} + + remote_control._to_neuronavigation_wrapper(msg) + + mock_callafter.assert_called_once_with(remote_control._to_neuronavigation, msg) + + +def test_connect(setup_remote_control): + remote_control, mock_socketio, mock_callafter = setup_remote_control + + mock_sio_instance = mock_socketio.return_value + + with patch("time.sleep", return_value=None): + + remote_control._connected = True + remote_control.connect() + mock_socketio.assert_called_once() + + mock_sio_instance.on.assert_any_call("connect", remote_control._on_connect) + mock_sio_instance.on.assert_any_call("disconnect", remote_control._on_disconnect) + mock_sio_instance.on.assert_any_call("to_neuronavigation", remote_control._to_neuronavigation_wrapper) + + mock_sio_instance.connect.assert_called_once_with(remote_control._remote_host) + + mock_sio_instance.emit.assert_called_once_with("restart_robot_main_loop") From 40d518067d688ed13518ba5bf72462df11bce506 Mon Sep 17 00:00:00 2001 From: Devarsh Shroff <79686864+DevarshShroff@users.noreply.github.com> Date: Sat, 15 Mar 2025 23:07:12 -0700 Subject: [PATCH 2/3] Rebasing on top of latest master --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 51e91f6e0..5ba08ee66 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,4 +28,4 @@ pyclaron ; python_version == "3.8" and sys_platform == "windows" # Testing pytest==8.3.5 -pytest-mock==3.14.0 \ No newline at end of file +pytest-mock==3.14.0 From 89fc8e7a435ee9dada114a3fff140a0abe115ff3 Mon Sep 17 00:00:00 2001 From: Devarsh Shroff <79686864+DevarshShroff@users.noreply.github.com> Date: Thu, 20 Mar 2025 20:02:09 -0700 Subject: [PATCH 3/3] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 5ba08ee66..51e91f6e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,4 +28,4 @@ pyclaron ; python_version == "3.8" and sys_platform == "windows" # Testing pytest==8.3.5 -pytest-mock==3.14.0 +pytest-mock==3.14.0 \ No newline at end of file