def test_forward_compat_serdes_new_enum_field(): test_map = WhitelistMap() @_whitelist_for_serdes(whitelist_map=test_map) class Corge(Enum): FOO = 1 BAR = 2 assert test_map.has_enum_entry("Corge") corge = Corge.FOO packed = _pack_value(corge, whitelist_map=test_map) # pylint: disable=function-redefined @_whitelist_for_serdes(whitelist_map=test_map) class Corge(Enum): FOO = 1 BAR = 2 BAZ = 3 unpacked = _unpack_value(packed, whitelist_map=test_map) assert unpacked != corge assert unpacked.name == corge.name assert unpacked.value == corge.value
def test_serdes_enum_backcompat(): test_map = WhitelistMap.create() @_whitelist_for_serdes(whitelist_map=test_map) class Corge(Enum): FOO = 1 BAR = 2 assert test_map.has_enum_entry("Corge") corge = Corge.FOO packed = _pack_value(corge, whitelist_map=test_map, descent_path="") class CorgeBackCompatSerializer(DefaultEnumSerializer): @classmethod def value_from_storage_str(cls, storage_str, klass): if storage_str == "FOO": value = "FOO_FOO" else: value = storage_str return super().value_from_storage_str(value, klass) # pylint: disable=function-redefined @_whitelist_for_serdes(whitelist_map=test_map, serializer=CorgeBackCompatSerializer) class Corge(Enum): BAR = 2 BAZ = 3 FOO_FOO = 4 unpacked = _unpack_value(packed, whitelist_map=test_map, descent_path="") assert unpacked != corge assert unpacked == Corge.FOO_FOO