# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright © 2017 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source # & Institut Laue - Langevin # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantidqt package # # # std imports import unittest # 3rdparty imports from mantid.api import WorkspaceFactory from mantid.py3compat import mock from qtpy.QtGui import QIcon from qtpy.QtWidgets import QDialog, QDialogButtonBox # local imports from mantidqt.utils.qt.testing import GuiTest from mantidqt.dialogs.spectraselectordialog import (get_spectra_selection, parse_selection_str, SpectraSelectionDialog) class SpectraSelectionDialogTest(GuiTest): _mock_get_icon = None _single_spec_ws = None _multi_spec_ws = None def setUp(self): # patch away getting a real icon as it can hit a race condition when running tests # in parallel patcher = mock.patch('mantidqt.dialogs.spectraselectordialog.get_icon') self._mock_get_icon = patcher.start() self._mock_get_icon.return_value = QIcon() self.addCleanup(patcher.stop) if self._single_spec_ws is None: self.__class__._single_spec_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=1, XLength=1, YLength=1) self.__class__._multi_spec_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=200, XLength=1, YLength=1) def test_initial_dialog_setup(self): workspaces = [self._multi_spec_ws] dlg = SpectraSelectionDialog(workspaces) self.assertFalse(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled()) def test_filling_workspace_details_single_workspace(self): workspaces = [self._multi_spec_ws] dlg = SpectraSelectionDialog(workspaces) self.assertEqual("valid range: 1-200", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-199", dlg._ui.wkspIndices.placeholderText()) def test_filling_workspace_details_multiple_workspaces_of_same_size(self): workspaces = [self._multi_spec_ws, self._multi_spec_ws] dlg = SpectraSelectionDialog(workspaces) self.assertEqual("valid range: 1-200", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-199", dlg._ui.wkspIndices.placeholderText()) def test_filling_workspace_details_multiple_workspaces_of_different_sizes(self): cropped_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=50, XLength=1, YLength=1) for i in range(cropped_ws.getNumberHistograms()): cropped_ws.getSpectrum(i).setSpectrumNo(51 + i) dlg = SpectraSelectionDialog([cropped_ws, self._multi_spec_ws]) self.assertEqual("valid range: 51-100", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-49", dlg._ui.wkspIndices.placeholderText()) def test_valid_text_in_boxes_activates_ok(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) def do_test(input_box): input_box.setText("1") self.assertTrue(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled()) input_box.clear() self.assertFalse(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled()) do_test(dlg._ui.wkspIndices) do_test(dlg._ui.specNums) def test_plot_all_gives_only_workspaces_indices(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.buttonBox.button(QDialogButtonBox.YesToAll).click() self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual(range(200), dlg.selection.wksp_indices) def test_entered_workspace_indices_gives_correct_selection_back(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.wkspIndices.setText("1-3,5") dlg._ui.buttonBox.button(QDialogButtonBox.Ok).click() self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual([1, 2, 3, 5], dlg.selection.wksp_indices) def test_entered_spectra_gives_correct_selection_back(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.wkspIndices.setText("50-60") dlg._ui.buttonBox.button(QDialogButtonBox.Ok).click() self._mock_get_icon.assert_called_once_with('mdi.asterisk', 'red', 0.6) self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual(list(range(50, 61)), dlg.selection.wksp_indices) @mock.patch('mantidqt.dialogs.spectraselectordialog.SpectraSelectionDialog', autospec=True) def test_get_spectra_selection_cancelled_returns_None(self, dialog_mock): # a new instance of the mock created inside get_spectra_selection will return # dialog_mock dialog_mock.return_value = dialog_mock dialog_mock.Rejected = QDialog.Rejected dialog_mock.exec_.return_value = dialog_mock.Rejected selection = get_spectra_selection([self._multi_spec_ws]) dialog_mock.exec_.assert_called_once_with() self.assertTrue(selection is None) @mock.patch('mantidqt.dialogs.spectraselectordialog.SpectraSelectionDialog') def test_get_spectra_selection_does_not_use_dialog_for_single_spectrum(self, dialog_mock): selection = get_spectra_selection([self._single_spec_ws]) dialog_mock.assert_not_called() self.assertEqual([0], selection.wksp_indices) self.assertEqual([self._single_spec_ws], selection.workspaces) def test_parse_selection_str_single_number(self): s = '1' self.assertEqual([1], parse_selection_str(s, 1, 3)) s = '2' self.assertEqual([2], parse_selection_str(s, 1, 3)) s = '3' self.assertEqual([3], parse_selection_str(s, 1, 3)) s = '-1' self.assertTrue(parse_selection_str(s, 1, 1) is None) s = '1' self.assertTrue(parse_selection_str(s, 2, 2) is None) s = '1' self.assertTrue(parse_selection_str(s, 2, 3) is None) def test_parse_selection_str_single_range(self): s = '1-3' self.assertEqual([1, 2, 3], parse_selection_str(s, 1, 3)) s = '2-4' self.assertEqual([2, 3, 4], parse_selection_str(s, 1, 5)) s = '2-4' self.assertTrue(parse_selection_str(s, 2, 3) is None) s = '2-4' self.assertTrue(parse_selection_str(s, 3, 5) is None) def test_parse_selection_str_mix_number_range_spaces(self): s = '1-3, 5,8,10, 11 ,12-14 , 15 -16, 16- 19' self.assertEqual([1, 2, 3, 5, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], parse_selection_str(s, 1, 20)) # --------------- failure tests ----------- def test_construction_with_non_MatrixWorkspace_type_raises_exception(self): table = WorkspaceFactory.Instance().createTable() self.assertRaises(ValueError, SpectraSelectionDialog, [self._single_spec_ws, table]) def test_get_spectra_selection_raises_error_with_wrong_workspace_type(self): table = WorkspaceFactory.Instance().createTable() self.assertRaises(ValueError, get_spectra_selection, [self._single_spec_ws, table]) if __name__ == '__main__': unittest.main()