Commit 038aa6d9 authored by Trond Aasan's avatar Trond Aasan
Browse files

Fix Tree.__repr__ and refactor and add tests and

parent ce4586cb
Pipeline #86662 passed with stage
in 46 seconds
......@@ -44,7 +44,10 @@ class Tree(Generic[T]):
yield v
def __repr__(self):
return f"Tree(data={self.data}, children={self.children})"
children = ", ".join(list(map(repr, self.children)))
return (
f"Tree(data={self.data}, id_key={repr(self.id_key)}, children=[{children}])"
)
def __eq__(self, other):
return all(
......
import itertools
from typing import NamedTuple, Optional, List
import pytest
......@@ -66,6 +67,87 @@ def node_with_id_equals_parent_id():
return Node(id=20001, parent_id=20001)
@pytest.fixture
def nodes(
node_1,
node_2,
node_3,
node_4,
node_5,
):
return [
node_1,
node_2,
node_3,
node_4,
node_5,
]
@pytest.fixture
def nodes_x(
node_x_1,
node_x_2,
):
return [
node_x_1,
node_x_2,
]
@pytest.fixture
def tree_1(
node_1,
node_2,
node_3,
node_4,
node_5,
):
t_5 = Tree(data=node_5, id_key="id", children=[])
t_4 = Tree(data=node_4, id_key="id", children=[t_5])
t_3 = Tree(data=node_3, id_key="id", children=[])
t_2 = Tree(data=node_2, id_key="id", children=[t_4])
t_1 = Tree(data=node_1, id_key="id", children=[t_2, t_3])
t_2.parent = t_1
t_3.parent = t_1
t_4.parent = t_2
t_5.parent = t_4
return [t_1, t_2, t_3, t_4, t_5]
@pytest.fixture
def tree_x1(
node_x_1,
node_x_2,
):
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
t_x_2.parent = t_x_1
return [t_x_1, t_x_2]
@pytest.fixture
def tree_with_loop(
node_x_1,
node_x_2,
):
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
t_x_2.parent = t_x_1
t_x_1.parent = t_x_2
return [t_x_1, t_x_2]
@pytest.fixture
def forest(tree_1, tree_x1):
return {x.id: x for x in [*tree_1, *tree_x1]}
def test_build_forest_loop_parent_equals_key(
node_1,
node_with_id_equals_parent_id,
......@@ -117,57 +199,34 @@ def test_build_forest_parent_not_found(
_build_forest(items)
def test_build_forest(
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
):
items = [
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
]
t_5 = Tree(data=node_5, id_key="id", children=[])
t_4 = Tree(data=node_4, id_key="id", children=[t_5])
t_3 = Tree(data=node_3, id_key="id", children=[])
t_2 = Tree(data=node_2, id_key="id", children=[t_4])
t_1 = Tree(data=node_1, id_key="id", children=[t_2, t_3])
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
def test_build_forest(nodes, nodes_x, forest):
forest = _build_forest(items)
x = _build_forest([*nodes, *nodes_x])
assert forest == {x.id: x for x in [t_1, t_2, t_3, t_4, t_5, t_x_1, t_x_2]}
assert x == forest
def test_tree_iter(
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
):
items = [
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
]
def test_tree_iter(tree_1):
xs = list(sorted(tree_1[0], key=lambda x: x.id))
assert xs == tree_1
def test_tree_values(tree_1, nodes):
xs = list(sorted(tree_1[0].values(), key=lambda x: x.id))
assert xs == nodes
def test_depth(tree_1):
assert tree_1[0].depth == 1
assert tree_1[1].depth == 2
def test_depth_throws_loop_detected(tree_with_loop):
with pytest.raises(LoopDetected):
tree_with_loop[0].depth # noqa
forest = _build_forest(items)
forest_data = sorted(map(lambda x: x.data, forest.values()), key=lambda x: x.id)
assert items == forest_data
def test___repr__(tree_1):
assert eval(repr(tree_1)) == tree_1
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment