diff options
| author | Natalia <124304+nessita@users.noreply.github.com> | 2023-08-31 09:09:30 -0300 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2023-09-14 10:15:33 -0300 |
| commit | 691f70c47755c7f55fa75ce607d028f81468b745 (patch) | |
| tree | 4d791a30d85e51c0362dfa6fb85a518758affeb5 /tests/model_fields | |
| parent | 5bfb3cbf49e2b9701e7c42989e14a72374adb6bd (diff) | |
Fixed #24561 -- Added support for callables on model fields' choices.
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/models.py | 4 | ||||
| -rw-r--r-- | tests/model_fields/test_charfield.py | 15 | ||||
| -rw-r--r-- | tests/model_fields/test_integerfield.py | 15 | ||||
| -rw-r--r-- | tests/model_fields/tests.py | 18 |
4 files changed, 51 insertions, 1 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 7fb0f8b610..b966da59e5 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -77,6 +77,9 @@ class Choiceful(models.Model): HEART = 3, "Heart" CLUB = 4, "Club" + def get_choices(): + return [(i, str(i)) for i in range(3)] + no_choices = models.IntegerField(null=True) empty_choices = models.IntegerField(choices=(), null=True) with_choices = models.IntegerField(choices=[(1, "A")], null=True) @@ -88,6 +91,7 @@ class Choiceful(models.Model): empty_choices_text = models.TextField(choices=()) choices_from_enum = models.IntegerField(choices=Suit) choices_from_iterator = models.IntegerField(choices=((i, str(i)) for i in range(3))) + choices_from_callable = models.IntegerField(choices=get_choices) class BigD(models.Model): diff --git a/tests/model_fields/test_charfield.py b/tests/model_fields/test_charfield.py index 782158d210..e841ed807a 100644 --- a/tests/model_fields/test_charfield.py +++ b/tests/model_fields/test_charfield.py @@ -89,3 +89,18 @@ class ValidationTests(SimpleTestCase): msg = "This field cannot be null." with self.assertRaisesMessage(ValidationError, msg): f.clean(None, None) + + def test_callable_choices(self): + def get_choices(): + return {str(i): f"Option {i}" for i in range(3)} + + f = models.CharField(max_length=1, choices=get_choices) + + for i in get_choices(): + with self.subTest(i=i): + self.assertEqual(i, f.clean(i, None)) + + with self.assertRaises(ValidationError): + f.clean("A", None) + with self.assertRaises(ValidationError): + f.clean("3", None) diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py index 0d91cff9eb..34966304fb 100644 --- a/tests/model_fields/test_integerfield.py +++ b/tests/model_fields/test_integerfield.py @@ -318,3 +318,18 @@ class ValidationTests(SimpleTestCase): f.clean("A", None) with self.assertRaises(ValidationError): f.clean("3", None) + + def test_callable_choices(self): + def get_choices(): + return {i: str(i) for i in range(3)} + + f = models.IntegerField(choices=get_choices) + + for i in get_choices(): + with self.subTest(i=i): + self.assertEqual(i, f.clean(i, None)) + + with self.assertRaises(ValidationError): + f.clean("A", None) + with self.assertRaises(ValidationError): + f.clean("3", None) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index bc57a00a1d..36e54d4b8b 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -4,6 +4,7 @@ from django import forms from django.core.exceptions import ValidationError from django.db import models from django.test import SimpleTestCase, TestCase +from django.utils.choices import CallableChoiceIterator from django.utils.functional import lazy from .models import ( @@ -162,6 +163,7 @@ class ChoicesTests(SimpleTestCase): ) cls.choices_from_enum = Choiceful._meta.get_field("choices_from_enum") cls.choices_from_iterator = Choiceful._meta.get_field("choices_from_iterator") + cls.choices_from_callable = Choiceful._meta.get_field("choices_from_callable") def test_choices(self): self.assertIsNone(self.no_choices.choices) @@ -174,6 +176,12 @@ class ChoicesTests(SimpleTestCase): self.assertEqual( self.choices_from_iterator.choices, [(0, "0"), (1, "1"), (2, "2")] ) + self.assertIsInstance( + self.choices_from_callable.choices, CallableChoiceIterator + ) + self.assertEqual( + self.choices_from_callable.choices.func(), [(0, "0"), (1, "1"), (2, "2")] + ) def test_flatchoices(self): self.assertEqual(self.no_choices.flatchoices, []) @@ -186,6 +194,9 @@ class ChoicesTests(SimpleTestCase): self.assertEqual( self.choices_from_iterator.flatchoices, [(0, "0"), (1, "1"), (2, "2")] ) + self.assertEqual( + self.choices_from_callable.flatchoices, [(0, "0"), (1, "1"), (2, "2")] + ) def test_check(self): self.assertEqual(Choiceful.check(), []) @@ -204,9 +215,14 @@ class ChoicesTests(SimpleTestCase): self.assertIsInstance(no_choices_formfield, forms.IntegerField) fields = ( self.empty_choices, - self.with_choices, self.empty_choices_bool, self.empty_choices_text, + self.with_choices, + self.with_choices_dict, + self.with_choices_nested_dict, + self.choices_from_enum, + self.choices_from_iterator, + self.choices_from_callable, ) for field in fields: with self.subTest(field=field): |
