From f9721191269d8962b9fd4c13776fdfe418e30b06 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Thu, 21 Dec 2017 14:41:05 +1300 Subject: [PATCH] beef up partition view tests a bit --- .../views/filesystem/tests/test_partition.py | 40 ++++++++++++++++++- subiquitycore/testing/view_helpers.py | 29 ++++++++------ 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/subiquity/ui/views/filesystem/tests/test_partition.py b/subiquity/ui/views/filesystem/tests/test_partition.py index a555d4d7..a0d838c6 100644 --- a/subiquity/ui/views/filesystem/tests/test_partition.py +++ b/subiquity/ui/views/filesystem/tests/test_partition.py @@ -9,6 +9,7 @@ from subiquitycore.testing import view_helpers from subiquity.controllers.filesystem import FilesystemController from subiquity.models.filesystem import ( + dehumanize_size, Disk, FilesystemModel, Partition, @@ -43,6 +44,41 @@ class PartitionViewTests(unittest.TestCase): view = self.make_view() self.assertIsNone(view_helpers.find_button_matching(view, "Delete")) - def test_delete_present_for_partition(self): + def test_delete_not_disabled_for_ordinary_partition(self): view = self.make_view(Partition(size=50)) - self.assertIsNotNone(view_helpers.find_button_matching(view, "Delete")) + but, path = view_helpers.find_button_matching(view, "Delete", return_path=True) + self.assertIsNotNone(but) + for w in path: + if isinstance(w, urwid.WidgetDisable): + self.fail("Delete button is disabled") + + def test_delete_disabled_for_boot_partition(self): + view = self.make_view(Partition(size=50, flag="boot")) + but, path = view_helpers.find_button_matching(view, "Delete", return_path=True) + self.assertIsNotNone(but) + for w in path: + if isinstance(w, urwid.WidgetDisable): + return + else: + self.fail("Delete button not disabled") + + def test_click_delete_button(self): + partition = Partition(size=50) + view = self.make_view(partition) + but = view_helpers.find_button_matching(view, "Delete") + view_helpers.click(but) + view.controller.delete_partition.assert_called_once_with(partition) + + def test_create_partition(self): + valid_data = { + 'partnum':1, + 'size':"1M", + 'fstype':FilesystemModel.fs_by_name["ext4"], + } + view = self.make_view() + view_helpers.enter_data(view.form, valid_data) + view_helpers.click(view.form.done_btn.base_widget) + valid_data['mount'] = '/' + valid_data['size'] = dehumanize_size(valid_data['size']) + view.controller.partition_disk_handler.assert_called_once_with( + view.disk, None, valid_data) diff --git a/subiquitycore/testing/view_helpers.py b/subiquitycore/testing/view_helpers.py index 3db76ef2..0a6d3d8c 100644 --- a/subiquitycore/testing/view_helpers.py +++ b/subiquitycore/testing/view_helpers.py @@ -2,31 +2,36 @@ import re import urwid -def find_with_pred(w, pred): - def _walk(w): +def find_with_pred(w, pred, return_path=False): + def _walk(w, path): if pred(w): - return w + return w, path if hasattr(w, '_wrapped_widget'): - return _walk(w._wrapped_widget) + return _walk(w._wrapped_widget, (w,) + path) if hasattr(w, 'original_widget'): - return _walk(w.original_widget) + return _walk(w.original_widget, (w,) + path) if isinstance(w, urwid.ListBox): for w in w.body: - r = _walk(w) + r, p = _walk(w, (w,) + path) if r: - return r + return r, p elif hasattr(w, 'contents'): contents = w.contents for w, _ in contents: - r = _walk(w) + r, p = _walk(w, (w,) + path) if r: - return r - return _walk(w) + return r, p + return None, None + r, p = _walk(w, ()) + if return_path: + return r, p + else: + return r -def find_button_matching(w, pat): +def find_button_matching(w, pat, return_path=False): def pred(w): return isinstance(w, urwid.Button) and re.match(pat, w.label) - return find_with_pred(w, pred) + return find_with_pred(w, pred, return_path) def click(but): but._emit('click')