From 476567a1f4f2712575679d9adecd384f1530783c Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Fri, 28 Jun 2024 16:36:34 +0300 Subject: [PATCH] Fix restoring entity classes DatabaseMapping.restore_items() returns restored items and errors, not just the items. Re #2861 --- spinetoolbox/spine_db_worker.py | 4 +++- tests/test_SpineDBManager.py | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/spinetoolbox/spine_db_worker.py b/spinetoolbox/spine_db_worker.py index 751b871de..87813ba93 100644 --- a/spinetoolbox/spine_db_worker.py +++ b/spinetoolbox/spine_db_worker.py @@ -277,7 +277,9 @@ def restore_items(self, item_type, ids): item_type (str): item type ids (set): ids of items to restore """ - items = self._db_map.restore_items(item_type, *ids) + items, errors = self._db_map.restore_items(item_type, *ids) + if any(errors): + self._db_mngr.error_msg.emit({self._db_map: errors}) if Asterisk in ids: items = self._db_map.get_items(item_type) self._db_mngr.update_icons(self._db_map, item_type, items) diff --git a/tests/test_SpineDBManager.py b/tests/test_SpineDBManager.py index 40fbe4e76..be3b65cfa 100644 --- a/tests/test_SpineDBManager.py +++ b/tests/test_SpineDBManager.py @@ -286,6 +286,45 @@ def test_add_object_metadata(self): ) +class TestDoRestoreItems(unittest.TestCase): + @classmethod + def setUpClass(cls): + if not QApplication.instance(): + QApplication() + + def setUp(self): + self._temp_dir = TemporaryDirectory() + db_path = Path(self._temp_dir.name, "db.sqlite") + self._db_url = "sqlite:///" + str(db_path) + self._db_mngr = SpineDBManager(None, None) + self._logger = MagicMock() + + def tearDown(self): + self._db_mngr.close_all_sessions() + self._db_mngr.clean_up() + # Database connection may still be open. Retry cleanup until it succeeds. + running = True + while running: + QApplication.processEvents() + try: + self._temp_dir.cleanup() + except NotADirectoryError: + pass + else: + running = False + + def test_restore_entity_class(self): + db_map = self._db_mngr.get_db_map(self._db_url, self._logger, create=True) + entity_class, error = db_map.add_entity_class_item(name="Gadget") + self.assertIsNone(error) + class_item = self._db_mngr.get_item(db_map, "entity_class", entity_class["id"]) + self.assertIs(entity_class, class_item) + self._db_mngr.remove_items({db_map: {"entity_class": {entity_class["id"]}}}) + self.assertFalse(class_item.is_valid()) + self._db_mngr.do_restore_items(db_map, "entity_class", {entity_class["id"]}) + self.assertTrue(class_item.is_valid()) + + class TestImportExportData(unittest.TestCase): @classmethod def setUpClass(cls):