Skip to content

Commit a29813f

Browse files
committed
test: Add polars test for binary tree
1 parent 3d059f7 commit a29813f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

tests/binarytree/test_construct.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

33
import pandas as pd
4+
import polars as pl
45
import pytest
56

67
from bigtree.binarytree.construct import list_to_binarytree
@@ -12,6 +13,8 @@
1213
list_to_tree,
1314
list_to_tree_by_relation,
1415
nested_dict_to_tree,
16+
polars_to_tree,
17+
polars_to_tree_by_relation,
1518
)
1619
from tests.node.test_binarynode import assert_binarytree_structure_root2
1720
from tests.test_constants import Constants
@@ -234,3 +237,62 @@ def tearDown(self):
234237
def test_dataframe_to_tree_by_relation(self):
235238
root = dataframe_to_tree_by_relation(self.relation_data, node_type=BinaryNode)
236239
assert_binarytree_structure_root2(root)
240+
241+
242+
class TestPolarsToTree(unittest.TestCase):
243+
def setUp(self):
244+
"""
245+
Binary Tree should have structure
246+
1
247+
├── 2
248+
│ ├── 4
249+
│ │ └── 8
250+
│ └── 5
251+
└── 3
252+
├── 6
253+
└── 7
254+
"""
255+
self.path_data = pl.DataFrame(
256+
[
257+
["1", 90],
258+
["1/2", 65],
259+
["1/3", 60],
260+
["1/2/4", 40],
261+
["1/2/5", 35],
262+
["1/3/6", 38],
263+
["1/3/7", 10],
264+
["1/2/4/8", 6],
265+
],
266+
schema=["PATH", "age"],
267+
)
268+
269+
def tearDown(self):
270+
self.path_data = None
271+
272+
def test_polars_to_tree(self):
273+
root = polars_to_tree(self.path_data, node_type=BinaryNode)
274+
assert_binarytree_structure_root2(root)
275+
276+
277+
class TestPolarsToTreeByRelation(unittest.TestCase):
278+
def setUp(self):
279+
self.relation_data = pl.DataFrame(
280+
[
281+
["1", None, 90],
282+
["2", "1", 65],
283+
["3", "1", 60],
284+
["4", "2", 40],
285+
["5", "2", 35],
286+
["6", "3", 38],
287+
["7", "3", 10],
288+
["8", "4", 6],
289+
],
290+
schema=["child", "parent", "age"],
291+
)
292+
293+
def tearDown(self):
294+
self.relation_data = None
295+
296+
def test_polars_to_tree_by_relation(self):
297+
root = polars_to_tree_by_relation(self.relation_data, node_type=BinaryNode)
298+
assert_binarytree_structure_root2(root)

tests/binarytree/test_export.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pandas as pd
2+
import polars as pl
23

34
from bigtree.tree.export import (
45
print_tree,
@@ -7,6 +8,7 @@
78
tree_to_dot,
89
tree_to_nested_dict,
910
tree_to_newick,
11+
tree_to_polars,
1012
)
1113
from tests.conftest import assert_print_statement
1214
from tests.test_constants import Constants
@@ -41,6 +43,26 @@ def test_tree_to_dataframe(binarytree_node):
4143
pd.testing.assert_frame_equal(expected, actual)
4244

4345

46+
class TestTreeToPolars:
47+
@staticmethod
48+
def test_tree_to_polars(binarytree_node):
49+
expected = pl.DataFrame(
50+
[
51+
["/1", "1"],
52+
["/1/2", "2"],
53+
["/1/2/4", "4"],
54+
["/1/2/4/8", "8"],
55+
["/1/2/5", "5"],
56+
["/1/3", "3"],
57+
["/1/3/6", "6"],
58+
["/1/3/7", "7"],
59+
],
60+
schema=["path", "name"],
61+
)
62+
actual = tree_to_polars(binarytree_node)
63+
assert expected.equals(actual)
64+
65+
4466
class TestTreeToDict:
4567
@staticmethod
4668
def test_tree_to_dict(binarytree_node):

0 commit comments

Comments
 (0)