Skip to content
Open
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
2 changes: 1 addition & 1 deletion pageindex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def structure_to_list(structure):

def get_leaf_nodes(structure):
if isinstance(structure, dict):
if not structure['nodes']:
if not structure.get('nodes'):
structure_node = copy.deepcopy(structure)
structure_node.pop('nodes', None)
return [structure_node]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_issue_330.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import sys

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from pageindex.utils import get_leaf_nodes, list_to_tree


class TestIssue330GetLeafNodes:
def test_leaf_node_without_nodes_key(self):
leaf = {"title": "Section", "start_index": 1, "end_index": 1}
result = get_leaf_nodes(leaf)
assert len(result) == 1
assert result[0]["title"] == "Section"
assert "nodes" not in result[0]

def test_tree_from_list_to_tree(self):
flat = [
{"structure": "1", "title": "Root", "start_index": 1, "end_index": 2},
{"structure": "1.1", "title": "Child", "start_index": 2, "end_index": 2},
]
tree = list_to_tree(flat)
leaves = get_leaf_nodes(tree)
titles = {node["title"] for node in leaves}
assert titles == {"Child"}