From 41674a9975fced5a78d8e73b5648fb33fdeeae12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Eide?= Date: Thu, 18 Jan 2024 08:46:44 +0100 Subject: [PATCH] Mock fixes --- src/ert/gui/simulation/run_dialog.py | 6 +- .../gui/simulation/test_run_dialog.py | 79 ++++++++++--------- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/ert/gui/simulation/run_dialog.py b/src/ert/gui/simulation/run_dialog.py index 9ec74fd06ab..1d25bb66e8b 100644 --- a/src/ert/gui/simulation/run_dialog.py +++ b/src/ert/gui/simulation/run_dialog.py @@ -287,9 +287,9 @@ def killJobs(self): if kill_job == QMessageBox.Yes: # Normally this slot would be invoked by the signal/slot system, # but the worker is busy tracking the evaluation. - self._tracker.request_termination() - self._worker_thread.quit() - self._worker_thread.wait() + # self._tracker.request_termination() + # self._worker_thread.quit() + # self._worker_thread.wait() self._on_finished() self.finished.emit(-1) return kill_job diff --git a/tests/unit_tests/gui/simulation/test_run_dialog.py b/tests/unit_tests/gui/simulation/test_run_dialog.py index 2d278b82ce2..5f41d1ee811 100644 --- a/tests/unit_tests/gui/simulation/test_run_dialog.py +++ b/tests/unit_tests/gui/simulation/test_run_dialog.py @@ -1,6 +1,6 @@ import os from pathlib import Path -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch import pytest from pytestqt.qtbot import QtBot @@ -25,15 +25,21 @@ from tests.unit_tests.gui.simulation.test_run_path_dialog import handle_run_path_dialog -def test_success(runmodel, qtbot: QtBot, mock_tracker): +class RunModelMock(MagicMock): + def add_send_event_callback(self, callback): + self.callback = callback + + +def test_success(qtbot: QtBot): notifier = Mock() - widget = RunDialog("poly.ert", runmodel, notifier) + run_model_mock = RunModelMock() + + widget = RunDialog("mock.ert", run_model_mock, notifier) widget.show() qtbot.addWidget(widget) - with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker: - tracker.return_value = mock_tracker([EndEvent(failed=False, failed_msg="")]) - widget.startSimulation() + widget.startSimulation() + run_model_mock.callback(EndEvent(failed=False, failed_msg="")) with qtbot.waitExposed(widget, timeout=30000): qtbot.waitUntil(lambda: widget._total_progress_bar.value() == 100) @@ -41,15 +47,15 @@ def test_success(runmodel, qtbot: QtBot, mock_tracker): assert widget.done_button.text() == "Done" -def test_kill_simulations(runmodel, qtbot: QtBot, mock_tracker): +def test_kill_simulations(qtbot: QtBot): notifier = Mock() - widget = RunDialog("poly.ert", runmodel, notifier) + run_model_mock = RunModelMock() + widget = RunDialog("mock.ert", run_model_mock, notifier) widget.show() qtbot.addWidget(widget) - with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker: - tracker.return_value = mock_tracker([EndEvent(failed=False, failed_msg="")]) - widget.startSimulation() + widget.startSimulation() + run_model_mock.callback(EndEvent(failed=False, failed_msg="")) with qtbot.waitSignal(widget.finished, timeout=30000): @@ -78,16 +84,15 @@ def handle_dialog(): widget.killJobs() -def test_large_snapshot( - runmodel, large_snapshot, qtbot: QtBot, mock_tracker, timeout_per_iter=5000 -): +def test_large_snapshot(large_snapshot, qtbot: QtBot, timeout_per_iter=5000): notifier = Mock() - widget = RunDialog("poly.ert", runmodel, notifier) + run_model_mock = RunModelMock() + widget = RunDialog("mock.ert", run_model_mock, notifier) widget.show() qtbot.addWidget(widget) - with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker: - iter_0 = FullSnapshotEvent( + events = [ + FullSnapshotEvent( snapshot=large_snapshot, phase_name="Foo", current_phase=0, @@ -95,8 +100,8 @@ def test_large_snapshot( progress=0.5, iteration=0, indeterminate=False, - ) - iter_1 = FullSnapshotEvent( + ), + FullSnapshotEvent( snapshot=large_snapshot, phase_name="Foo", current_phase=0, @@ -104,11 +109,13 @@ def test_large_snapshot( progress=0.5, iteration=1, indeterminate=False, - ) - tracker.return_value = mock_tracker( - [iter_0, iter_1, EndEvent(failed=False, failed_msg="")] - ) - widget.startSimulation() + ), + EndEvent(failed=False, failed_msg=""), + ] + + widget.startSimulation() + for event in events: + run_model_mock.callback(event) with qtbot.waitExposed(widget, timeout=timeout_per_iter * 6): qtbot.waitUntil( @@ -317,15 +324,16 @@ def test_large_snapshot( ), ], ) -def test_run_dialog(events, tab_widget_count, runmodel, qtbot: QtBot): +def test_run_dialog(events, tab_widget_count, qtbot: QtBot): notifier = Mock() - widget = RunDialog("poly.ert", runmodel, notifier) + run_model_mock = RunModelMock() + widget = RunDialog("mock.ert", run_model_mock, notifier) widget.show() qtbot.addWidget(widget) - with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker: - tracker.return_value = mock_tracker(events) - widget.startSimulation() + widget.startSimulation() + for event in events: + run_model_mock.callback(event) with qtbot.waitExposed(widget, timeout=30000): qtbot.mouseClick(widget.show_details_button, Qt.LeftButton) @@ -476,17 +484,16 @@ def handle_dialog(): ), ], ) -def test_run_dialog_memory_usage_showing( - events, tab_widget_count, runmodel, qtbot: QtBot, mock_tracker -): +def test_run_dialog_memory_usage_showing(events, tab_widget_count, qtbot: QtBot): notifier = Mock() - widget = RunDialog("poly.ert", runmodel, notifier) + run_model_mock = RunModelMock() + widget = RunDialog("poly.ert", run_model_mock, notifier) widget.show() qtbot.addWidget(widget) - with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker: - tracker.return_value = mock_tracker(events) - widget.startSimulation() + widget.startSimulation() + for event in events: + run_model_mock.callback(event) with qtbot.waitExposed(widget, timeout=30000): qtbot.mouseClick(widget.show_details_button, Qt.LeftButton)