* Serialize default values in oneofs when calling to_dict() or to_json() This change is consistent with the official protobuf implementation. If a default value is set when using a oneof, and then a message is translated from message -> JSON -> message, the default value is kept in tact. Also, if no default value is set, they remain null. * Some cleanup + testing for nested messages with oneofs * Cleanup oneof_enum test cases, they should be fixed This _should_ address: https://github.com/danielgtaylor/python-betterproto/issues/63 * Include default value oneof fields when serializing to bytes This will cause oneof fields with default values to explicitly be sent to clients. Note that does not mean that all fields are serialized and sent to clients, just those that _could_ be null and are not. * Remove assignment when populating a sub-message within a proto Also, move setattr out one indentation level * Properly transform proto with empty string in oneof to bytes Also, updated tests to ensure that which_one_of picks up the set field * Formatting betterproto/__init__.py * Adding test cases demonstrating equivalent behaviour with google impl * Removing a temporary file I made locally * Adding some clarifying comments * Fixing tests for python38
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import pytest
|
|
|
|
import betterproto
|
|
from tests.output_betterproto.oneof_enum import (
|
|
Move,
|
|
Signal,
|
|
Test,
|
|
)
|
|
from tests.util import get_test_case_json_data
|
|
|
|
|
|
def test_which_one_of_returns_enum_with_default_value():
|
|
"""
|
|
returns first field when it is enum and set with default value
|
|
"""
|
|
message = Test()
|
|
message.from_json(get_test_case_json_data("oneof_enum", "oneof_enum-enum-0.json"))
|
|
|
|
assert message.move == Move(
|
|
x=0, y=0
|
|
) # Proto3 will default this as there is no null
|
|
assert message.signal == Signal.PASS
|
|
assert betterproto.which_one_of(message, "action") == ("signal", Signal.PASS)
|
|
|
|
|
|
def test_which_one_of_returns_enum_with_non_default_value():
|
|
"""
|
|
returns first field when it is enum and set with non default value
|
|
"""
|
|
message = Test()
|
|
message.from_json(get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json"))
|
|
assert message.move == Move(
|
|
x=0, y=0
|
|
) # Proto3 will default this as there is no null
|
|
assert message.signal == Signal.RESIGN
|
|
assert betterproto.which_one_of(message, "action") == ("signal", Signal.RESIGN)
|
|
|
|
|
|
def test_which_one_of_returns_second_field_when_set():
|
|
message = Test()
|
|
message.from_json(get_test_case_json_data("oneof_enum"))
|
|
assert message.move == Move(x=2, y=3)
|
|
assert message.signal == Signal.PASS
|
|
assert betterproto.which_one_of(message, "action") == ("move", Move(x=2, y=3))
|