Skip to content

Commit

Permalink
Fix restoring entity classes (#2865)
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen authored Jun 28, 2024
2 parents 24df69b + 476567a commit c09d43d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spinetoolbox/spine_db_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_SpineDBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c09d43d

Please sign in to comment.