Add OneOf support, rework field detection
This commit is contained in:
@@ -13,23 +13,23 @@ def test_has_field():
|
||||
|
||||
# Unset by default
|
||||
foo = Foo()
|
||||
assert foo.bar.serialized_on_wire == False
|
||||
assert betterproto.serialized_on_wire(foo.bar) == False
|
||||
|
||||
# Serialized after setting something
|
||||
foo.bar.baz = 1
|
||||
assert foo.bar.serialized_on_wire == True
|
||||
assert betterproto.serialized_on_wire(foo.bar) == True
|
||||
|
||||
# Still has it after setting the default value
|
||||
foo.bar.baz = 0
|
||||
assert foo.bar.serialized_on_wire == True
|
||||
assert betterproto.serialized_on_wire(foo.bar) == True
|
||||
|
||||
# Manual override
|
||||
foo.bar.serialized_on_wire = False
|
||||
assert foo.bar.serialized_on_wire == False
|
||||
# Manual override (don't do this)
|
||||
foo.bar._serialized_on_wire = False
|
||||
assert betterproto.serialized_on_wire(foo.bar) == False
|
||||
|
||||
# Can manually set it but defaults to false
|
||||
foo.bar = Bar()
|
||||
assert foo.bar.serialized_on_wire == False
|
||||
assert betterproto.serialized_on_wire(foo.bar) == False
|
||||
|
||||
|
||||
def test_enum_as_int_json():
|
||||
@@ -70,3 +70,48 @@ def test_unknown_fields():
|
||||
|
||||
new_again = Newer().parse(round_trip)
|
||||
assert newer == new_again
|
||||
|
||||
|
||||
def test_oneof_support():
|
||||
@dataclass
|
||||
class Sub(betterproto.Message):
|
||||
val: int = betterproto.int32_field(1)
|
||||
|
||||
@dataclass
|
||||
class Foo(betterproto.Message):
|
||||
bar: int = betterproto.int32_field(1, group="group1")
|
||||
baz: str = betterproto.string_field(2, group="group1")
|
||||
sub: Sub = betterproto.message_field(3, group="group2")
|
||||
abc: str = betterproto.string_field(4, group="group2")
|
||||
|
||||
foo = Foo()
|
||||
|
||||
assert betterproto.which_one_of(foo, "group1")[0] == ""
|
||||
|
||||
foo.bar = 1
|
||||
foo.baz = "test"
|
||||
|
||||
# Other oneof fields should now be unset
|
||||
assert foo.bar == 0
|
||||
assert betterproto.which_one_of(foo, "group1")[0] == "baz"
|
||||
|
||||
foo.sub.val = 1
|
||||
assert betterproto.serialized_on_wire(foo.sub)
|
||||
|
||||
foo.abc = "test"
|
||||
|
||||
# Group 1 shouldn't be touched, group 2 should have reset
|
||||
assert foo.sub.val == 0
|
||||
assert betterproto.serialized_on_wire(foo.sub) == False
|
||||
assert betterproto.which_one_of(foo, "group2")[0] == "abc"
|
||||
|
||||
# Zero value should always serialize for one-of
|
||||
foo = Foo(bar=0)
|
||||
assert betterproto.which_one_of(foo, "group1")[0] == "bar"
|
||||
assert bytes(foo) == b"\x08\x00"
|
||||
|
||||
# Round trip should also work
|
||||
foo2 = Foo().parse(bytes(foo))
|
||||
assert betterproto.which_one_of(foo2, "group1")[0] == "bar"
|
||||
assert foo.bar == 0
|
||||
assert betterproto.which_one_of(foo2, "group2")[0] == ""
|
||||
|
||||
Reference in New Issue
Block a user