File: test_schema_registry_async.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (154 lines) | stat: -rw-r--r-- 9,915 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
# 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 uuid
import os

from azure.schemaregistry.aio import SchemaRegistryClient
from azure.identity.aio import ClientSecretCredential
from azure.core.exceptions import ClientAuthenticationError, ServiceRequestError, HttpResponseError

from schemaregistry_preparer import SchemaRegistryPreparer
from devtools_testutils import AzureTestCase, PowerShellPreparer
from devtools_testutils.azure_testcase import _is_autorest_v3

from azure.core.credentials import AccessToken

SchemaRegistryPowerShellPreparer = functools.partial(PowerShellPreparer, "schemaregistry", schemaregistry_endpoint="fake_resource.servicebus.windows.net", schemaregistry_group="fakegroup")

class SchemaRegistryAsyncTests(AzureTestCase):

    def create_client(self, endpoint):
        credential = self.get_credential(SchemaRegistryClient, is_async=True)
        return self.create_client_from_credential(SchemaRegistryClient, credential, endpoint=endpoint, is_async=True)

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_basic_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        client = self.create_client(schemaregistry_endpoint)
        async with client:
            schema_name = self.get_resource_name('test-schema-basic-async')
            schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
            serialization_type = "Avro"
            schema_properties = await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)

            assert schema_properties.schema_id is not None
            assert schema_properties.location is not None
            assert schema_properties.location_by_id is not None
            assert schema_properties.version is 1
            assert schema_properties.serialization_type == "Avro"

            returned_schema = await client.get_schema(schema_id=schema_properties.schema_id)

            assert returned_schema.schema_properties.schema_id == schema_properties.schema_id
            assert returned_schema.schema_properties.location is not None
            assert returned_schema.schema_properties.location_by_id is not None
            assert returned_schema.schema_properties.version == 1
            assert returned_schema.schema_properties.serialization_type == "Avro"
            assert returned_schema.schema_content == schema_str

            returned_schema_properties = await client.get_schema_id(schemaregistry_group, schema_name, serialization_type, schema_str)

            assert returned_schema_properties.schema_id == schema_properties.schema_id
            assert returned_schema_properties.location is not None
            assert returned_schema_properties.location_by_id is not None
            assert returned_schema_properties.version == 1
            assert returned_schema_properties.serialization_type == "Avro"
        await client._generated_client._config.credential.close()

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_update_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        client = self.create_client(schemaregistry_endpoint)
        async with client:
            schema_name = self.get_resource_name('test-schema-update-async')
            schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
            serialization_type = "Avro"
            schema_properties = await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)

            assert schema_properties.schema_id is not None
            assert schema_properties.location is not None
            assert schema_properties.location_by_id is not None
            assert schema_properties.version != 0
            assert schema_properties.serialization_type == "Avro"

            schema_str_new = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_food","type":["string","null"]}]}"""
            new_schema_properties = await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str_new)

            assert new_schema_properties.schema_id is not None
            assert new_schema_properties.location is not None
            assert new_schema_properties.location_by_id is not None
            assert new_schema_properties.version == schema_properties.version + 1
            assert new_schema_properties.serialization_type == "Avro"

            new_schema = await client.get_schema(schema_id=new_schema_properties.schema_id)

            assert new_schema.schema_properties.schema_id != schema_properties.schema_id
            assert new_schema.schema_properties.schema_id == new_schema_properties.schema_id
            assert new_schema.schema_properties.location is not None
            assert new_schema.schema_properties.location_by_id is not None
            assert new_schema.schema_content == schema_str_new
            assert new_schema.schema_properties.version == schema_properties.version + 1
            assert new_schema.schema_properties.serialization_type == "Avro"
        await client._generated_client._config.credential.close()

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_same_twice_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        client = self.create_client(schemaregistry_endpoint)
        schema_name = self.get_resource_name('test-schema-twice-async')
        schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"age","type":["int","null"]},{"name":"city","type":["string","null"]}]}"""
        serialization_type = "Avro"
        async with client:
            schema_properties = await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)
            schema_properties_second = await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)
            assert schema_properties.schema_id == schema_properties_second.schema_id
        await client._generated_client._config.credential.close()

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_negative_wrong_credential_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        credential = ClientSecretCredential(tenant_id="fake", client_id="fake", client_secret="fake")
        client = SchemaRegistryClient(endpoint=schemaregistry_endpoint, credential=credential)
        async with client, credential:
            schema_name = self.get_resource_name('test-schema-negative-async')
            schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
            serialization_type = "Avro"
            with pytest.raises(ClientAuthenticationError):
                await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_negative_wrong_endpoint_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        client = self.create_client("nonexist.servicebus.windows.net")
        async with client:
            schema_name = self.get_resource_name('test-schema-nonexist-async')
            schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
            serialization_type = "Avro"
            with pytest.raises(ServiceRequestError):
                await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)
        await client._generated_client._config.credential.close()

    @SchemaRegistryPowerShellPreparer()
    async def test_schema_negative_no_schema_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
        client = self.create_client(schemaregistry_endpoint)
        async with client:
            with pytest.raises(HttpResponseError):
                await client.get_schema('a')

            with pytest.raises(HttpResponseError):
                await client.get_schema('a' * 32)
        await client._generated_client._config.credential.close()