def test_FromFile_required_fields_not_set_uninitialized_okay(suffix): """Test that DecodeError not raised if required fields not set.""" with tempfile.NamedTemporaryFile(prefix="labm8_proto_", suffix=suffix) as f: proto_in = test_protos_pb2.AnotherTestMessage(number=1) pbutil.ToFile( test_protos_pb2.AnotherTestMessage(number=1), pathlib.Path(f.name), ) pbutil.FromFile( pathlib.Path(f.name), test_protos_pb2.TestMessage(), uninitialized_okay=True, )
def test_FromString_DecodeError_unknown_field(): """Test that DecodeError is raised if string contains unknown field.""" with test.Raises(pbutil.DecodeError) as e_info: proto = pbutil.FromString( 'foo: "bar"', test_protos_pb2.AnotherTestMessage(), ) assert ('1:1 : Message type "labm8.AnotherTestMessage" ' 'has no field named "foo".') == str(e_info.value)
def test_FromFile_required_fields_not_set(suffix): """Test that DecodeError raised if required fields not set.""" with tempfile.NamedTemporaryFile(prefix="labm8_proto_", suffix=suffix) as f: pbutil.ToFile( test_protos_pb2.AnotherTestMessage(number=1), pathlib.Path(f.name), ) with test.Raises(pbutil.DecodeError) as e_info: pbutil.FromFile(pathlib.Path(f.name), test_protos_pb2.TestMessage()) assert f"Required fields not set: '{f.name}'" == str(e_info.value)
def test_FromString_valid_input(): """Test that an valid input is decoded.""" proto = pbutil.FromString("number: 5", test_protos_pb2.AnotherTestMessage()) assert 5 == proto.number
def test_FromString_empty_string(): """Test that an empty string can be parsed as a proto.""" proto = pbutil.FromString("", test_protos_pb2.AnotherTestMessage()) assert not proto.number