File: test_json_encoder.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (405 lines) | stat: -rw-r--r-- 19,800 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import functools
import pytest
import json

try:
    import jsonschema
except ImportError:
    pass
from typing import Mapping, Any

from azure.core.exceptions import ResourceNotFoundError
from azure.schemaregistry import SchemaRegistryClient, SchemaFormat
from azure.schemaregistry.encoder.jsonencoder import JsonSchemaEncoder, InvalidContentError
from devtools_testutils import (
    AzureRecordedTestCase,
    EnvironmentVariableLoader,
    recorded_by_proxy,
)

SchemaRegistryEnvironmentVariableLoader = functools.partial(
    EnvironmentVariableLoader,
    "schemaregistry",
    schemaregistry_json_fully_qualified_namespace="fake_resource_json.servicebus.windows.net",
    schemaregistry_group="fakegroup",
)

DRAFT2020_12_SCHEMA_JSON = {
    "$id": "https://example.com/person.schema.json",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Person",
    "type": "object",
    "properties": {
        "name": {"type": "string", "description": "Person's name."},
        "favorite_color": {"type": "string", "description": "Favorite color."},
        "favorite_number": {
            "description": "Favorite number.",
            "type": "integer",
        },
    },
}
DRAFT2020_12_SCHEMA_STR = json.dumps(DRAFT2020_12_SCHEMA_JSON)
DRAFT2020_12_SCHEMA_IDENTIFIER = "https://json-schema.org/draft/2020-12/schema"

DRAFT07_SCHEMA_JSON = {
    "$id": "https://example.com/persondraft07.schema.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "PersonDraft07",
    "type": "object",
    "properties": {
        "name": {"type": "string", "description": "Person's name."},
        "favorite_color": {"type": "string", "description": "Favorite color."},
        "favorite_number": {
            "description": "Favorite number.",
            "type": "integer",
        },
        "org": {"type": "string", "description": "Organization name."},
    },
}
DRAFT07_SCHEMA_STR = json.dumps(DRAFT07_SCHEMA_JSON)
DRAFT07_SCHEMA_IDENTIFIER = "http://json-schema.org/draft-07/schema"
DRAFT06_SCHEMA_IDENTIFIER = "http://json-schema.org/draft-06/schema"
DRAFT04_SCHEMA_IDENTIFIER = "http://json-schema.org/draft-04/schema"
DRAFT03_SCHEMA_IDENTIFIER = "http://json-schema.org/draft-03/schema"
DRAFT2019_09_SCHEMA_IDENTIFIER = "https://json-schema.org/draft/2019-09/schema"


