summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorT. Franzel <tfranzel@users.noreply.github.com>2023-03-11 00:34:13 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-21 19:44:41 +0100
commita2eaea8f22305b57dff3ab13add2e2887287bb6f (patch)
treea2f1acee503899e52a583572a46aeee0d1a5432c /tests/model_fields
parent051d5944f86400b9b3476db60bc73de7e9964810 (diff)
downloaddjango-a2eaea8f22305b57dff3ab13add2e2887287bb6f.tar.gz
Fixed #34388 -- Allowed using choice enumeration types directly on model and form fields.
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/models.py7
-rw-r--r--tests/model_fields/test_charfield.py4
-rw-r--r--tests/model_fields/test_integerfield.py4
-rw-r--r--tests/model_fields/tests.py5
4 files changed, 16 insertions, 4 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index c35dfc2ebe..424a78746d 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -69,11 +69,18 @@ class WhizIterEmpty(models.Model):
class Choiceful(models.Model):
+ class Suit(models.IntegerChoices):
+ DIAMOND = 1, "Diamond"
+ SPADE = 2, "Spade"
+ HEART = 3, "Heart"
+ CLUB = 4, "Club"
+
no_choices = models.IntegerField(null=True)
empty_choices = models.IntegerField(choices=(), null=True)
with_choices = models.IntegerField(choices=[(1, "A")], null=True)
empty_choices_bool = models.BooleanField(choices=())
empty_choices_text = models.TextField(choices=())
+ choices_from_enum = models.IntegerField(choices=Suit)
class BigD(models.Model):
diff --git a/tests/model_fields/test_charfield.py b/tests/model_fields/test_charfield.py
index e9c1444f16..782158d210 100644
--- a/tests/model_fields/test_charfield.py
+++ b/tests/model_fields/test_charfield.py
@@ -75,11 +75,11 @@ class ValidationTests(SimpleTestCase):
f.clean("not a", None)
def test_enum_choices_cleans_valid_string(self):
- f = models.CharField(choices=self.Choices.choices, max_length=1)
+ f = models.CharField(choices=self.Choices, max_length=1)
self.assertEqual(f.clean("c", None), "c")
def test_enum_choices_invalid_input(self):
- f = models.CharField(choices=self.Choices.choices, max_length=1)
+ f = models.CharField(choices=self.Choices, max_length=1)
msg = "Value 'a' is not a valid choice."
with self.assertRaisesMessage(ValidationError, msg):
f.clean("a", None)
diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py
index 7698160678..6761589b7e 100644
--- a/tests/model_fields/test_integerfield.py
+++ b/tests/model_fields/test_integerfield.py
@@ -301,11 +301,11 @@ class ValidationTests(SimpleTestCase):
f.clean("0", None)
def test_enum_choices_cleans_valid_string(self):
- f = models.IntegerField(choices=self.Choices.choices)
+ f = models.IntegerField(choices=self.Choices)
self.assertEqual(f.clean("1", None), 1)
def test_enum_choices_invalid_input(self):
- f = models.IntegerField(choices=self.Choices.choices)
+ f = models.IntegerField(choices=self.Choices)
with self.assertRaises(ValidationError):
f.clean("A", None)
with self.assertRaises(ValidationError):
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 6d4a91afa2..fe8526a480 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -156,6 +156,7 @@ class ChoicesTests(SimpleTestCase):
cls.empty_choices_bool = Choiceful._meta.get_field("empty_choices_bool")
cls.empty_choices_text = Choiceful._meta.get_field("empty_choices_text")
cls.with_choices = Choiceful._meta.get_field("with_choices")
+ cls.choices_from_enum = Choiceful._meta.get_field("choices_from_enum")
def test_choices(self):
self.assertIsNone(self.no_choices.choices)
@@ -192,6 +193,10 @@ class ChoicesTests(SimpleTestCase):
with self.subTest(field=field):
self.assertIsInstance(field.formfield(), forms.ChoiceField)
+ def test_choices_from_enum(self):
+ # Choices class was transparently resolved when given as argument.
+ self.assertEqual(self.choices_from_enum.choices, Choiceful.Suit.choices)
+
class GetFieldDisplayTests(SimpleTestCase):
def test_choices_and_field_display(self):