def test_timestamp_to_python_idempotent(): # This path can never run in the current configuration because proto # values are the only thing ever saved, and `to_python` is a read method. # # However, we test idempotency for consistency with `to_proto` and # general resiliency. marshal = BaseMarshal() py_value = "f.b.d,f.c" assert marshal.to_python(field_mask_pb2.FieldMask, py_value) is py_value
def test_duration_to_python_idempotent(): # This path can never run in the current configuration because proto # values are the only thing ever saved, and `to_python` is a read method. # # However, we test idempotency for consistency with `to_proto` and # general resiliency. marshal = BaseMarshal() py_value = timedelta(seconds=240) assert marshal.to_python(duration_pb2.Duration, py_value) is py_value
def test_timestamp_to_python_idempotent(): # This path can never run in the current configuration because proto # values are the only thing ever saved, and `to_python` is a read method. # # However, we test idempotency for consistency with `to_proto` and # general resiliency. marshal = BaseMarshal() py_value = DatetimeWithNanoseconds(2012, 4, 21, 15, tzinfo=timezone.utc) assert marshal.to_python(timestamp_pb2.Timestamp, py_value) is py_value
def test_bool_value_to_python(): # This path can never run in the current configuration because proto # values are the only thing ever saved, and `to_python` is a read method. # # However, we test idempotency for consistency with `to_proto` and # general resiliency. marshal = BaseMarshal() assert marshal.to_python(wrappers_pb2.BoolValue, True) is True assert marshal.to_python(wrappers_pb2.BoolValue, False) is False assert marshal.to_python(wrappers_pb2.BoolValue, None) is None
def test_invalid_marshal_class(): marshal = BaseMarshal() with pytest.raises(TypeError): @marshal.register(empty_pb2.Empty) class Marshal: pass
def test_invalid_target_registration(): marshal = BaseMarshal() with pytest.raises(TypeError): @marshal.register(object) class Rule: def to_proto(self, value): return value def to_python(self, value, *, absent=None): return value
def test_registration(): marshal = BaseMarshal() @marshal.register(empty_pb2.Empty) class Rule: def to_proto(self, value): return value def to_python(self, value, *, absent=None): return value assert isinstance(marshal._rules[empty_pb2.Empty], Rule)
def test_invalid_marshal_rule(): marshal = BaseMarshal() with pytest.raises(TypeError): marshal.register(empty_pb2.Empty, rule=object())
def test_marshal_to_proto_stringy_numbers(pb_type, value, expected): marshal = BaseMarshal() assert marshal.to_proto(pb_type, value) == expected
def test_strict_to_proto(): m = BaseMarshal() with pytest.raises(TypeError): m.to_proto(dict, None, strict=True)