From f1c0a3baf74baae94a0d4137782e15edcdf54a7e Mon Sep 17 00:00:00 2001 From: Natalia <124304+nessita@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:15:31 -0300 Subject: [PATCH] Added tests for model field's choices iterator/iterable values. --- tests/field_deconstruction/tests.py | 15 +++++++++++++++ tests/model_fields/models.py | 1 + tests/model_fields/tests.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) 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):