File: test_jsonimporter.py

package info (click to toggle)
anytree 2.12.1-3.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: python: 4,044; makefile: 12
file content (65 lines) | stat: -rw-r--r-- 1,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from tempfile import NamedTemporaryFile

from anytree.exporter import DictExporter
from anytree.importer import JsonImporter

from .helper import eq_


def test_json_importer():
    """Json Importer."""
    refdata = {
        "id": "root",
        "children": [
            {"id": "sub0", "children": [{"id": "sub0B"}, {"id": "sub0A"}]},
            {
                "id": "sub1",
                "children": [{"id": "sub1A"}, {"id": "sub1B"}, {"id": "sub1C", "children": [{"id": "sub1Ca"}]}],
            },
        ],
    }
    lines = [
        "{",
        '  "children": [',
        "    {",
        '      "children": [',
        "        {",
        '          "id": "sub0B"',
        "        },",
        "        {",
        '          "id": "sub0A"',
        "        }",
        "      ],",
        '      "id": "sub0"',
        "    },",
        "    {",
        '      "children": [',
        "        {",
        '          "id": "sub1A"',
        "        },",
        "        {",
        '          "id": "sub1B"',
        "        },",
        "        {",
        '          "children": [',
        "            {",
        '              "id": "sub1Ca"',
        "            }",
        "          ],",
        '          "id": "sub1C"',
        "        }",
        "      ],",
        '      "id": "sub1"',
        "    }",
        "  ],",
        '  "id": "root"',
        "}",
    ]

    imported = DictExporter().export(JsonImporter().import_("\n".join(lines)))
    eq_(refdata, imported)
    with NamedTemporaryFile(mode="w+") as ref:
        ref.write("\n".join(lines))
        ref.seek(0)
        imported = DictExporter().export(JsonImporter().read(ref))
    eq_(refdata, imported)