Skip to content

Commit 5e7a434

Browse files
authored
Merge pull request #13 from marcosschroh/fix/add-missing-tests
feat: Some missing tests added.
2 parents 338727a + 2ecb9ef commit 5e7a434

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
exclude = .git,__pycache__,docs/source/build,dist
3+
max-line-length = 100

docs/client.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def get_compatibility(subject, headers=None):
130130
headers (dict): Extra headers to add on the requests
131131
132132
Returns:
133-
str: one of 'NONE','FULL','FORWARD', or 'BACKWARD'
133+
str: one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE,
134+
FULL, FULL_TRANSITIVE, NONE
134135
135136
Raises:
136137
ClientError: if the request was unsuccessful or an invalid
@@ -144,9 +145,11 @@ def get_compatibility(subject, headers=None):
144145
def update_compatibility(level, subject, headers=None):
145146
"""
146147
Update the compatibility level for a subject.
148+
If subject is None, the compatibility level is global.
147149
148150
Args:
149-
level (str): ex: 'NONE','FULL','FORWARD', or 'BACKWARD'
151+
level (str): one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE,
152+
FULL, FULL_TRANSITIVE, NONE
150153
headers (dict): Extra headers to add on the requests
151154
152155
Returns:

schema_registry/client/client.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def register(self, subject, avro_schema, headers=None):
189189
msg = "Unable to register schema"
190190

191191
if msg is not None:
192-
raise ClientError(message=msg, code=code, server_traceback=result)
192+
raise ClientError(message=msg, http_code=code, server_traceback=result)
193193

194194
schema_id = result["id"]
195195
self._cache_schema(avro_schema, schema_id, subject)
@@ -214,7 +214,9 @@ def delete_subject(self, subject, headers=None):
214214

215215
result, code = self.request(url, method="DELETE", headers=headers)
216216
if not (status.HTTP_200_OK <= code < status.HTTP_300_MULTIPLE_CHOICES):
217-
raise ClientError("Unable to delete subject", code, server_traceback=result)
217+
raise ClientError(
218+
"Unable to delete subject", http_code=code, server_traceback=result
219+
)
218220
return result
219221

220222
def get_by_id(self, schema_id, headers=None):
@@ -252,7 +254,7 @@ def get_by_id(self, schema_id, headers=None):
252254
# bad schema - should not happen
253255
raise ClientError(
254256
f"Received bad schema (id {schema_id})",
255-
code,
257+
http_code=code,
256258
server_traceback=result,
257259
)
258260

@@ -365,7 +367,9 @@ def test_compatibility(self, subject, avro_schema, version="latest", headers=Non
365367
elif status.HTTP_200_OK <= code < status.HTTP_300_MULTIPLE_CHOICES:
366368
return result.get("is_compatible")
367369
else:
368-
log.error(f"Unable to check the compatibility: {code}")
370+
log.error(
371+
f"Unable to check the compatibility: {code}. Traceback: {result}"
372+
)
369373
return False
370374
except Exception as e:
371375
log.error(f"request() failed: {e}")
@@ -374,10 +378,13 @@ def test_compatibility(self, subject, avro_schema, version="latest", headers=Non
374378
def update_compatibility(self, level, subject=None, headers=None):
375379
"""
376380
PUT /config/(string: subject)
377-
Update the compatibility level for a subject. Level must be one of:
381+
Update the compatibility level.
382+
If subject is None, the compatibility level is global.
378383
379384
Args:
380-
level (str): ex: 'NONE','FULL','FORWARD', or 'BACKWARD'
385+
level (str): one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE,
386+
FULL, FULL_TRANSITIVE, NONE
387+
subject (str): Option subject
381388
headers (dict): Extra headers to add on the requests
382389
383390
Returns:
@@ -394,8 +401,10 @@ def update_compatibility(self, level, subject=None, headers=None):
394401
result, code = self.request(url, method="PUT", body=body, headers=headers)
395402
if status.HTTP_200_OK <= code < status.HTTP_300_MULTIPLE_CHOICES:
396403
return result["compatibility"]
397-
else:
398-
raise ClientError(f"Unable to update level: {level}. Error code: {code}")
404+
405+
raise ClientError(
406+
f"Unable to update level: {level}.", http_code=code, server_traceback=result
407+
)
399408

