Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and a fix for BaseStore.parent #1166

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)