Skip to content

Commit

Permalink
Tests and a fix for BaseStore.parent
Browse files Browse the repository at this point in the history
- add unit tests for BaseStore.parent
- ensure that the store is unchanged when called with invalid IDs
- add helpful error messages to the exceptions
  • Loading branch information
gycsaba96 authored and diegogangl committed Dec 30, 2024
1 parent c271e2a commit 77fc73a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
20 changes: 9 additions & 11 deletions GTG/core/base_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
66 changes: 66 additions & 0 deletions tests/core/test_base_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 77fc73a

Please sign in to comment.