| 1 |
from tempfile import NamedTemporaryFile |
| 2 |
|
| 3 |
from anytree.exporter import DictExporter |
| 4 |
from anytree.importer import JsonImporter |
| 5 |
|
| 6 |
from .helper import eq_ |
| 7 |
|
| 8 |
|
| 9 |
def test_json_importer(): |
| 10 |
"""Json Importer.""" |
| 11 |
refdata = { |
| 12 |
"id": "root", |
| 13 |
"children": [ |
| 14 |
{"id": "sub0", "children": [{"id": "sub0B"}, {"id": "sub0A"}]}, |
| 15 |
{ |
| 16 |
"id": "sub1", |
| 17 |
"children": [{"id": "sub1A"}, {"id": "sub1B"}, {"id": "sub1C", "children": [{"id": "sub1Ca"}]}], |
| 18 |
}, |
| 19 |
], |
| 20 |
} |
| 21 |
lines = [ |
| 22 |
"{", |
| 23 |
' "children": [', |
| 24 |
" {", |
| 25 |
' "children": [', |
| 26 |
" {", |
| 27 |
' "id": "sub0B"', |
| 28 |
" },", |
| 29 |
" {", |
| 30 |
' "id": "sub0A"', |
| 31 |
" }", |
| 32 |
" ],", |
| 33 |
' "id": "sub0"', |
| 34 |
" },", |
| 35 |
" {", |
| 36 |
' "children": [', |
| 37 |
" {", |
| 38 |
' "id": "sub1A"', |
| 39 |
" },", |
| 40 |
" {", |
| 41 |
' "id": "sub1B"', |
| 42 |
" },", |
| 43 |
" {", |
| 44 |
' "children": [', |
| 45 |
" {", |
| 46 |
' "id": "sub1Ca"', |
| 47 |
" }", |
| 48 |
" ],", |
| 49 |
' "id": "sub1C"', |
| 50 |
" }", |
| 51 |
" ],", |
| 52 |
' "id": "sub1"', |
| 53 |
" }", |
| 54 |
" ],", |
| 55 |
' "id": "root"', |
| 56 |
"}", |
| 57 |
] |
| 58 |
|
| 59 |
imported = DictExporter().export(JsonImporter().import_("\n".join(lines))) |
| 60 |
eq_(refdata, imported) |
| 61 |
with NamedTemporaryFile(mode="w+") as ref: |
| 62 |
ref.write("\n".join(lines)) |
| 63 |
ref.seek(0) |
| 64 |
imported = DictExporter().export(JsonImporter().read(ref)) |
| 65 |
eq_(refdata, imported) |