|
| 1 | +from collections import namedtuple |
| 2 | + |
| 3 | +import pytest |
| 4 | +from easydict import EasyDict |
| 5 | + |
| 6 | +from treevalue import generic_flatten, generic_unflatten, FastTreeValue, register_integrate_container, generic_mapping |
| 7 | + |
| 8 | +nt = namedtuple('nt', ['a', 'b']) |
| 9 | + |
| 10 | + |
| 11 | +class MyTreeValue(FastTreeValue): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.unittest |
| 16 | +class TestTreeIntegrationGeneral: |
| 17 | + def test_general_flatten_and_unflatten(self): |
| 18 | + demo_data = { |
| 19 | + 'a': 1, |
| 20 | + 'b': [2, 3, 'f'], |
| 21 | + 'c': (2, 5, 'ds', EasyDict({ |
| 22 | + 'x': None, |
| 23 | + 'z': [34, '1.2'], |
| 24 | + })), |
| 25 | + 'd': nt('f', 100), |
| 26 | + 'e': MyTreeValue({'x': 1, 'y': 'dsfljk'}) |
| 27 | + } |
| 28 | + v, spec = generic_flatten(demo_data) |
| 29 | + assert v == [1, [2, 3, 'f'], [2, 5, 'ds', [None, [34, '1.2']]], ['f', 100], [1, 'dsfljk']] |
| 30 | + |
| 31 | + rv = generic_unflatten(v, spec) |
| 32 | + assert rv == demo_data |
| 33 | + assert isinstance(rv['c'][-1], EasyDict) |
| 34 | + assert isinstance(rv['d'], nt) |
| 35 | + assert isinstance(rv['c'][-1]['z'], list) |
| 36 | + assert isinstance(rv['e'], MyTreeValue) |
| 37 | + |
| 38 | + def test_register_my_class(self): |
| 39 | + class MyDC: |
| 40 | + def __init__(self, x, y): |
| 41 | + self.x = x |
| 42 | + self.y = y |
| 43 | + |
| 44 | + def __eq__(self, other): |
| 45 | + return isinstance(other, MyDC) and self.x == other.x and self.y == other.y |
| 46 | + |
| 47 | + def _mydc_flatten(v): |
| 48 | + return [v.x, v.y], MyDC |
| 49 | + |
| 50 | + def _mydc_unflatten(v, spec): |
| 51 | + return spec(*v) |
| 52 | + |
| 53 | + register_integrate_container(MyDC, _mydc_flatten, _mydc_unflatten) |
| 54 | + |
| 55 | + demo_data = { |
| 56 | + 'a': 1, |
| 57 | + 'b': [2, 3, 'f'], |
| 58 | + 'c': (2, 5, 'ds', EasyDict({ |
| 59 | + 'x': None, |
| 60 | + 'z': MyDC(34, '1.2'), |
| 61 | + })), |
| 62 | + 'd': nt('f', 100), |
| 63 | + 'e': MyTreeValue({'x': 1, 'y': 'dsfljk'}) |
| 64 | + } |
| 65 | + v, spec = generic_flatten(demo_data) |
| 66 | + assert v == [1, [2, 3, 'f'], [2, 5, 'ds', [None, [34, '1.2']]], ['f', 100], [1, 'dsfljk']] |
| 67 | + |
| 68 | + rv = generic_unflatten(v, spec) |
| 69 | + assert rv == demo_data |
| 70 | + assert isinstance(rv['c'][-1], EasyDict) |
| 71 | + assert isinstance(rv['d'], nt) |
| 72 | + assert isinstance(rv['c'][-1]['z'], MyDC) |
| 73 | + assert isinstance(rv['e'], MyTreeValue) |
| 74 | + |
| 75 | + def test_generic_mapping(self): |
| 76 | + demo_data = { |
| 77 | + 'a': 1, |
| 78 | + 'b': [2, 3, 'f'], |
| 79 | + 'c': (2, 5, 'ds', EasyDict({ |
| 80 | + 'x': None, |
| 81 | + 'z': (34, '1.2'), |
| 82 | + })), |
| 83 | + 'd': nt('f', 100), |
| 84 | + 'e': MyTreeValue({'x': 1, 'y': 'dsfljk'}) |
| 85 | + } |
| 86 | + assert generic_mapping(demo_data, str) == { |
| 87 | + 'a': '1', |
| 88 | + 'b': ['2', '3', 'f'], |
| 89 | + 'c': ('2', '5', 'ds', EasyDict({ |
| 90 | + 'x': 'None', |
| 91 | + 'z': ('34', '1.2'), |
| 92 | + })), |
| 93 | + 'd': nt('f', '100'), |
| 94 | + 'e': MyTreeValue({'x': '1', 'y': 'dsfljk'}) |
| 95 | + } |
0 commit comments