Skip to content

Commit

Permalink
test: add test that checks new message addition are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
eliax1996 committed Oct 17, 2023
1 parent 04bff10 commit d6d2b17
Showing 1 changed file with 181 additions and 0 deletions.
181 changes: 181 additions & 0 deletions tests/integration/test_dependencies_compatibility_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Copyright (c) 2019 Aiven Ltd
See LICENSE for details
"""

from karapace.client import Client
from karapace.protobuf.kotlin_wrapper import trim_margin
from tests.utils import create_subject_name_factory
Expand Down Expand Up @@ -669,3 +670,183 @@ async def test_protobuf_customer_update_when_having_references(registry_async_cl
res = await registry_async_client.post(f"subjects/{subject_customer}/versions", json=body)

assert res.status_code == 200


async def test_protobuf_schema_compatibility_full_path_renaming():
dependency = """\
package "hellofresh.shared.customer.delivery";
message DeliveryId {
string delivery_id = 1;
}\
"""

original_full_path = """\
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
hellofresh.shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

evolved_partial_path = """\
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

# placeholder for a real test
assert dependency + original_full_path + evolved_partial_path != ""


async def test_protobuf_schema_compatibility_partial_path_renaming():
dependency = """\
package "hellofresh.shared.customer.delivery";
message DeliveryId {
string delivery_id = 1;
}\
"""

original_partial_path = """\
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
hellofresh.shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

evolved_full_path = """\
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

# placeholder for a real test
assert dependency + original_partial_path + evolved_full_path != ""


async def test_protobuf_schema_compatibility_import_renaming_should_fail():
dependency = """\
package "hellofresh.shared.customer.delivery";
message DeliveryId {
string delivery_id = 1;
}\
"""

updated_dependency = """\
package "shared.customer.delivery";
message DeliveryId {
string delivery_id = 1;
}\
"""

original_partial_path = """\
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
import "shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

evolved_partial_path = """\
import "shared/customer/delivery/v1beta1/delivery_id.proto";
import "hellofresh/shared/customer/delivery/v1beta1/delivery_id.proto";
message PauseDeliveryRequest {
shared.customer.delivery.v1beta1.DeliveryId delivery_id = 1;
}\
"""

# placeholder for a real test
assert dependency + original_partial_path + evolved_partial_path + updated_dependency != ""
# this should fail because now it's referring to the other object.


async def test_protobuf_schema_update_add_message(registry_async_client: Client) -> None:
subject_place = create_subject_name_factory("test_protobuf_place")()
subject_customer = create_subject_name_factory("test_protobuf_customer")()

for subject in [subject_place, subject_customer]:
res = await registry_async_client.put(f"config/{subject}", json={"compatibility": "BACKWARD"})
assert res.status_code == 200

place_proto = """\
syntax = "proto3";
package a1;
message Place {
string city = 1;
int32 zone = 2;
}
"""

body = {"schemaType": "PROTOBUF", "schema": place_proto}
res = await registry_async_client.post(f"subjects/{subject_place}/versions", json=body)

assert res.status_code == 200

customer_proto = """\
syntax = "proto3";
package a1;
import "place.proto";
import "google/type/postal_address.proto";
// @producer: another comment
message Customer {
string name = 1;
int32 code = 2;
Place place = 3;
google.type.PostalAddress address = 4;
}
"""
body = {
"schemaType": "PROTOBUF",
"schema": customer_proto,
"references": [
{
"name": "place.proto",
"subject": subject_place,
"version": -1,
}
],
}
res = await registry_async_client.post(f"subjects/{subject_customer}/versions", json=body)

assert res.status_code == 200

customer_proto_updated = """\
syntax = "proto3";
package a1;
import "place.proto";
import "google/type/postal_address.proto";
// @consumer: the comment was incorrect, updating it now
message Customer {
string name = 1;
int32 code = 2;
Place place = 3;
google.type.PostalAddress address = 4;
}
message Bar {
string another = 1;
}
"""

body = {
"schemaType": "PROTOBUF",
"schema": customer_proto_updated,
"references": [
{
"name": "place.proto",
"subject": subject_place,
"version": -1,
}
],
}
res = await registry_async_client.post(f"subjects/{subject_customer}/versions", json=body)

assert res.status_code == 200

res = await registry_async_client.get(f"subjects/{subject_customer}/versions/2")

assert res.status_code == 200
assert res.json()["schema"] == customer_proto_updated

0 comments on commit d6d2b17

Please sign in to comment.