Skip to content

Commit

Permalink
add tests for text choices class
Browse files Browse the repository at this point in the history
  • Loading branch information
saadullahaleem committed May 2, 2023
1 parent 960bebd commit 301087d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
import pytz
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import IntegerChoices
from django.db.models import IntegerChoices, TextChoices
from django.http import QueryDict
from django.test import TestCase, override_settings
from django.utils.timezone import activate, deactivate, override
Expand Down Expand Up @@ -1826,7 +1826,7 @@ def test_edit_choices(self):
field.run_validation(2)
assert exc_info.value.detail == ['"2" is not a valid choice.']

def test_enum_choices(self):
def test_integer_choices(self):
class ChoiceCase(IntegerChoices):
first = auto()
second = auto()
Expand All @@ -1851,6 +1851,29 @@ class ChoiceCase(IntegerChoices):
assert field.run_validation(ChoiceCase.first) == 1
assert field.run_validation("1") == 1

def test_text_choices(self):
class ChoiceCase(TextChoices):
first = auto()
second = auto()
# Enum validate
choices = [
(ChoiceCase.first, "first"),
(ChoiceCase.second, "second")
]

field = serializers.ChoiceField(choices=choices)
assert field.run_validation(ChoiceCase.first) == "first"
assert field.run_validation("first") == "first"

choices = [
(ChoiceCase.first.value, "first"),
(ChoiceCase.second.value, "second")
]

field = serializers.ChoiceField(choices=choices)
assert field.run_validation(ChoiceCase.first) == "first"
assert field.run_validation("first") == "first"


class TestChoiceFieldWithType(FieldValues):
"""
Expand Down

0 comments on commit 301087d

Please sign in to comment.