Newer
Older
# This file is part of the mantidqt package
#
# Copyright (C) 2017 mantidproject
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# std imports
import unittest
try:
from unittest import mock
except ImportError:
import mock
from mantid.api import WorkspaceFactory
from qtpy.QtWidgets import QDialog, QDialogButtonBox
from mantidqt.utils.qt.test import requires_qapp
from mantidqt.dialogs.spectraselectordialog import (get_spectra_selection, parse_selection_str,
SpectraSelectionDialog)
class SpectraSelectionDialogTest(unittest.TestCase):
_single_spec_ws = None
_multi_spec_ws = None
def setUp(self):
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.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)
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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__':