From bb4f0c84807d3a6b97c0003d459d44e2b9932f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Gy=C3=B6rgyi?= Date: Sun, 29 Dec 2024 12:51:59 +0100 Subject: [PATCH] Tests and a fix for BaseStore.parent - add unit tests for BaseStore.parent - ensure that the store is unchanged when called with invalid IDs - add helpful error messages to the exceptions --- GTG/core/base_store.py | 20 +++++------ tests/core/test_base_store.py | 66 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/GTG/core/base_store.py b/GTG/core/base_store.py index 8880b113c..3f3920c1d 100644 --- a/GTG/core/base_store.py +++ b/GTG/core/base_store.py @@ -179,19 +179,17 @@ def batch_remove(self,item_ids: list[UUID]) -> None: def parent(self, item_id: UUID, parent_id: UUID) -> None: """Add a child to an item.""" - try: - item = self.lookup[item_id] - except KeyError: - raise + if item_id not in self.lookup: + raise KeyError("item_id is not in store: "+str(item_id)) + if parent_id not in self.lookup: + raise KeyError("parent_id is not in store: "+str(parent_id)) - try: - self.data.remove(item) - self.lookup[parent_id].children.append(item) - item.parent = self.lookup[parent_id] + item = self.lookup[item_id] + item.parent = self.lookup[parent_id] - self.emit('parent-change', item, self.lookup[parent_id]) - except KeyError: - raise + self.data.remove(item) + self.lookup[parent_id].children.append(item) + self.emit('parent-change', item, self.lookup[parent_id]) def unparent(self, item_id: UUID, parent_id: UUID) -> None: diff --git a/tests/core/test_base_store.py b/tests/core/test_base_store.py index 970b6d25c..b259672e7 100644 --- a/tests/core/test_base_store.py +++ b/tests/core/test_base_store.py @@ -67,3 +67,69 @@ def on_remove(obj,s): self.store.connect('removed',on_remove) self.store.remove(self.tree_items[0].id) self.assertEqual(removed,{ str(item.id) for item in self.tree_items }) + + + +class BaseStoreParent(TestCase): + + + def setUp(self): + self.store = BaseStore() + + self.root1 = StoreItem(uuid4()) + self.root2 = StoreItem(uuid4()) + self.child = StoreItem(uuid4()) + self.invalid_id = uuid4() + + self.store.add(self.root1) + self.store.add(self.root2) + self.store.add(self.child,parent_id=self.root1.id) + + + def test_parent_is_set(self): + self.store.parent(self.root2.id,self.root1.id) + self.assertEqual(self.root2.parent,self.root1) + + + def test_children_list_is_updated(self): + self.store.parent(self.root2.id,self.root1.id) + self.assertTrue(self.root2 in self.root1.children) + + + def test_root_elements_are_updated(self): + self.store.parent(self.root2.id,self.root1.id) + self.assertEqual(self.store.count(root_only=True),1) + + + def test_invalid_item_id_raises_exception(self): + with self.assertRaises(KeyError): + self.store.parent(self.invalid_id,self.root1.id) + + + def test_invalid_item_id_does_not_update_children(self): + try: + self.store.parent(self.invalid_id,self.root1.id) + except KeyError: + pass + self.assertTrue(self.invalid_id not in self.root1.children) + + + def test_invalid_parent_id_raises_exception(self): + with self.assertRaises(KeyError): + self.store.parent(self.root1.id,self.invalid_id) + + + def test_invalid_parent_id_does_not_update_parent(self): + try: + self.store.parent(self.root1.id,self.invalid_id) + except KeyError: + pass + self.assertEqual(self.root1.parent,None) + + + def test_invalid_parent_id_does_not_update_root_elements(self): + try: + self.store.parent(self.root1.id,self.invalid_id) + except KeyError: + pass + self.assertEqual(self.store.count(root_only=True),2)