400409
def get_compatibility(self, subject, headers=None):
401410
"""
@@ -406,8 +415,8 @@ def get_compatibility(self, subject, headers=None):
406415
headers (dict): Extra headers to add on the requests
407416
408417
Returns:
409-
str: one of 'NONE','FULL','FORWARD', or 'BACKWARD'
410-
418+
str: one of BACKWARD, BACKWARD_TRANSITIVE, FORWARD, FORWARD_TRANSITIVE,
419+
FULL, FULL_TRANSITIVE, NONE
411420
Raises:
412421
ClientError: if the request was unsuccessful or an invalid
413422
compatibility level was returned
@@ -421,9 +430,7 @@ def get_compatibility(self, subject, headers=None):
421430
status.HTTP_200_OK <= code < status.HTTP_300_MULTIPLE_CHOICES
422431
)
423432
if not is_successful_request:
424-
raise ClientError(
425-
f"Unable to fetch compatibility level. Error code: {code}"
426-
)
433+
raise ClientError(f"Unable to fetch compatibility level")
427434

428435
compatibility = result.get("compatibilityLevel")
429436
if compatibility not in utils.VALID_LEVELS:
@@ -433,7 +440,7 @@ def get_compatibility(self, subject, headers=None):
433440
error_msg_suffix = str(compatibility)
434441
raise ClientError(
435442
f"Invalid compatibility level received: {error_msg_suffix}",
436-
code,
443+
http_code=code,
437444
server_traceback=result,
438445
)
439446

tests/client/test_http_client.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ def test_getters(client):
8585
assert fetched == parsed
8686

8787

88-
# def test_schema_compatibility(client):
89-
# adv = load.loads(data_gen.ADVANCED_SCHEMA)
90-
91-
9288
def test_multi_register(client):
9389
"""
9490
Register two different schemas under the same subject
@@ -117,6 +113,40 @@ def test_multi_register(client):
117113
assert latest_schema_2 == latest_schema_3
118114

119115

116+
def test_compatibility(client, user_schema_v3):
117+
"""
118+
Test the compatibility of a new User Schema against the latest one.
119+
The last user schema is the V2.
120+
"""
121+
compatibility = client.test_compatibility("test-user-schema", user_schema_v3)
122+
assert compatibility
123+
124+
125+
def test_update_compatibility(client):
126+
"""
127+
The latest User V2 schema is BACKWARD and FORWARDFULL compatibility (FULL).
128+
So, we can ipdate compatibility level for the specified subject.
129+
"""
130+
assert client.update_compatibility("FULL", "test-user-schema") == "FULL"
131+
132+
133+
def test_get_compatibility(client):
134+
"""
135+
Test latest compatibility for test-user-schema subject
136+
"""
137+
assert client.get_compatibility("test-user-schema") == "FULL"
138+
139+
140+
def test_delete_subject(client, user_schema_v3):
141+
subject = "subject-to-delete"
142+
versions = [load.loads(data_gen.USER_V1), load.loads(data_gen.USER_V2)]
143+
144+
for version in versions:
145+
client.register(subject, version)
146+
147+
assert client.delete_subject(subject) == [1, 2]
148+
149+
120150
def test_context(client):
121151
with client as c:
122152
parsed = load.loads(data_gen.BASIC_SCHEMA)

tests/conftest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,40 @@ def country_schema():
4141
)
4242

4343

44+
@pytest.fixture
45+
def user_schema_v3():
46+
"""
47+
The user V2 is:
48+
{
49+
"type": "record",
50+
"name": "User",
51+
"aliases": ["UserKey"],
52+
"fields": [
53+
{"name": "name", "type": "string"},
54+
{"name": "favorite_number", "type": ["int", "null"], "default": 42},
55+
{"name": "favorite_color", "type": ["string", "null"], "default": "purple"}
56+
]
57+
}
58+
"""
59+
return SchemaFromJSONData(
60+
{
61+
"type": "record",
62+
"name": "User",
63+
"aliases": ["UserKey"],
64+
"fields": [
65+
{"name": "name", "type": "string"},
66+
{"name": "favorite_number", "type": ["int", "null"], "default": 42},
67+
{
68+
"name": "favorite_color",
69+
"type": ["string", "null"],
70+
"default": "purple",
71+
},
72+
{"name": "country", "type": ["null", "string"], "default": None},
73+
],
74+
}
75+
)
76+
77+
4478
@pytest.fixture
4579
def message_serializer(client):
4680
return MessageSerializer(client)

0 commit comments

Comments
 (0)