summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNatalia <124304+nessita@users.noreply.github.com>2023-08-16 15:15:31 -0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-08-22 05:55:23 +0200
commitf1c0a3baf74baae94a0d4137782e15edcdf54a7e (patch)
treeb287002bf0df8b534244469d7383a34236a3b966 /tests
parent6934fc3f6eeaecc9363d0949c2f9691940a12121 (diff)
Added tests for model field's choices iterator/iterable values.
Diffstat (limited to 'tests')
-rw-r--r--tests/field_deconstruction/tests.py15
-rw-r--r--tests/model_fields/models.py1
-rw-r--r--tests/model_fields/tests.py12
3 files changed, 28 insertions, 0 deletions
diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py
index c78ed62876..3663886708 100644
--- a/tests/field_deconstruction/tests.py
+++ b/tests/field_deconstruction/tests.py
@@ -97,6 +97,21 @@ class FieldDeconstructionTests(SimpleTestCase):
kwargs, {"choices": [("A", "One"), ("B", "Two")], "max_length": 1}
)
+ def test_choices_iterator(self):
+ field = models.IntegerField(choices=((i, str(i)) for i in range(3)))
+ name, path, args, kwargs = field.deconstruct()
+ self.assertEqual(path, "django.db.models.IntegerField")
+ self.assertEqual(args, [])
+ self.assertEqual(kwargs, {"choices": [(0, "0"), (1, "1"), (2, "2")]})
+
+ def test_choices_iterable(self):
+ # Pass an iterator (but not an iterable) to choices.
+ field = models.IntegerField(choices="012345")
+ name, path, args, kwargs = field.deconstruct()
+ self.assertEqual(path, "django.db.models.IntegerField")
+ self.assertEqual(args, [])
+ self.assertEqual(kwargs, {"choices": ["0", "1", "2", "3", "4", "5"]})
+
def test_csi_field(self):
field = models.CommaSeparatedIntegerField(max_length=100)
name, path, args, kwargs = field.deconstruct()
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 424a78746d..1776cb9bcb 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -81,6 +81,7 @@ class Choiceful(models.Model):
empty_choices_bool = models.BooleanField(choices=())
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)))
class BigD(models.Model):
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index fe8526a480..91ef6058f9 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -157,16 +157,27 @@ class ChoicesTests(SimpleTestCase):
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")
+ 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.with_choices.choices, [(1, "A")])
+ self.assertEqual(
+ self.choices_from_iterator.choices, [(0, "0"), (1, "1"), (2, "2")]
+ )
def test_flatchoices(self):
self.assertEqual(self.no_choices.flatchoices, [])
self.assertEqual(self.empty_choices.flatchoices, [])
+ 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.choices_from_iterator.flatchoices, [(0, "0"), (1, "1"), (2, "2")]
+ )
def test_check(self):
self.assertEqual(Choiceful.check(), [])
@@ -196,6 +207,7 @@ class ChoicesTests(SimpleTestCase):
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)
+ self.assertEqual(self.choices_from_enum.flatchoices, Choiceful.Suit.choices)
class GetFieldDisplayTests(SimpleTestCase):