| 1 |
from anytree import AnyNode, Node, NodeMixin |
| 2 |
from anytree.exporter import DictExporter |
| 3 |
|
| 4 |
from .helper import eq_ |
| 5 |
|
| 6 |
|
| 7 |
def test_dict_exporter(): |
| 8 |
"""Dict Exporter.""" |
| 9 |
root = AnyNode(id="root") |
| 10 |
s0 = AnyNode(id="sub0", parent=root) |
| 11 |
s0b = AnyNode(id="sub0B", parent=s0) |
| 12 |
s0a = AnyNode(id="sub0A", parent=s0) |
| 13 |
s1 = AnyNode(id="sub1", parent=root, foo="bar") |
| 14 |
s1a = AnyNode(id="sub1A", parent=s1) |
| 15 |
s1b = AnyNode(id="sub1B", parent=s1) |
| 16 |
s1c = AnyNode(id="sub1C", parent=s1) |
| 17 |
s1ca = AnyNode(id="sub1Ca", parent=s1c) |
| 18 |
|
| 19 |
exporter = DictExporter() |
| 20 |
eq_( |
| 21 |
exporter.export(root), |
| 22 |
{ |
| 23 |
"id": "root", |
| 24 |
"children": [ |
| 25 |
{"id": "sub0", "children": [{"id": "sub0B"}, {"id": "sub0A"}]}, |
| 26 |
{ |
| 27 |
"id": "sub1", |
| 28 |
"foo": "bar", |
| 29 |
"children": [{"id": "sub1A"}, {"id": "sub1B"}, {"id": "sub1C", "children": [{"id": "sub1Ca"}]}], |
| 30 |
}, |
| 31 |
], |
| 32 |
}, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def test_dict_exporter_node(): |
| 37 |
"""Dict Exporter.""" |
| 38 |
root = Node("root") |
| 39 |
s0 = Node("sub0", parent=root) |
| 40 |
s0b = Node("sub0B", parent=s0) |
| 41 |
s0a = Node("sub0A", parent=s0) |
| 42 |
s1 = Node("sub1", parent=root, foo="bar") |
| 43 |
s1a = Node("sub1A", parent=s1) |
| 44 |
s1b = Node("sub1B", parent=s1) |
| 45 |
s1c = Node("sub1C", parent=s1) |
| 46 |
s1ca = Node("sub1Ca", parent=s1c) |
| 47 |
|
| 48 |
exporter = DictExporter() |
| 49 |
eq_( |
| 50 |
exporter.export(root), |
| 51 |
{ |
| 52 |
"name": "root", |
| 53 |
"children": [ |
| 54 |
{"name": "sub0", "children": [{"name": "sub0B"}, {"name": "sub0A"}]}, |
| 55 |
{ |
| 56 |
"name": "sub1", |
| 57 |
"foo": "bar", |
| 58 |
"children": [ |
| 59 |
{"name": "sub1A"}, |
| 60 |
{"name": "sub1B"}, |
| 61 |
{"name": "sub1C", "children": [{"name": "sub1Ca"}]}, |
| 62 |
], |
| 63 |
}, |
| 64 |
], |
| 65 |
}, |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
def test_dict_exporter_filter(): |
| 70 |
"""Dict Exporter.""" |
| 71 |
root = Node("root") |
| 72 |
s0 = Node("sub0", parent=root) |
| 73 |
s0b = Node("sub0B", parent=s0) |
| 74 |
s0a = Node("sub0A", parent=s0) |
| 75 |
s1 = Node("sub1", parent=root, foo="bar") |
| 76 |
s1a = Node("sub1A", parent=s1) |
| 77 |
s1b = Node("sub1B", parent=s1) |
| 78 |
s1c = Node("sub1C", parent=s1) |
| 79 |
s1ca = Node("sub1Ca", parent=s1c) |
| 80 |
|
| 81 |
exporter = DictExporter(attriter=lambda attrs: [(k, v) for k, v in attrs if k == "name"]) |
| 82 |
eq_( |
| 83 |
exporter.export(root), |
| 84 |
{ |
| 85 |
"name": "root", |
| 86 |
"children": [ |
| 87 |
{"name": "sub0", "children": [{"name": "sub0B"}, {"name": "sub0A"}]}, |
| 88 |
{ |
| 89 |
"name": "sub1", |
| 90 |
"children": [ |
| 91 |
{"name": "sub1A"}, |
| 92 |
{"name": "sub1B"}, |
| 93 |
{"name": "sub1C", "children": [{"name": "sub1Ca"}]}, |
| 94 |
], |
| 95 |
}, |
| 96 |
], |
| 97 |
}, |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
def test_dict_exporter_mixin(): |
| 102 |
"""Dict Exporter.""" |
| 103 |
|
| 104 |
class MyClass(NodeMixin): |
| 105 |
def __init__(self, foo, parent=None): |
| 106 |
super().__init__() |
| 107 |
self.foo = foo |
| 108 |
self.parent = parent |
| 109 |
|
| 110 |
root = MyClass("root") |
| 111 |
s0 = MyClass("s0", parent=root) |
| 112 |
s0b = MyClass("s0b", parent=s0) |
| 113 |
s0a = MyClass("s0a", parent=s0) |
| 114 |
s1 = MyClass("s1", parent=root) |
| 115 |
s1a = MyClass("s1a", parent=s1) |
| 116 |
s1b = MyClass("s1b", parent=s1) |
| 117 |
s1c = MyClass("s1c", parent=s1) |
| 118 |
s1ca = MyClass("s1ca", parent=s1c) |
| 119 |
|
| 120 |
exporter = DictExporter() |
| 121 |
eq_( |
| 122 |
exporter.export(root), |
| 123 |
{ |
| 124 |
"foo": "root", |
| 125 |
"children": [ |
| 126 |
{"foo": "s0", "children": [{"foo": "s0b"}, {"foo": "s0a"}]}, |
| 127 |
{ |
| 128 |
"foo": "s1", |
| 129 |
"children": [{"foo": "s1a"}, {"foo": "s1b"}, {"foo": "s1c", "children": [{"foo": "s1ca"}]}], |
| 130 |
}, |
| 131 |
], |
| 132 |
}, |
| 133 |
) |