Properly serialize zero-value messages in a oneof group (#176)
Also improve test utils to make it easier to have multiple json examples. Co-authored-by: Christopher Chambers <chris@peanutcode.com>
This commit is contained in:
@@ -5,11 +5,11 @@ from tests.util import get_test_case_json_data
|
||||
|
||||
def test_which_count():
|
||||
message = Test()
|
||||
message.from_json(get_test_case_json_data("oneof"))
|
||||
message.from_json(get_test_case_json_data("oneof")[0])
|
||||
assert betterproto.which_one_of(message, "foo") == ("count", 100)
|
||||
|
||||
|
||||
def test_which_name():
|
||||
message = Test()
|
||||
message.from_json(get_test_case_json_data("oneof", "oneof-name.json"))
|
||||
message.from_json(get_test_case_json_data("oneof", "oneof_name.json")[0])
|
||||
assert betterproto.which_one_of(message, "foo") == ("name", "foobar")
|
||||
|
||||
3
tests/inputs/oneof_empty/oneof_empty.json
Normal file
3
tests/inputs/oneof_empty/oneof_empty.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"nothing": {}
|
||||
}
|
||||
15
tests/inputs/oneof_empty/oneof_empty.proto
Normal file
15
tests/inputs/oneof_empty/oneof_empty.proto
Normal file
@@ -0,0 +1,15 @@
|
||||
syntax = "proto3";
|
||||
|
||||
message Nothing {}
|
||||
|
||||
message MaybeNothing {
|
||||
string sometimes = 42;
|
||||
}
|
||||
|
||||
message Test {
|
||||
oneof empty {
|
||||
Nothing nothing = 1;
|
||||
MaybeNothing maybe1 = 2;
|
||||
MaybeNothing maybe2 = 3;
|
||||
}
|
||||
}
|
||||
3
tests/inputs/oneof_empty/oneof_empty_maybe1.json
Normal file
3
tests/inputs/oneof_empty/oneof_empty_maybe1.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"maybe1": {}
|
||||
}
|
||||
5
tests/inputs/oneof_empty/oneof_empty_maybe2.json
Normal file
5
tests/inputs/oneof_empty/oneof_empty_maybe2.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"maybe2": {
|
||||
"sometimes": "now"
|
||||
}
|
||||
}
|
||||
0
tests/inputs/oneof_empty/test_oneof_empty.py
Normal file
0
tests/inputs/oneof_empty/test_oneof_empty.py
Normal file
@@ -14,7 +14,9 @@ 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"))
|
||||
message.from_json(
|
||||
get_test_case_json_data("oneof_enum", "oneof_enum-enum-0.json")[0]
|
||||
)
|
||||
|
||||
assert message.move == Move(
|
||||
x=0, y=0
|
||||
@@ -28,7 +30,9 @@ 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"))
|
||||
message.from_json(
|
||||
get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json")[0]
|
||||
)
|
||||
assert message.move == Move(
|
||||
x=0, y=0
|
||||
) # Proto3 will default this as there is no null
|
||||
@@ -38,7 +42,7 @@ def test_which_one_of_returns_enum_with_non_default_value():
|
||||
|
||||
def test_which_one_of_returns_second_field_when_set():
|
||||
message = Test()
|
||||
message.from_json(get_test_case_json_data("oneof_enum"))
|
||||
message.from_json(get_test_case_json_data("oneof_enum")[0])
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user