From 4b163d2d2b0e6be5e5db343fc7e55f419ce96559 Mon Sep 17 00:00:00 2001 From: Matthew D Jones Date: Mon, 16 Mar 2020 08:05:59 +0000 Subject: [PATCH] Add unit test and fix for deleting component with transformation --- nexus_constructor/component_tree_model.py | 26 +++++++++++++---------- tests/test_component_tree_model.py | 14 ++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/nexus_constructor/component_tree_model.py b/nexus_constructor/component_tree_model.py index dd2150727..0817ffdf5 100644 --- a/nexus_constructor/component_tree_model.py +++ b/nexus_constructor/component_tree_model.py @@ -130,18 +130,22 @@ def _remove_transformation(self, index: QModelIndex): def _remove_component(self, index: QModelIndex): component = index.internalPointer() transforms = component.transforms - if transforms and transforms[0].dependents(): - reply = QMessageBox.question( - None, - "Delete component?", - "this component has transformations that are depended on. Are you sure you want to delete it?", - QMessageBox.Yes, - QMessageBox.No, + if transforms: + has_dependents_other_than_the_component_being_deleted = ( + len(transforms[0].dependents) > 1 ) - if reply == QMessageBox.Yes: - pass - elif reply == QMessageBox.No: - return + if has_dependents_other_than_the_component_being_deleted: + reply = QMessageBox.question( + None, + "Delete component?", + "this component has transformations that are depended on. Are you sure you want to delete it?", + QMessageBox.Yes, + QMessageBox.No, + ) + if reply == QMessageBox.Yes: + pass + elif reply == QMessageBox.No: + return remove_index = self.components.index(index.internalPointer()) self.beginRemoveRows(QModelIndex(), remove_index, remove_index) for transform in transforms: diff --git a/tests/test_component_tree_model.py b/tests/test_component_tree_model.py index 9eb038cdc..c6d7ff2b2 100644 --- a/tests/test_component_tree_model.py +++ b/tests/test_component_tree_model.py @@ -509,6 +509,20 @@ def test_remove_component(nexus_wrapper): assert under_test.rowCount(QModelIndex()) == 0 +def test_remove_component_with_transformation(nexus_wrapper): + instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS) + under_test = ComponentTreeModel(instrument) + instrument.create_component("Some name", "some class", "desc") + component_index = under_test.index(0, 0, QModelIndex()) + under_test.add_rotation(component_index) + assert under_test.rowCount(QModelIndex()) == 1 + under_test.remove_node(component_index) + assert under_test.rowCount(QModelIndex()) == 0, ( + "Expected component to be successfully deleted because it has " + "a transformation that only has it as a dependent" + ) + + def test_remove_transformation(nexus_wrapper): instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS) under_test = ComponentTreeModel(instrument)