Skip to content

Commit

Permalink
Fixed unit tests, now using math.isclose to compare floats
Browse files Browse the repository at this point in the history
  • Loading branch information
elvio.aruta98 committed Jan 5, 2024
1 parent 573ea8a commit e73a6a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
27 changes: 10 additions & 17 deletions mir_connector/inorbit_mir_connector/tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def reset_mock():
connector.mir_api.reset_mock()

# Simulate an executor timeout, which should disable robot mission tracking
connector.inorbit_sess.missions_module.executor.wait_until_idle = Mock(return_value=False)
connector.inorbit_sess.missions_module.executor.wait_until_idle = Mock(
return_value=False
)
assert connector.mission_tracking.mir_mission_tracking_enabled is False
callback_kwargs["command_name"] = "customCommand"
callback_kwargs["args"] = ["queue_mission", ["--mission_id", "1"]]
Expand All @@ -92,7 +94,9 @@ def reset_mock():
reset_mock()

# Queue mission
connector.inorbit_sess.missions_module.executor.wait_until_idle = Mock(return_value=True)
connector.inorbit_sess.missions_module.executor.wait_until_idle = Mock(
return_value=True
)
callback_kwargs["command_name"] = "customCommand"
callback_kwargs["args"] = ["queue_mission", ["--mission_id", "2"]]
connector.command_callback(**callback_kwargs)
Expand All @@ -114,7 +118,10 @@ def reset_mock():
callback_kwargs["command_name"] = "customCommand"
callback_kwargs["args"] = ["abort_missions", []]
connector.command_callback(**callback_kwargs)
assert connector.inorbit_sess.missions_module.executor.cancel_mission.call_args == call("*")
assert (
connector.inorbit_sess.missions_module.executor.cancel_mission.call_args
== call("*")
)
assert connector.mir_api.abort_all_missions.call_args == call()
callback_kwargs["options"]["result_function"].assert_called_with("0")
reset_mock()
Expand Down Expand Up @@ -229,18 +236,6 @@ def test_connector_loop(connector, monkeypatch):

def run_loop_once():
monkeypatch.setattr(inorbit_mir_connector.src.connector, "sleep", Mock())
monkeypatch.setattr(
inorbit_mir_connector.src.connector.psutil,
"cpu_percent",
Mock(return_value=12.5),
)
virtual_memory = Mock()
virtual_memory.percent = 33.3
monkeypatch.setattr(
inorbit_mir_connector.src.connector.psutil,
"virtual_memory",
Mock(return_value=virtual_memory),
)
connector_thread = threading.Thread(target=connector.start)
connector_thread.start()
while not inorbit_mir_connector.src.connector.sleep.called:
Expand Down Expand Up @@ -322,8 +317,6 @@ def run_loop_once():
"mode_text": "Mission",
"robot_model": "MiR100",
"waiting_for": "",
"cpu": 12.5,
"memory_usage": 33.3,
}
)

Expand Down
35 changes: 28 additions & 7 deletions mir_connector/inorbit_mir_connector/tests/test_mir_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from deepdiff import DeepDiff
from requests.exceptions import HTTPError
from unittest.mock import MagicMock
import math


@pytest.fixture
def mir_api(requests_mock, monkeypatch):
mir_host_address = "example.com"
mir_host_port = 8080
requests_mock.post("http://example.com:8080/?mode=log-in", text="I'm letting you in")
requests_mock.post(
"http://example.com:8080/?mode=log-in", text="I'm letting you in"
)
monkeypatch.setattr(websocket, "WebSocketApp", MagicMock())
api = MirApiV2(
mir_host_address=mir_host_address,
Expand Down Expand Up @@ -160,17 +163,35 @@ def test_websocket_diagnostics_agg_msg(mir_websocket, sample_mir_diagnostics_agg
assert not mir_websocket.last_diagnostics_agg_msg

# Process expected message
mir_websocket.on_message(mir_websocket.ws, json.dumps(sample_mir_diagnostics_agg_data))
assert DeepDiff(sample_mir_diagnostics_agg_data, mir_websocket.last_diagnostics_agg_msg) == {}
mir_websocket.on_message(
mir_websocket.ws, json.dumps(sample_mir_diagnostics_agg_data)
)
assert (
DeepDiff(
sample_mir_diagnostics_agg_data, mir_websocket.last_diagnostics_agg_msg
)
== {}
)

# Make sure invalid message won't override last_diagnostics_agg_msg
mir_websocket.on_message(mir_websocket.ws, "fail json parse")
assert DeepDiff(sample_mir_diagnostics_agg_data, mir_websocket.last_diagnostics_agg_msg) == {}
assert (
DeepDiff(
sample_mir_diagnostics_agg_data, mir_websocket.last_diagnostics_agg_msg
)
== {}
)

# Test methods for getting relevant values
assert mir_websocket.get_cpu_usage() == "49.2"
cpu_usage = float(mir_websocket.get_cpu_usage())
assert math.isclose(cpu_usage, 0.492, abs_tol=0.0001)

# Process message with missing data
mir_websocket.on_message(mir_websocket.ws, json.dumps({"topic": "/diagnostics_agg"}))
assert DeepDiff({"topic": "/diagnostics_agg"}, mir_websocket.last_diagnostics_agg_msg) == {}
mir_websocket.on_message(
mir_websocket.ws, json.dumps({"topic": "/diagnostics_agg"})
)
assert (
DeepDiff({"topic": "/diagnostics_agg"}, mir_websocket.last_diagnostics_agg_msg)
== {}
)
assert mir_websocket.get_cpu_usage() is None

0 comments on commit e73a6a6

Please sign in to comment.