Skip to content
Snippets Groups Projects
Commit 38f37ada authored by Ian Bush's avatar Ian Bush
Browse files

Refs #22477 Added a complete set of presenter tests

parent 3d60ae7d
No related branches found
No related tags found
No related merge requests found
......@@ -35,7 +35,7 @@ class PlotSelector(PluginWidget):
plot_selector_presenter = PlotSelectorPresenter(CurrentFigure)
# layout
self.plot_selector = plot_selector_presenter.get_widget()
self.plot_selector = plot_selector_presenter.widget
layout = QVBoxLayout()
layout.addWidget(self.plot_selector)
self.setLayout(layout)
......
......@@ -39,52 +39,48 @@ class PlotSelectorPresenter(object):
# Make sure the plot list is up to date
self.update_plot_list()
def get_widget(self):
return self.view
def update_plot_list(self):
self.model.update_plot_list()
filter_text = self.view.get_filter_text()
if not filter_text:
self.view.set_plot_list(self.model.plot_list)
else:
self.filter_list_by_string(filter_text)
self._filter_plot_list_by_string(filter_text)
# ----------------------- Plot Selection ------------------------
# ------------------------ Plot Closing -------------------------
def list_double_clicked(self):
plot_name = self.view.get_currently_selected_plot_name()
self.make_plot_active(plot_name)
def close_button_clicked(self):
selected_plots = self.view.get_all_selected_plot_names()
self._close_plots(selected_plots)
def make_plot_active(self, plot_name):
self.model.make_plot_active(plot_name)
def _close_plots(self, list_of_plots):
for plot_name in list_of_plots:
self._close_plot(plot_name)
def _close_plot(self, plot_name):
self.model.close_plot(plot_name)
# ----------------------- Plot Filtering ------------------------
def filter_text_changed(self):
filter_text = self.view.get_filter_text()
self.filter_list_by_string(filter_text)
self._filter_plot_list_by_string(filter_text)
def filter_list_by_string(self, filter_text):
def _filter_plot_list_by_string(self, filter_text):
if not filter_text:
self.view.set_plot_list(self.model.plot_list)
else:
filtered_plot_list = []
for plot_name in self.model.plot_list:
if filter_text in plot_name:
if filter_text.lower() in plot_name.lower():
filtered_plot_list.append(plot_name)
self.view.set_plot_list(filtered_plot_list)
# ------------------------ Plot Closing -------------------------
def close_button_clicked(self):
selected_plots = self.view.get_all_selected_plot_names()
self.close_plots(selected_plots)
def close_plots(self, list_of_plots):
for plot_name in list_of_plots:
self.close_plot(plot_name)
# ----------------------- Plot Selection ------------------------
def close_plot(self, plot_name):
self.model.close_plot(plot_name)
def list_double_clicked(self):
plot_name = self.view.get_currently_selected_plot_name()
self.make_plot_active(plot_name)
def make_plot_active(self, plot_name):
self.model.make_plot_active(plot_name)
......@@ -35,19 +35,34 @@ class PlotSelectorPresenterTest(unittest.TestCase):
self.widget.get_filter_text = mock.Mock(return_value="")
self.model = mock.Mock(spec=PlotSelectorModel)
self.model.configure_mock(plot_list=["Plot1", "Plot2", "Plot3"])
self.model.configure_mock(plot_list=["Plot1", "Plot2", "Plot3", "Graph99"])
self.presenter = PlotSelectorPresenter(None, self.widget, self.model)
self.presenter.widget = self.widget
self.presenter.model = self.model
# Ignore calls during the setup
self.widget.reset_mock()
self.model.reset_mock()
def convert_list_to_calls(self, list_to_convert):
call_list = []
for item in list_to_convert:
call_list.append(mock.call(item))
return call_list
# ------------------------ Closing Tests ------------------------
def test_plot_list_update(self):
self.presenter.update_plot_list()
self.assertEqual(self.model.update_plot_list.call_count, 1)
self.widget.set_plot_list.assert_called_once_with(["Plot1", "Plot2", "Plot3", "Graph99"])
def test_plot_list_update_with_filter_set(self):
self.widget.get_filter_text = mock.Mock(return_value="Graph99")
self.presenter.update_plot_list()
self.assertEqual(self.model.update_plot_list.call_count, 1)
self.widget.set_plot_list.assert_called_once_with(["Graph99"])
# ------------------------ Plot Closing -------------------------
def test_close_button_pressed_single_plot(self):
self.widget.get_all_selected_plot_names = mock.Mock(return_value=["Plot1"])
......@@ -76,6 +91,38 @@ class PlotSelectorPresenterTest(unittest.TestCase):
self.presenter.close_button_clicked()
self.assertEqual(self.model.close_plot.call_count, 0)
# ----------------------- Plot Filtering ------------------------
def test_no_filtering_displays_all_plots(self):
self.presenter.filter_text_changed()
self.widget.set_plot_list.assert_called_once_with(["Plot1", "Plot2", "Plot3", "Graph99"])
def test_plots_filtered_on_full_name(self):
self.widget.get_filter_text = mock.Mock(return_value="Plot1")
self.presenter.filter_text_changed()
self.widget.set_plot_list.assert_called_once_with(["Plot1"])
def test_plots_filtered_on_substring(self):
self.widget.get_filter_text = mock.Mock(return_value="lot")
self.presenter.filter_text_changed()
self.widget.set_plot_list.assert_called_once_with(["Plot1", "Plot2", "Plot3"])
def test_filtering_case_invariant(self):
self.widget.get_filter_text = mock.Mock(return_value="pLOT1")
self.presenter.filter_text_changed()
self.widget.set_plot_list.assert_called_once_with(["Plot1"])
# ----------------------- Plot Selection ------------------------
def test_double_clicking_plot_brings_to_front(self):
self.widget.get_currently_selected_plot_name = mock.Mock(return_value="Plot2")
self.presenter.list_double_clicked()
self.model.make_plot_active.assert_called_once_with("Plot2")
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment