Exemplo n.º 1
0
    def test_to_representation_handles_none(self):

        field = EnumField(choices=SomeEnum)

        value = field.to_representation(None)

        self.assertIsNone(value)
Exemplo n.º 2
0
    def test_choices(self):

        field = EnumField(choices=SomeEnum)

        self.assertDictEqual(field.choices, {"test1": 1, "test2": 2})

        field = EnumField(choices=SomeEnum)

        self.assertDictEqual(field.choices, {"test1": 1, "test2": 2})
Exemplo n.º 3
0
class SubLocationSerializer(serializers.ModelSerializer):
    date_entries = DateEntrySerializer(many=True, read_only=True)
    type = EnumField(choices=LocationType, to_choice=lambda x: (x, x.name), to_repr=lambda x: x)

    class Meta:
        model = Location
        fields = '__all__'
Exemplo n.º 4
0
    def test_to_internal_value_works(self):

        field = EnumField(choices=SomeEnum)

        self.assertEqual(field.to_internal_value(2), SomeEnum.test2)
        self.assertEqual(field.to_internal_value("test2"), SomeEnum.test2)

        field = EnumField(choices=SomeEnum)

        self.assertEqual(field.to_internal_value(2), SomeEnum.test2)
        self.assertEqual(field.to_internal_value("test2"), SomeEnum.test2)
Exemplo n.º 5
0
class VoteForArticleCommandDeserializer(
        serializers.Serializer[VoteForArticleCommand]):
    user_id = serializers.UUIDField()
    article_id = serializers.UUIDField()
    vote = EnumField(Vote)

    # Ignored mypy error:
    # Signature of "create" incompatible with supertype "BaseSerializer"
    def create(self) -> VoteForArticleCommand:  # type: ignore
        """
        Create and return a new `VoteForArticleCommand` instance,
        given the validated data.
        """
        return VoteForArticleCommand(**self.validated_data)
Exemplo n.º 6
0
    def test_to_representation_works(self):

        field = EnumField(choices=SomeEnum)

        self.assertEqual(field.to_representation(SomeEnum.test1), "test1")
Exemplo n.º 7
0
    def test_to_internal_value_validates(self):

        field = EnumField(choices=SomeEnum)

        with self.assertRaises(ValidationError):
            field.to_internal_value("test3")

        with self.assertRaises(ValidationError):
            field.to_internal_value(3)

        field = EnumField(choices=SomeEnum)

        with self.assertRaises(ValidationError):
            field.to_internal_value("test3")

        with self.assertRaises(ValidationError):
            field.to_internal_value(3)
class SuccessfullyVotedResultSerializer(
        serializers.Serializer[SuccessfullyVotedResult]):
    user_id = serializers.UUIDField()
    article_id = serializers.UUIDField()
    vote = EnumField(Vote)