File: test_json.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (169 lines) | stat: -rw-r--r-- 5,749 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import json
import warnings
from unittest.case import TestCase
from unittest.mock import ANY, Mock, call

from tests.fixtures.books import BookForm, Books
from tests.fixtures.datatypes import Telephone
from xsdata.exceptions import XmlContextError
from xsdata.formats.dataclass.serializers.json import DictFactory, JsonSerializer
from xsdata.models.datatype import XmlDate
from xsdata.models.xsd import Attribute
from xsdata.utils.testing import XmlVarFactory


class JsonSerializerTests(TestCase):
    def setUp(self):
        super().setUp()
        self.books = Books(
            book=[
                BookForm(
                    id="bk001",
                    author="Hightower, Kim",
                    title="The First Book",
                    genre="Fiction",
                    price=44.95,
                    pub_date=XmlDate.from_string("2000-10-01"),
                    review="An amazing story of nothing.",
                ),
                BookForm(
                    id="bk002",
                    author="Nagata, Suanne",
                    title="Becoming Somebody",
                    genre="Biography",
                    review="A masterpiece of the fine art of gossiping.",
                ),
            ]
        )

        self.expected = {
            "book": [
                {
                    "author": "Hightower, Kim",
                    "genre": "Fiction",
                    "id": "bk001",
                    "lang": "en",
                    "price": 44.95,
                    "pub_date": "2000-10-01",
                    "review": "An amazing story of nothing.",
                    "title": "The First Book",
                },
                {
                    "author": "Nagata, Suanne",
                    "genre": "Biography",
                    "id": "bk002",
                    "lang": "en",
                    "review": "A masterpiece of the fine art of gossiping.",
                    "title": "Becoming Somebody",
                },
            ]
        }

    def test_render(self):
        serializer = JsonSerializer(dict_factory=DictFactory.FILTER_NONE)
        actual = serializer.render(self.books)

        self.assertEqual(self.expected, json.loads(actual))

    def test_render_a_none_dataclass_object(self):
        with self.assertRaises(XmlContextError):
            JsonSerializer().render(1)

    def test_render_list_of_objects(self):
        serializer = JsonSerializer(dict_factory=DictFactory.FILTER_NONE)
        actual = serializer.render(self.books.book)
        self.assertEqual(self.expected["book"], json.loads(actual))

    def test_render_with_enum(self):
        obj = Attribute()
        serializer = JsonSerializer(dict_factory=DictFactory.FILTER_NONE)
        actual = json.loads(serializer.render(obj))

        self.assertEqual("optional", actual["use"])

    def test_convert_namedtuple(self):
        var = XmlVarFactory.create(types=(Telephone,))
        serializer = JsonSerializer(dict_factory=DictFactory.FILTER_NONE)
        actual = serializer.convert(Telephone(30, 234, 56783), var)
        self.assertEqual("30-234-56783", actual)

    def test_indent_deprecation(self):
        dump_factory = Mock(json.dump)

        with warnings.catch_warnings(record=True) as w:
            serializer = JsonSerializer(dump_factory=dump_factory)
            serializer.render(self.books)

            serializer.config.pretty_print = True
            serializer.render(self.books)

            serializer.indent = 4
            serializer.render(self.books)

        dump_factory.assert_has_calls(
            [
                call(ANY, ANY, indent=None),
                call(ANY, ANY, indent=2),
                call(ANY, ANY, indent=4),
            ]
        )

        self.assertEqual(
            "JsonSerializer indent property is deprecated, use SerializerConfig",
            str(w[-1].message),
        )

    def test_next_value(self):
        book = self.books.book[0]
        serializer = JsonSerializer()

        actual = [name for name, value in serializer.next_value(book)]
        expected = [
            "author",
            "title",
            "genre",
            "price",
            "pub_date",
            "review",
            "id",
            "lang",
        ]
        self.assertEqual(expected, actual)

        serializer.config.ignore_default_attributes = True
        expected = expected[:-1]
        actual = [name for name, value in serializer.next_value(book)]
        self.assertEqual(expected, actual)

    def test_pretty_print_indent(self):
        serializer = JsonSerializer()
        serializer.config.pretty_print = True
        serializer.config.pretty_print_indent = "    "

        actual = serializer.render(self.books)
        expected = f"""{{
    "book": [
        {{
            "author": "{self.expected["book"][0]["author"]}",
            "title": "{self.expected["book"][0]["title"]}",
            "genre": "{self.expected["book"][0]["genre"]}",
            "price": {self.expected["book"][0]["price"]},
            "pub_date": "{self.expected["book"][0]["pub_date"]}",
            "review": "{self.expected["book"][0]["review"]}",
            "id": "{self.expected["book"][0]["id"]}",
            "lang": "en"
        }},
        {{
            "author": "{self.expected["book"][1]["author"]}",
            "title": "{self.expected["book"][1]["title"]}",
            "genre": "{self.expected["book"][1]["genre"]}",
            "price": null,
            "pub_date": null,
            "review": "{self.expected["book"][1]["review"]}",
            "id": "{self.expected["book"][1]["id"]}",
            "lang": "en"
        }}
    ]
}}"""

        self.assertEqual(expected, actual)