From 33e31c8b6c12d29f4f0aa2adac5c53d0a13d9283 Mon Sep 17 00:00:00 2001
From: Conor Finn <conor.finn@stfc.ac.uk>
Date: Mon, 21 Oct 2019 14:35:01 +0100
Subject: [PATCH] RE #27012 Add tests for plotting and presenter

Tests added for plotting functionality in the model and the current functionality of the presenter.
---
 .../tabs/calibration/test/test_presenter.py   |  15 ++-
 .../tabs/focus/presenter.py                   |   1 -
 .../tabs/focus/test/test_model.py             |  25 +++++
 .../tabs/focus/test/test_presenter.py         | 100 +++++++++++++++++-
 4 files changed, 135 insertions(+), 6 deletions(-)

diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_presenter.py b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_presenter.py
index 4d9b1f868a6..a975de0f42d 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_presenter.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_presenter.py
@@ -102,9 +102,16 @@ class CalibrationPresenterTest(unittest.TestCase):
         result = self.presenter.validate_run_numbers()
         self.assertTrue(result)
 
-    def test_set_instrument_override(self):
-        instrument = "TEST"
+    def test_set_instrument_override_ENGINX(self):
+        instrument = 0
         self.presenter.set_instrument_override(instrument)
 
-        self.view.set_instrument_override.assert_called_with(instrument)
-        self.assertEqual(self.presenter.instrument, instrument)
+        self.view.set_instrument_override.assert_called_with("ENGINX")
+        self.assertEqual(self.presenter.instrument, "ENGINX")
+
+    def test_set_instrument_override_IMAT(self):
+        instrument = 1
+        self.presenter.set_instrument_override(instrument)
+
+        self.view.set_instrument_override.assert_called_with("IMAT")
+        self.assertEqual(self.presenter.instrument, "IMAT")
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py
index d30e46c7599..8492ab66325 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py
@@ -13,7 +13,6 @@ from mantidqt.utils.asynchronous import AsyncTask
 from mantid.simpleapi import logger
 
 
-# noinspection PyArgumentList
 class FocusPresenter(object):
     def __init__(self, model, view):
         self.model = model
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_model.py
index 3257003c89e..6ede0137ddf 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_model.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_model.py
@@ -44,8 +44,33 @@ class FocusModelTest(unittest.TestCase):
         fetch_van.return_value = ("mocked_integ", "mocked_curves")
         banks = ["1", "2"]
         load_focus.return_value = "mocked_sample"
+
         self.model.focus_run("305761", banks, False, "ENGINX", "0", self.current_calibration)
         self.assertEqual(len(banks), run_focus.call_count)
         run_focus.assert_called_with("mocked_sample",
                                      model.FOCUSED_OUTPUT_WORKSPACE_NAME + banks[-1],
                                      "mocked_integ", "mocked_curves", banks[-1])
+
+    @patch(file_path + ".FocusModel._plot_focused_workspace")
+    @patch(file_path + ".FocusModel._run_focus")
+    @patch(file_path + ".FocusModel._load_focus_sample_run")
+    @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces")
+    def test_focus_plotted_when_checked(self, fetch_van, load_focus, run_focus, plot_focus):
+        fetch_van.return_value = ("mocked_integ", "mocked_curves")
+        banks = ["1", "2"]
+        load_focus.return_value = "mocked_sample"
+
+        self.model.focus_run("305761", banks, True, "ENGINX", "0", self.current_calibration)
+        self.assertEqual(len(banks), plot_focus.call_count)
+
+    @patch(file_path + ".FocusModel._plot_focused_workspace")
+    @patch(file_path + ".FocusModel._run_focus")
+    @patch(file_path + ".FocusModel._load_focus_sample_run")
+    @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces")
+    def test_focus_not_plotted_when_not_checked(self, fetch_van, load_focus, run_focus, plot_focus):
+        fetch_van.return_value = ("mocked_integ", "mocked_curves")
+        banks = ["1", "2"]
+        load_focus.return_value = "mocked_sample"
+
+        self.model.focus_run("305761", banks, False, "ENGINX", "0", self.current_calibration)
+        self.assertEqual(0, plot_focus.call_count)
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_presenter.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_presenter.py
index 5372405cf71..cefcccf4788 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_presenter.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_presenter.py
@@ -28,6 +28,104 @@ class FocusPresenterTest(unittest.TestCase):
         self.view.get_north_bank.return_value = False
         self.view.get_south_bank.return_value = True
         self.view.get_plot_output.return_value = True
