Adding upstream version 1.2+20240521.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
6b2864e4b9
commit
8512f66c5a
229 changed files with 19561 additions and 0 deletions
|
@ -0,0 +1,30 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package oneof_default_value_serialization;
|
||||
|
||||
import "google/protobuf/duration.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
message Message{
|
||||
int64 value = 1;
|
||||
}
|
||||
|
||||
message NestedMessage{
|
||||
int64 id = 1;
|
||||
oneof value_type{
|
||||
Message wrapped_message_value = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message Test{
|
||||
oneof value_type {
|
||||
bool bool_value = 1;
|
||||
int64 int64_value = 2;
|
||||
google.protobuf.Timestamp timestamp_value = 3;
|
||||
google.protobuf.Duration duration_value = 4;
|
||||
Message wrapped_message_value = 5;
|
||||
NestedMessage wrapped_nested_message_value = 6;
|
||||
google.protobuf.BoolValue wrapped_bool_value = 7;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
import aristaproto
|
||||
from tests.output_aristaproto.oneof_default_value_serialization import (
|
||||
Message,
|
||||
NestedMessage,
|
||||
Test,
|
||||
)
|
||||
|
||||
|
||||
def assert_round_trip_serialization_works(message: Test) -> None:
|
||||
assert aristaproto.which_one_of(message, "value_type") == aristaproto.which_one_of(
|
||||
Test().from_json(message.to_json()), "value_type"
|
||||
)
|
||||
|
||||
|
||||
def test_oneof_default_value_serialization_works_for_all_values():
|
||||
"""
|
||||
Serialization from message with oneof set to default -> JSON -> message should keep
|
||||
default value field intact.
|
||||
"""
|
||||
|
||||
test_cases = [
|
||||
Test(bool_value=False),
|
||||
Test(int64_value=0),
|
||||
Test(
|
||||
timestamp_value=datetime.datetime(
|
||||
year=1970,
|
||||
month=1,
|
||||
day=1,
|
||||
hour=0,
|
||||
minute=0,
|
||||
tzinfo=datetime.timezone.utc,
|
||||
)
|
||||
),
|
||||
Test(duration_value=datetime.timedelta(0)),
|
||||
Test(wrapped_message_value=Message(value=0)),
|
||||
# NOTE: Do NOT use aristaproto.BoolValue here, it will cause JSON serialization
|
||||
# errors.
|
||||
# TODO: Do we want to allow use of BoolValue directly within a wrapped field or
|
||||
# should we simply hard fail here?
|
||||
Test(wrapped_bool_value=False),
|
||||
]
|
||||
for message in test_cases:
|
||||
assert_round_trip_serialization_works(message)
|
||||
|
||||
|
||||
def test_oneof_no_default_values_passed():
|
||||
message = Test()
|
||||
assert (
|
||||
aristaproto.which_one_of(message, "value_type")
|
||||
== aristaproto.which_one_of(Test().from_json(message.to_json()), "value_type")
|
||||
== ("", None)
|
||||
)
|
||||
|
||||
|
||||
def test_oneof_nested_oneof_messages_are_serialized_with_defaults():
|
||||
"""
|
||||
Nested messages with oneofs should also be handled
|
||||
"""
|
||||
message = Test(
|
||||
wrapped_nested_message_value=NestedMessage(
|
||||
id=0, wrapped_message_value=Message(value=0)
|
||||
)
|
||||
)
|
||||
assert (
|
||||
aristaproto.which_one_of(message, "value_type")
|
||||
== aristaproto.which_one_of(Test().from_json(message.to_json()), "value_type")
|
||||
== (
|
||||
"wrapped_nested_message_value",
|
||||
NestedMessage(id=0, wrapped_message_value=Message(value=0)),
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue