File: test_jsonexporter.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 (93 lines) | stat: -rw-r--r-- 2,540 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import filecmp
import os
from tempfile import NamedTemporaryFile

from anytree import AnyNode
from anytree.exporter import JsonExporter

from .helper import eq_


def test_json_exporter():
    """Json Exporter."""
    root = AnyNode(id="root")
    s0 = AnyNode(id="sub0", parent=root)
    AnyNode(id="sub0B", parent=s0)
    AnyNode(id="sub0A", parent=s0)
    s1 = AnyNode(id="sub1", parent=root)
    AnyNode(id="sub1A", parent=s1)
    AnyNode(id="sub1B", parent=s1)
    s1c = AnyNode(id="sub1C", parent=s1)
    AnyNode(id="sub1Ca", parent=s1c)

    exporter = JsonExporter(indent=2, sort_keys=True)
    exported = exporter.export(root).splitlines()
    exported = [e.rstrip() for e in exported]  # just a fix for a strange py2x behavior.
    lines = [
        "{",
        '  "children": [',
        "    {",
        '      "children": [',
        "        {",
        '          "id": "sub0B"',
        "        },",
        "        {",
        '          "id": "sub0A"',
        "        }",
        "      ],",
        '      "id": "sub0"',
        "    },",
        "    {",
        '      "children": [',
        "        {",
        '          "id": "sub1A"',
        "        },",
        "        {",
        '          "id": "sub1B"',
        "        },",
        "        {",
        '          "children": [',
        "            {",
        '              "id": "sub1Ca"',
        "            }",
        "          ],",
        '          "id": "sub1C"',
        "        }",
        "      ],",
        '      "id": "sub1"',
        "    }",
        "  ],",
        '  "id": "root"',
        "}",
    ]
    eq_(exported, lines)

    exporter = JsonExporter(indent=2, sort_keys=True, maxlevel=2)
    exported = exporter.export(root).splitlines()
    exported = [e.rstrip() for e in exported]  # just a fix for a strange py2x behavior.
    limitedlines = [
        "{",
        '  "children": [',
        "    {",
        '      "id": "sub0"',
        "    },",
        "    {",
        '      "id": "sub1"',
        "    }",
        "  ],",
        '  "id": "root"',
        "}",
    ]

    eq_(exported, limitedlines)

    try:
        with NamedTemporaryFile(mode="w+", delete=False) as ref:
            with NamedTemporaryFile(mode="w+", delete=False) as gen:
                ref.write("\n".join(lines))
                exporter.write(root, gen)
        # on Windows, you must close the files before comparison
        filecmp.cmp(ref.name, gen.name)
    finally:
        os.remove(ref.name)
        os.remove(gen.name)