+        self.view.is_searching.return_value = False
 
         self.presenter.on_focus_clicked()
-        worker.assert_called_with("305738", ["South"], True)
+        worker.assert_called_with("305738", ["South"], True, None)
+
+    @patch(tab_path + ".presenter.FocusPresenter.start_focus_worker")
+    def test_worker_not_started_while_searching(self, worker):
+        self.view.get_focus_filename.return_value = "305738"
+        self.view.get_north_bank.return_value = False
+        self.view.get_south_bank.return_value = True
+        self.view.get_plot_output.return_value = True
+        self.view.is_searching.return_value = True
+
+        self.presenter.on_focus_clicked()
+        worker.assert_not_called()
+
+    @patch(tab_path + ".presenter.FocusPresenter.start_focus_worker")
+    def test_worker_not_started_when_no_banks_selected(self, worker):
+        self.view.get_focus_filename.return_value = "305738"
+        self.view.get_north_bank.return_value = False
+        self.view.get_south_bank.return_value = False
+        self.view.get_plot_output.return_value = True
+        self.view.is_searching.return_value = True
+
+        self.presenter.on_focus_clicked()
+        worker.assert_not_called()
+
+    def test_controls_disabled_disables_both(self):
+        self.presenter.disable_focus_controls()
+
+        self.view.set_focus_button_enabled.assert_called_with(False)
+        self.view.set_plot_output_enabled.assert_called_with(False)
+
+    def test_controls_enabled_enables_both(self):
+        self.presenter.enable_focus_controls()
+
+        self.view.set_focus_button_enabled.assert_called_with(True)
+        self.view.set_plot_output_enabled.assert_called_with(True)
+
+    @patch(tab_path + ".presenter.logger.warning")
+    def test_on_worker_error_posts_to_logger_and_enables_controls(self, logger):
+        fail_info = 2024278
+
+        self.presenter._on_worker_error(fail_info)
+
+        logger.assert_called_with(str(fail_info))
+        self.view.set_focus_button_enabled.assert_called_with(True)
+        self.view.set_plot_output_enabled.assert_called_with(True)
+
+    def test_get_banks(self):
+        self.view.get_north_bank.return_value = True
+        self.view.get_south_bank.return_value = True
+
+        self.assertEqual(["North", "South"], self.presenter.get_banks())
+
+        self.view.get_north_bank.return_value = True
+        self.view.get_south_bank.return_value = False
+
+        self.assertEqual(["North"], self.presenter.get_banks())
+
+        self.view.get_north_bank.return_value = False
+        self.view.get_south_bank.return_value = True
+
+        self.assertEqual(["South"], self.presenter.get_banks())
+
+        self.view.get_north_bank.return_value = False
+        self.view.get_south_bank.return_value = False
+
+        self.assertEqual([], self.presenter.get_banks())
+
+    def test_validate_with_invalid_focus_path(self):
+        self.view.get_focus_valid.return_value = False
+        banks = ["North", "South"]
+
+        self.assertFalse(self.presenter.validate(banks))
+
+    @patch(tab_path + ".presenter.FocusPresenter.create_error_message")
+    def test_validate_with_invalid_calibration(self, create_error):
+        self.presenter.current_calibration = {"vanadium_path": None, "ceria_path": None}
+        banks = ["North", "South"]
+
+        self.presenter.validate(banks)
+        create_error.assert_called_with("Load a calibration from the Calibration tab before focusing.")
+
+    @patch(tab_path + ".presenter.FocusPresenter.create_error_message")
+    def test_validate_while_searching(self, create_error):
+        self.presenter.current_calibration = {"vanadium_path": "Fake/File/Path", "ceria_path": "Fake/Path"}
+        self.view.is_searching.return_value = True
+        banks = ["North", "South"]
+
+        self.assertEqual(False, self.presenter.validate(banks))
+        self.assertEqual(0, create_error.call_count)
+
+    @patch(tab_path + ".presenter.FocusPresenter.create_error_message")
+    def test_validate_with_no_banks_selected(self, create_error):
+        self.presenter.current_calibration = {"vanadium_path": "Fake/Path", "ceria_path": "Fake/Path"}
+        self.view.is_searching.return_value = False
+        banks = []
+
+        self.presenter.validate(banks)
+        create_error.assert_called_with("Please select at least one bank.")
-- 
GitLab