File: test_json.py

package info (click to toggle)
python-proto-plus 1.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: python: 4,649; makefile: 27
file content (265 lines) | stat: -rw-r--r-- 7,180 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (C) 2020  Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import re

import proto
from google.protobuf.json_format import MessageToJson, Parse, ParseError


def test_message_to_json():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)

    s = Squid(mass_kg=100)
    json = Squid.to_json(s)
    json = json.replace(" ", "").replace("\n", "")
    assert json == '{"massKg":100}'


def test_message_to_json_no_indent():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)
        name = proto.Field(proto.STRING, number=2)

    s = Squid(mass_kg=100, name="no_new_lines_squid")
    json = Squid.to_json(s, indent=None)
    assert json == '{"massKg": 100, "name": "no_new_lines_squid"}'


def test_message_from_json():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)

    json = """{
    "massKg": 100
    }
    """

    s = Squid.from_json(json)
    assert s == Squid(mass_kg=100)


def test_message_json_round_trip():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)

    s = Squid(mass_kg=100)
    json = Squid.to_json(s)
    s2 = Squid.from_json(json)

    assert s == s2


def test_json_stringy_enums():
    class Squid(proto.Message):
        zone = proto.Field(proto.ENUM, number=1, enum="Zone")

    class Zone(proto.Enum):
        EPIPELAGIC = 0
        MESOPELAGIC = 1
        BATHYPELAGIC = 2
        ABYSSOPELAGIC = 3

    s1 = Squid(zone=Zone.MESOPELAGIC)
    json = (
        Squid.to_json(s1, use_integers_for_enums=False)
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json == '{"zone":"MESOPELAGIC"}'

    s2 = Squid.from_json(json)
    assert s2.zone == s1.zone


def test_json_default_enums():
    class Squid(proto.Message):
        zone = proto.Field(proto.ENUM, number=1, enum="Zone")

    class Zone(proto.Enum):
        EPIPELAGIC = 0
        MESOPELAGIC = 1
        BATHYPELAGIC = 2
        ABYSSOPELAGIC = 3

    s = Squid()
    assert s.zone == Zone.EPIPELAGIC
    json1 = Squid.to_json(s).replace(" ", "").replace("\n", "")
    assert json1 == '{"zone":0}'

    json2 = (
        Squid.to_json(s, use_integers_for_enums=False)
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json2 == '{"zone":"EPIPELAGIC"}'


def test_json_default_values():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)
        name = proto.Field(proto.STRING, number=2)

    s = Squid(name="Steve")
    json1 = (
        Squid.to_json(s, including_default_value_fields=False)
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json1 == '{"name":"Steve"}'

    json1 = (
        Squid.to_json(s, always_print_fields_with_no_presence=False)
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json1 == '{"name":"Steve"}'

    json1 = (
        Squid.to_json(
            s,
            including_default_value_fields=False,
            always_print_fields_with_no_presence=False,
        )
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json1 == '{"name":"Steve"}'

    with pytest.raises(
        ValueError,
        match="Arguments.*always_print_fields_with_no_presence.*including_default_value_fields.*must match",
    ):
        Squid.to_json(
            s,
            including_default_value_fields=True,
            always_print_fields_with_no_presence=False,
        ).replace(" ", "").replace("\n", "")

    with pytest.raises(
        ValueError,
        match="Arguments.*always_print_fields_with_no_presence.*including_default_value_fields.*must match",
    ):
        Squid.to_json(
            s,
            including_default_value_fields=False,
            always_print_fields_with_no_presence=True,
        ).replace(" ", "").replace("\n", "")

    json2 = (
        Squid.to_json(
            s,
            including_default_value_fields=True,
            always_print_fields_with_no_presence=True,
        )
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json2 == '{"name":"Steve","massKg":0}'

    json2 = (
        Squid.to_json(
            s,
            including_default_value_fields=True,
        )
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json2 == '{"name":"Steve","massKg":0}'

    json2 = (
        Squid.to_json(
            s,
            always_print_fields_with_no_presence=True,
        )
        .replace(" ", "")
        .replace("\n", "")
    )
    assert json2 == '{"name":"Steve","massKg":0}'

    json2 = Squid.to_json(s).replace(" ", "").replace("\n", "")
    assert json2 == '{"name":"Steve","massKg":0}'

    s1 = Squid.from_json(json1)
    s2 = Squid.from_json(json2)
    assert s == s1 == s2


def test_json_unknown_field():
    # Note that 'lengthCm' is unknown in the local definition.
    # This could happen if the client is using an older proto definition
    # than the server.
    json_str = '{\n  "massKg": 20,\n  "lengthCm": 100\n}'

    class Octopus(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)

    o = Octopus.from_json(json_str, ignore_unknown_fields=True)
    assert not hasattr(o, "length_cm")
    assert not hasattr(o, "lengthCm")

    # Don't permit unknown fields by default
    with pytest.raises(ParseError):
        o = Octopus.from_json(json_str)


def test_json_snake_case():
    class Squid(proto.Message):
        mass_kg = proto.Field(proto.INT32, number=1)

    json_str = '{\n  "mass_kg": 20\n}'
    s = Squid.from_json(json_str)

    assert s.mass_kg == 20

    assert Squid.to_json(s, preserving_proto_field_name=True) == json_str


def test_json_name():
    class Squid(proto.Message):
        massKg = proto.Field(proto.INT32, number=1, json_name="mass_in_kilograms")

    s = Squid(massKg=20)
    j = Squid.to_json(s)

    assert "mass_in_kilograms" in j

    s_two = Squid.from_json(j)

    assert s == s_two


def test_json_sort_keys():
    class Squid(proto.Message):
        name = proto.Field(proto.STRING, number=1)
        mass_kg = proto.Field(proto.INT32, number=2)

    s = Squid(name="Steve", mass_kg=20)
    j = Squid.to_json(s, sort_keys=True, indent=None)

    assert re.search(r"massKg.*name", j)


# TODO: https://github.com/googleapis/proto-plus-python/issues/390
def test_json_float_precision():
    class Squid(proto.Message):
        name = proto.Field(proto.STRING, number=1)
        mass_kg = proto.Field(proto.FLOAT, number=2)

    s = Squid(name="Steve", mass_kg=3.14159265)
    j = Squid.to_json(s, float_precision=3, indent=None)

    assert j == '{"name": "Steve", "massKg": 3.14}'