summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2023-08-31 02:57:40 +0100
committerGitHub <noreply@github.com>2023-08-30 22:57:40 -0300
commit500e01073adda32d5149624ee9a5cb7aa3d3583f (patch)
treef9416872a811aa39646deaf002414e0a7841b6d1 /tests/model_fields
parent68a8996bdfce2d191decd7b1c1a2b9fdea8e4b2f (diff)
Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/models.py34
-rw-r--r--tests/model_fields/test_integerfield.py8
-rw-r--r--tests/model_fields/tests.py20
3 files changed, 38 insertions, 24 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 1776cb9bcb..e1a5a3872f 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -31,24 +31,18 @@ class Bar(models.Model):
class Whiz(models.Model):
- CHOICES = (
- (
- "Group 1",
- (
- (1, "First"),
- (2, "Second"),
- ),
+ CHOICES = {
+ "Group 1": {
+ 1: "First",
+ 2: "Second",
+ },
+ "Group 2": (
+ (3, "Third"),
+ (4, "Fourth"),
),
- (
- "Group 2",
- (
- (3, "Third"),
- (4, "Fourth"),
- ),
- ),
- (0, "Other"),
- (5, _("translated")),
- )
+ 0: "Other",
+ 5: _("translated"),
+ }
c = models.IntegerField(choices=CHOICES, null=True)
@@ -61,7 +55,7 @@ WhizDelayed._meta.get_field("c").choices = Whiz.CHOICES
class WhizIter(models.Model):
- c = models.IntegerField(choices=iter(Whiz.CHOICES), null=True)
+ c = models.IntegerField(choices=iter(Whiz.CHOICES.items()), null=True)
class WhizIterEmpty(models.Model):
@@ -78,6 +72,10 @@ class Choiceful(models.Model):
no_choices = models.IntegerField(null=True)
empty_choices = models.IntegerField(choices=(), null=True)
with_choices = models.IntegerField(choices=[(1, "A")], null=True)
+ with_choices_dict = models.IntegerField(choices={1: "A"}, null=True)
+ with_choices_nested_dict = models.IntegerField(
+ choices={"Thing": {1: "A"}}, null=True
+ )
empty_choices_bool = models.BooleanField(choices=())
empty_choices_text = models.TextField(choices=())
choices_from_enum = models.IntegerField(choices=Suit)
diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py
index 6761589b7e..0d91cff9eb 100644
--- a/tests/model_fields/test_integerfield.py
+++ b/tests/model_fields/test_integerfield.py
@@ -279,6 +279,14 @@ class ValidationTests(SimpleTestCase):
f = models.IntegerField(choices=(("group", ((10, "A"), (20, "B"))), (30, "C")))
self.assertEqual(10, f.clean(10, None))
+ def test_choices_validation_supports_named_groups_dicts(self):
+ f = models.IntegerField(choices={"group": ((10, "A"), (20, "B")), 30: "C"})
+ self.assertEqual(10, f.clean(10, None))
+
+ def test_choices_validation_supports_named_groups_nested_dicts(self):
+ f = models.IntegerField(choices={"group": {10: "A", 20: "B"}, 30: "C"})
+ self.assertEqual(10, f.clean(10, None))
+
def test_nullable_integerfield_raises_error_with_blank_false(self):
f = models.IntegerField(null=True, blank=False)
with self.assertRaises(ValidationError):
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 91ef6058f9..bc57a00a1d 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -156,15 +156,21 @@ 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.with_choices_dict = Choiceful._meta.get_field("with_choices_dict")
+ cls.with_choices_nested_dict = Choiceful._meta.get_field(
+ "with_choices_nested_dict"
+ )
cls.choices_from_enum = Choiceful._meta.get_field("choices_from_enum")
cls.choices_from_iterator = Choiceful._meta.get_field("choices_from_iterator")
def test_choices(self):
self.assertIsNone(self.no_choices.choices)
- self.assertEqual(self.empty_choices.choices, ())
- self.assertEqual(self.empty_choices_bool.choices, ())
- self.assertEqual(self.empty_choices_text.choices, ())
+ self.assertEqual(self.empty_choices.choices, [])
+ self.assertEqual(self.empty_choices_bool.choices, [])
+ self.assertEqual(self.empty_choices_text.choices, [])
self.assertEqual(self.with_choices.choices, [(1, "A")])
+ self.assertEqual(self.with_choices_dict.choices, [(1, "A")])
+ self.assertEqual(self.with_choices_nested_dict.choices, [("Thing", [(1, "A")])])
self.assertEqual(
self.choices_from_iterator.choices, [(0, "0"), (1, "1"), (2, "2")]
)
@@ -175,6 +181,8 @@ class ChoicesTests(SimpleTestCase):
self.assertEqual(self.empty_choices_bool.flatchoices, [])
self.assertEqual(self.empty_choices_text.flatchoices, [])
self.assertEqual(self.with_choices.flatchoices, [(1, "A")])
+ self.assertEqual(self.with_choices_dict.flatchoices, [(1, "A")])
+ self.assertEqual(self.with_choices_nested_dict.flatchoices, [(1, "A")])
self.assertEqual(
self.choices_from_iterator.flatchoices, [(0, "0"), (1, "1"), (2, "2")]
)
@@ -290,11 +298,11 @@ class GetChoicesTests(SimpleTestCase):
("b", "Bar"),
(
"Group",
- (
+ [
("", "No Preference"),
("fg", "Foo"),
("bg", "Bar"),
- ),
+ ],
),
]
f = models.CharField(choices=choices)
@@ -302,7 +310,7 @@ class GetChoicesTests(SimpleTestCase):
def test_lazy_strings_not_evaluated(self):
lazy_func = lazy(lambda x: 0 / 0, int) # raises ZeroDivisionError if evaluated.
- f = models.CharField(choices=[(lazy_func("group"), (("a", "A"), ("b", "B")))])
+ f = models.CharField(choices=[(lazy_func("group"), [("a", "A"), ("b", "B")])])
self.assertEqual(f.get_choices(include_blank=True)[0], ("", "---------"))