Skip to content

Commit

Permalink
FIX/TST: be more thorough when filling EntryItem children. Adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed Nov 14, 2024
1 parent def7ba6 commit 42e01c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion superscore/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_fill_depth(fill_depth: int):
bknd = TestBackend([deep_coll])
client = Client(backend=bknd)

assert nest_depth(deep_coll) == 40
assert nest_depth(deep_coll) == 20
deep_coll.swap_to_uuids()
# for this test we want everything to be UUIDS
for entry in bknd._entry_cache.values():
Expand Down
21 changes: 17 additions & 4 deletions superscore/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def test_fill_uuids_nestable(
assert all(not isinstance(c, UUID) for c in nested_coll.children)


def test_fill_uuids_entry_item(linac_backend: TestBackend):
def test_fill_uuids_entry_item(linac_backend: TestBackend, qtbot: QtBot):
client = Client(backend=linac_backend)
nested_coll = linac_backend.get_entry("441ff79f-4948-480e-9646-55a1462a5a70")
assert not all(isinstance(c, UUID) for c in nested_coll.children)
Expand All @@ -247,6 +247,19 @@ def test_fill_uuids_entry_item(linac_backend: TestBackend):
original_depth = nest_depth(tree_model.root_item)
assert original_depth == 1

tree_model.root_item.fill_uuids(client)
new_depth = nest_depth(tree_model.root_item)
assert new_depth == 4
# fill just the first child
# fill depth can depend on how the backend returns data. Backend may not
# be lazy, so we assert only child1's children have EntryItems
root_item = tree_model.root_item
child1 = root_item.child(0)
assert child1.childCount() == 0
child1.fill_uuids(client)
assert child1.childCount() > 0
assert root_item.child(1).childCount() == 0
assert root_item.child(2).childCount() == 0

# filling the root item fills its children, which held uuids before
root_item.fill_uuids(client)
assert root_item.child(0).childCount() > 0
assert root_item.child(1).childCount() > 0
assert root_item.child(2).childCount() > 0
2 changes: 2 additions & 0 deletions superscore/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def get_last_index(index):

first_index = window.tree_view.model().index(0, 0)
last_index = get_last_index(first_index)
# expand does not fetch more data by itself
window.tree_view.expand(last_index)
window.tree_view.model().fetchMore(last_index)

assert count_visible_items(window.tree_view) == 7

Expand Down
16 changes: 13 additions & 3 deletions superscore/widgets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def fill_uuids(self, client: Optional[Client] = None) -> None:
if isinstance(self._data, Nestable):
if any(isinstance(child, UUID) for child in self._data.children):
client.fill(self._data, fill_depth=2)
# re-construct child nodes

# re-construct child EntryItems if there is a mismatch or if any
# hold UUIDs as _data
if (any(isinstance(e._data, UUID) for e in self._children)
or len(self._data.children) != self.childCount()):
self.takeChildren()
for child in self._data.children:
logger.debug(f'adding filled child: {type(child)}({child.uuid})')
Expand Down Expand Up @@ -224,10 +228,13 @@ def icon(self):
return qta.icon(icon_id)


def build_tree(entry: Entry, parent: Optional[EntryItem] = None) -> EntryItem:
def build_tree(
entry: Union[Entry, Root, UUID],
parent: Optional[EntryItem] = None
) -> EntryItem:
"""
Walk down the ``entry`` tree and create an `EntryItem` for each, linking
them to their parents
them to their parents.
Parameters
----------
Expand All @@ -242,6 +249,9 @@ def build_tree(entry: Entry, parent: Optional[EntryItem] = None) -> EntryItem:
EntryItem
the constructed `EntryItem` with parent-child linkages
"""
# Note the base case here is when ``entry`` is a UUID, in which an EntryItem
# is made and recursion stops. These children need to be present to let the
# view know that there are children in the item (that will later be filled)

item = EntryItem(entry, tree_parent=parent)
if isinstance(entry, Root):
Expand Down

0 comments on commit 42e01c8

Please sign in to comment.