class TestJsonSchemaEncoder(AzureRecordedTestCase):
    def create_client(self, **kwargs) -> SchemaRegistryClient:
        fully_qualified_namespace = kwargs.pop("fully_qualified_namespace")
        credential = self.get_credential(SchemaRegistryClient)
        return self.create_client_from_credential(
            SchemaRegistryClient, credential, fully_qualified_namespace=fully_qualified_namespace
        )

    @SchemaRegistryEnvironmentVariableLoader()
    @recorded_by_proxy
    def test_sr_json_encoder_encode_decode_w_schema_id(self, **kwargs):
        schemaregistry_json_fully_qualified_namespace = kwargs.pop("schemaregistry_json_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)
        sr_json_encoder = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT2020_12_SCHEMA_IDENTIFIER
        )

        # pre-register schema
        schema_properties = sr_client.register_schema(
            schemaregistry_group, DRAFT2020_12_SCHEMA_JSON["title"], DRAFT2020_12_SCHEMA_STR, SchemaFormat.JSON
        )
        schema_id = schema_properties.id

        # encode + validate w/ schema_id
        dict_content = {"name": "Ben", "favorite_number": 7, "favorite_color": "red"}
        encoded_message_content = sr_json_encoder.encode(dict_content, schema_id=schema_id)
        content_type = encoded_message_content["content_type"]
        encoded_content = encoded_message_content["content"]
        assert content_type.split("+")[0] == "application/json;serialization=Json"
        assert content_type.split("+")[1] == schema_id

        # passing both schema + schema_id to encode raises error
        with pytest.raises(TypeError):
            encoded_message_content = sr_json_encoder.encode(
                dict_content, schema_id=schema_id, schema=DRAFT2020_12_SCHEMA_STR
            )

        # decode
        encoded_content_dict = {"content": encoded_content, "content_type": content_type}
        decoded_content = sr_json_encoder.decode(encoded_content_dict)
        assert decoded_content["name"] == "Ben"
        assert decoded_content["favorite_number"] == 7
        assert decoded_content["favorite_color"] == "red"

        # bad content type when decoding raises error
        mime_type, schema_id = encoded_content_dict["content_type"].split("+")
        encoded_content_dict["content_type"] = "binary/fake+" + schema_id
        with pytest.raises(InvalidContentError) as e:
            decoded_content = sr_json_encoder.decode(encoded_content_dict)

        encoded_content_dict["content_type"] = "a+b+c"
        with pytest.raises(InvalidContentError) as e:
            decoded_content = sr_json_encoder.decode(encoded_content_dict)

    @SchemaRegistryEnvironmentVariableLoader()
    @recorded_by_proxy
    def test_sr_json_encoder_encode_schema_str(self, **kwargs):
        schemaregistry_json_fully_qualified_namespace = kwargs.pop("schemaregistry_json_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)
        sr_json_encoder = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT07_SCHEMA_IDENTIFIER
        )

        # encoding without registering schema raises error
        dict_content = {"name": "Ben", "favorite_number": 7, "favorite_color": "red", "org": "Azure"}
        with pytest.raises(ResourceNotFoundError):
            encoded_message_content = sr_json_encoder.encode(dict_content, schema=DRAFT07_SCHEMA_STR)

        # pre-register schema
        schema_properties = sr_client.register_schema(
            schemaregistry_group, DRAFT07_SCHEMA_JSON["title"], DRAFT07_SCHEMA_STR, SchemaFormat.JSON
        )
        schema_id = schema_properties.id

        # encode + validate w/ schema str
        encoded_message_content = sr_json_encoder.encode(dict_content, schema=DRAFT07_SCHEMA_STR)
        content_type = encoded_message_content["content_type"]
        assert content_type.split("+")[0] == "application/json;serialization=Json"
        assert content_type.split("+")[1] == schema_id

        sr_client.close()
        sr_json_encoder.close()

        # use encoder w/o group_name to decode
        extra_sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)
        sr_json_encoder_no_group = JsonSchemaEncoder(client=extra_sr_client, validate=DRAFT07_SCHEMA_IDENTIFIER)
        decoded_content = sr_json_encoder_no_group.decode(encoded_message_content)
        assert decoded_content["name"] == "Ben"
        assert decoded_content["favorite_number"] == 7
        assert decoded_content["favorite_color"] == "red"
        assert decoded_content["org"] == "Azure"

        # using encoder w/o group_name to encode raises error
        # group_name is used w/ schema to get_schema_id
        with pytest.raises(TypeError):
            encoded_message_content = sr_json_encoder_no_group.encode(dict_content, schema=DRAFT2020_12_SCHEMA_STR)

        sr_json_encoder_no_group.close()
        extra_sr_client.close()

    @SchemaRegistryEnvironmentVariableLoader()
    @recorded_by_proxy
    def test_sr_json_encoder_encode_message_type(self, **kwargs):
        schemaregistry_json_fully_qualified_namespace = kwargs.pop("schemaregistry_json_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)
        sr_json_encoder = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT2020_12_SCHEMA_IDENTIFIER
        )

        # pre-register schema
        schema_properties = sr_client.register_schema(
            schemaregistry_group, DRAFT2020_12_SCHEMA_JSON["title"], DRAFT2020_12_SCHEMA_STR, SchemaFormat.JSON
        )
        schema_id = schema_properties.id
        dict_content = {"name": "Ben", "favorite_number": 7, "favorite_color": "red"}

        # message type that doesn't fit OutboundMessageContent protocol
        class BadExample:
            def __init__(self, not_content):
                self.not_content = not_content

        # encoding invalid message_type raises error
        with pytest.raises(TypeError) as e:
            sr_json_encoder.encode({"name": "Ben"}, schema_id=schema_id, message_type=BadExample)
        assert "subtype of the OutboundMessageContent" in (str(e.value))

        # decoding invalid message_type raises error
        bad_ex = BadExample("fake")
        with pytest.raises(TypeError) as e:  # caught TypeError
            sr_json_encoder.decode(message=bad_ex)
        assert "subtype of the InboundMessageContent" in (str(e.value))

        # message type that fits OutboundMessageContent/InboundMessageContent protocol
        class GoodExample:
            def __init__(self, content, **kwargs):
                self.content = content
                self.content_type = None
                self.extra = kwargs.pop("extra", None)

            @classmethod
            def from_message_content(cls, content: bytes, content_type: str, **kwargs):
                ge = cls(content)
                ge.content_type = content_type
                return ge

            def __message_content__(self):
                return {"content": self.content, "content_type": self.content_type}

        # encode/decode valid message_type
        # test that extra kwargs pass through to message_type constructor
        good_ex_obj = sr_json_encoder.encode(dict_content, schema_id=schema_id, message_type=GoodExample, extra="val")
        decoded_content_obj = sr_json_encoder.decode(message=good_ex_obj)

        assert decoded_content_obj["name"] == "Ben"
        assert decoded_content_obj["favorite_number"] == 7
        assert decoded_content_obj["favorite_color"] == "red"
        sr_client.close()
        sr_json_encoder.close()

    @pytest.mark.skip("not allowing schema generation callable for now")
    @SchemaRegistryEnvironmentVariableLoader()
    @recorded_by_proxy
    def test_sr_json_encoder_encode_decode_with_generate_schema_callable(self, **kwargs):
        schemaregistry_json_fully_qualified_namespace = kwargs.pop("schemaregistry_json_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)
        sr_json_encoder = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT2020_12_SCHEMA_IDENTIFIER
        )

        from genson import SchemaBuilder

        def generate_schema(content: Mapping[str, Any]) -> Mapping[str, Any]:
            builder = SchemaBuilder()
            try:
                builder.add_object(content)
                schema = builder.to_schema()
                schema["title"] = "Example.PersonTitle"
                return schema
            except:
                raise ValueError("schema cannot be generated")

        # pre-register schema generated by callable
        dict_content = {"name": "Ben", "favorite_number": 7, "favorite_color": "red"}
        schema = generate_schema(dict_content)
        schema_properties = sr_client.register_schema(
            schemaregistry_group, schema["title"], json.dumps(schema), SchemaFormat.JSON
        )
        schema_id = schema_properties.id

        # encode w/ callable + override default schema
        encoded_message_content = sr_json_encoder.encode(dict_content, schema=generate_schema)
        content_type = encoded_message_content["content_type"]
        encoded_content = encoded_message_content["content"]
        assert content_type.split("+")[0] == "application/json;serialization=Json"
        assert content_type.split("+")[1] == schema_id

        # decode
        encoded_content_dict = {"content": encoded_content, "content_type": content_type}
        decoded_content = sr_json_encoder.decode(encoded_content_dict)
        assert decoded_content["name"] == "Ben"
        assert decoded_content["favorite_number"] == 7
        assert decoded_content["favorite_color"] == "red"

        # invalid content for generating schema
        def invalid_content():
            pass

        with pytest.raises(InvalidContentError):
            encoded_message_content = sr_json_encoder.encode(invalid_content, schema=generate_schema)
        sr_json_encoder.close()

    @SchemaRegistryEnvironmentVariableLoader()
    @recorded_by_proxy
    def test_sr_json_encoder_validate(self, **kwargs):
        schemaregistry_json_fully_qualified_namespace = kwargs.pop("schemaregistry_json_fully_qualified_namespace")
        schemaregistry_group = kwargs.pop("schemaregistry_group")
        sr_client = self.create_client(fully_qualified_namespace=schemaregistry_json_fully_qualified_namespace)

        # pre-register schema
        schema_properties = sr_client.register_schema(
            schemaregistry_group, DRAFT2020_12_SCHEMA_JSON["title"], DRAFT2020_12_SCHEMA_STR, SchemaFormat.JSON
        )
        schema_id = schema_properties.id

        # not passing in validate raises error
        with pytest.raises(TypeError):
            sr_json_encoder = JsonSchemaEncoder(client=sr_client, group_name=schemaregistry_group)

        # invalid draft identifier raises error
        with pytest.raises(ValueError) as exc:
            sr_json_encoder = JsonSchemaEncoder(
                client=sr_client, group_name=schemaregistry_group, validate="http://json-schema.org/draft-01/schema"
            )
        assert "not a supported" in str(exc.value)

        # Draft 3/4 do not accept float for integer type
        dict_content = {"name": "Ben", "favorite_number": 1.0, "favorite_color": "red"}
        sr_json_encoder_d3 = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT03_SCHEMA_IDENTIFIER
        )
        with pytest.raises(InvalidContentError) as exc:
            sr_json_encoder_d3.encode(dict_content, schema=DRAFT2020_12_SCHEMA_STR)
        assert isinstance(exc.value.__cause__, jsonschema.exceptions.ValidationError)
        assert exc.value.__cause__.message == "1.0 is not of type 'integer'"
        sr_json_encoder_d4 = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT04_SCHEMA_IDENTIFIER
        )
        with pytest.raises(InvalidContentError) as exc:
            sr_json_encoder_d4.encode(dict_content, schema_id=schema_id)
        assert isinstance(exc.value.__cause__, jsonschema.exceptions.ValidationError)
        assert exc.value.__cause__.message == "1.0 is not of type 'integer'"

        # Draft 6+ does accept float for integer type + test passing str value
        sr_json_encoder_d6 = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT06_SCHEMA_IDENTIFIER
        )
        encoded_message_content = sr_json_encoder_d6.encode(dict_content, schema=DRAFT2020_12_SCHEMA_STR)
        content_type = encoded_message_content["content_type"]
        assert content_type.split("+")[0] == "application/json;serialization=Json"

        assert content_type.split("+")[1] == schema_id
        sr_json_encoder_d19 = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT2019_09_SCHEMA_IDENTIFIER
        )
        encoded_message_content = sr_json_encoder_d19.encode(dict_content, schema_id=schema_id)

        # decode w/ Draft 3/4 validation raises error
        with pytest.raises(InvalidContentError) as exc:
            sr_json_encoder_d3.decode(encoded_message_content)
        assert isinstance(exc.value.__cause__, jsonschema.exceptions.ValidationError)
        assert exc.value.__cause__.message == "1.0 is not of type 'integer'"
        with pytest.raises(InvalidContentError) as exc:
            sr_json_encoder_d4.decode(encoded_message_content)
        assert isinstance(exc.value.__cause__, jsonschema.exceptions.ValidationError)
        assert exc.value.__cause__.message == "1.0 is not of type 'integer'"

        # decode w/ Draft 6+ validation passes
        decoded_content = sr_json_encoder_d6.decode(encoded_message_content)
        assert decoded_content["name"] == "Ben"
        assert decoded_content["favorite_number"] == 1.0
        assert decoded_content["favorite_color"] == "red"
        decoded_content = sr_json_encoder_d19.decode(encoded_message_content)
        assert decoded_content["name"] == "Ben"
        assert decoded_content["favorite_number"] == 1.0
        assert decoded_content["favorite_color"] == "red"

        # wrong data type, check with draft 7 validator
        sr_json_encoder_d7 = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=DRAFT07_SCHEMA_IDENTIFIER
        )
        dict_content_bad = {"name": "Ben", "favorite_number": 7, "favorite_color": 7}
        with pytest.raises(InvalidContentError) as e:
            encoded_message_content = sr_json_encoder_d7.encode(dict_content_bad, schema=DRAFT2020_12_SCHEMA_STR)
        assert "schema_id" in e.value.details

        # "turn off" validation w/ callable, wrong data type isn't detected
        def callable_validate(content: Mapping[str, Any], schema: Mapping[str, Any]) -> None:
            pass

        sr_json_encoder_no_validate = JsonSchemaEncoder(
            client=sr_client, group_name=schemaregistry_group, validate=callable_validate
        )
        dict_content_bad = {"name": "Ben", "favorite_number": 7, "favorite_color": 7}
        encoded_message_content = sr_json_encoder_no_validate.encode(dict_content_bad, schema=DRAFT2020_12_SCHEMA_STR)
        sr_json_encoder_d3.close()
        sr_json_encoder_d4.close()
        sr_json_encoder_d6.close()
        sr_json_encoder_d7.close()
        sr_json_encoder_d19.close()
        sr_json_encoder_no_validate.close()