MCPcopy
hub / github.com/danielgtaylor/python-betterproto / test_has_field

Function test_has_field

tests/test_features.py:28–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26
27
28def test_has_field():
29 @dataclass
30 class Bar(betterproto.Message):
31 baz: int = betterproto.int32_field(1)
32
33 @dataclass
34 class Foo(betterproto.Message):
35 bar: Bar = betterproto.message_field(1)
36
37 # Unset by default
38 foo = Foo()
39 assert betterproto.serialized_on_wire(foo.bar) is False
40
41 # Serialized after setting something
42 foo.bar.baz = 1
43 assert betterproto.serialized_on_wire(foo.bar) is True
44
45 # Still has it after setting the default value
46 foo.bar.baz = 0
47 assert betterproto.serialized_on_wire(foo.bar) is True
48
49 # Manual override (don't do this)
50 foo.bar._serialized_on_wire = False
51 assert betterproto.serialized_on_wire(foo.bar) is False
52
53 # Can manually set it but defaults to false
54 foo.bar = Bar()
55 assert betterproto.serialized_on_wire(foo.bar) is False
56
57 @dataclass
58 class WithCollections(betterproto.Message):
59 test_list: List[str] = betterproto.string_field(1)
60 test_map: Dict[str, str] = betterproto.map_field(
61 2, betterproto.TYPE_STRING, betterproto.TYPE_STRING
62 )
63
64 # Is always set from parse, even if all collections are empty
65 with_collections_empty = WithCollections().parse(bytes(WithCollections()))
66 assert betterproto.serialized_on_wire(with_collections_empty) == True
67 with_collections_list = WithCollections().parse(
68 bytes(WithCollections(test_list=["a", "b", "c"]))
69 )
70 assert betterproto.serialized_on_wire(with_collections_list) == True
71 with_collections_map = WithCollections().parse(
72 bytes(WithCollections(test_map={"a": "b", "c": "d"}))
73 )
74 assert betterproto.serialized_on_wire(with_collections_map) == True
75
76
77def test_class_init():

Callers

nothing calls this directly

Calls 4

BarClass · 0.85
WithCollectionsClass · 0.85
parseMethod · 0.80
FooClass · 0.70

Tested by

no test coverage detected