|
1 | 1 | import unittest |
2 | 2 |
|
3 | 3 | import pandas as pd |
| 4 | +import polars as pl |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from bigtree.binarytree.construct import list_to_binarytree |
|
12 | 13 | list_to_tree, |
13 | 14 | list_to_tree_by_relation, |
14 | 15 | nested_dict_to_tree, |
| 16 | + polars_to_tree, |
| 17 | + polars_to_tree_by_relation, |
15 | 18 | ) |
16 | 19 | from tests.node.test_binarynode import assert_binarytree_structure_root2 |
17 | 20 | from tests.test_constants import Constants |
@@ -234,3 +237,62 @@ def tearDown(self): |
234 | 237 | def test_dataframe_to_tree_by_relation(self): |
235 | 238 | root = dataframe_to_tree_by_relation(self.relation_data, node_type=BinaryNode) |
236 | 239 | 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) |
0 commit comments