| 1 |
from copy import deepcopy |
| 2 |
|
| 3 |
from anytree import Node, RenderTree |
| 4 |
from anytree.exporter import DictExporter |
| 5 |
from anytree.importer import DictImporter |
| 6 |
|
| 7 |
from .helper import eq_ |
| 8 |
|
| 9 |
|
| 10 |
def test_dict_importer(): |
| 11 |
"""Dict Importer.""" |
| 12 |
importer = DictImporter() |
| 13 |
exporter = DictExporter() |
| 14 |
refdata = { |
| 15 |
"id": "root", |
| 16 |
"children": [ |
| 17 |
{"id": "sub0", "children": [{"id": "sub0B"}, {"id": "sub0A"}]}, |
| 18 |
{ |
| 19 |
"id": "sub1", |
| 20 |
"children": [{"id": "sub1A"}, {"id": "sub1B"}, {"id": "sub1C", "children": [{"id": "sub1Ca"}]}], |
| 21 |
}, |
| 22 |
], |
| 23 |
} |
| 24 |
data = deepcopy(refdata) |
| 25 |
root = importer.import_(data) |
| 26 |
eq_(data, refdata) |
| 27 |
eq_(exporter.export(root), data) |
| 28 |
r = RenderTree(root) |
| 29 |
assert str(r).splitlines() == [ |
| 30 |
"AnyNode(id='root')", |
| 31 |
"├── AnyNode(id='sub0')", |
| 32 |
"│ ├── AnyNode(id='sub0B')", |
| 33 |
"│ └── AnyNode(id='sub0A')", |
| 34 |
"└── AnyNode(id='sub1')", |
| 35 |
" ├── AnyNode(id='sub1A')", |
| 36 |
" ├── AnyNode(id='sub1B')", |
| 37 |
" └── AnyNode(id='sub1C')", |
| 38 |
" └── AnyNode(id='sub1Ca')", |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
def test_dict_importer_node(): |
| 43 |
"""Dict Importer.""" |
| 44 |
importer = DictImporter(Node) |
| 45 |
exporter = DictExporter() |
| 46 |
refdata = { |
| 47 |
"name": "root", |
| 48 |
"children": [ |
| 49 |
{"name": "sub0", "children": [{"name": "sub0B"}, {"name": "sub0A"}]}, |
| 50 |
{ |
| 51 |
"name": "sub1", |
| 52 |
"children": [ |
| 53 |
{"name": "sub1A"}, |
| 54 |
{"name": "sub1B"}, |
| 55 |
{"name": "sub1C", "children": [{"name": "sub1Ca"}]}, |
| 56 |
], |
| 57 |
}, |
| 58 |
], |
| 59 |
} |
| 60 |
data = deepcopy(refdata) |
| 61 |
root = importer.import_(data) |
| 62 |
eq_(data, refdata) |
| 63 |
eq_(exporter.export(root), data) |
| 64 |
r = RenderTree(root) |
| 65 |
assert str(r).splitlines() == [ |
| 66 |
"Node('/root')", |
| 67 |
"├── Node('/root/sub0')", |
| 68 |
"│ ├── Node('/root/sub0/sub0B')", |
| 69 |
"│ └── Node('/root/sub0/sub0A')", |
| 70 |
"└── Node('/root/sub1')", |
| 71 |
" ├── Node('/root/sub1/sub1A')", |
| 72 |
" ├── Node('/root/sub1/sub1B')", |
| 73 |
" └── Node('/root/sub1/sub1C')", |
| 74 |
" └── Node('/root/sub1/sub1C/sub1Ca')", |
| 75 |
] |