Example #1
0
def test_enum_field_invalid_value():
    field = fields.Enum(MyEnum)
    invalid_value = "not_from_enum"
    with pytest.raises(ma.ValidationError, match="Not a valid value") as err:
        field._deserialize(invalid_value)
    for enm in MyEnum:
        assert err.match(enm.value), f"{enm.value} is not listed in valid enum values"

    field = fields.Enum(MyEnum, by_value=False)
    with pytest.raises(ma.ValidationError, match=r"Not a valid value") as err:
        field._deserialize(invalid_value)
    for enm in MyEnum:
        assert err.match(enm.name), f"{enm.name} is not listed in valid enum names"
Example #2
0
class DrawSourceCreateSerializer(ModelSerializer):
    quantity = fields.Int()
    resource = fields.Enum(db.DrawSourceResource)

    class Meta:
        model = db.draw_source
        fields = ('type', 'name', 'company_id', 'color', 'code',
                  'color_category', 'quantity', 'resource')
Example #3
0
def test_enum_field_by_name():
    my_enum_one = MyEnum.name_one
    field = fields.Enum(MyEnum, by_value=False)
    serialized = field._serialize(my_enum_one)
    assert serialized == my_enum_one.name, "invalid serialization by name"

    deserialized = field._deserialize(serialized)
    assert deserialized == my_enum_one, "invalid deserialization by name"
Example #4
0
class DrawSourceForUserSerializer(DrawSourceSerializer):
    quantity = fields.Int()
    resource = fields.Enum(db.DrawSourceResource)

    class Meta(DrawSourceSerializer.Meta):
        fields = ('id', 'type', 'name', 'companies', 'color', 'code',
                  'color_category', 'quantity', 'resource')

    async def update(self, instance, validated_data: typing.OrderedDict):
        from app.services.draw_source import DrawSourceService

        engine = self.serializer_context['view'].engine
        user = self.serializer_context['view'].user
        service = DrawSourceService(engine)
        await service.update(instance['id'], validated_data)
        return await service.get_for_user(user['id'], instance['id'])
Example #5
0
def test_enum_field_invalid_name_value(non_string_value):
    field = fields.Enum(MyEnum, by_value=False)
    with pytest.raises(ma.ValidationError, match="Not a valid string"):
        field._deserialize(non_string_value)
Example #6
0
def test_enum_serialize_null_value():
    field = fields.Enum(MyEnum)
    assert field._serialize(None) is None