mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Added tests for model field's choices iterator/iterable values.
This commit is contained in:
parent
6934fc3f6e
commit
f1c0a3baf7
@ -97,6 +97,21 @@ class FieldDeconstructionTests(SimpleTestCase):
|
|||||||
kwargs, {"choices": [("A", "One"), ("B", "Two")], "max_length": 1}
|
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):
|
def test_csi_field(self):
|
||||||
field = models.CommaSeparatedIntegerField(max_length=100)
|
field = models.CommaSeparatedIntegerField(max_length=100)
|
||||||
name, path, args, kwargs = field.deconstruct()
|
name, path, args, kwargs = field.deconstruct()
|
||||||
|
@ -81,6 +81,7 @@ class Choiceful(models.Model):
|
|||||||
empty_choices_bool = models.BooleanField(choices=())
|
empty_choices_bool = models.BooleanField(choices=())
|
||||||
empty_choices_text = models.TextField(choices=())
|
empty_choices_text = models.TextField(choices=())
|
||||||
choices_from_enum = models.IntegerField(choices=Suit)
|
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):
|
class BigD(models.Model):
|
||||||
|
@ -157,16 +157,27 @@ class ChoicesTests(SimpleTestCase):
|
|||||||
cls.empty_choices_text = Choiceful._meta.get_field("empty_choices_text")
|
cls.empty_choices_text = Choiceful._meta.get_field("empty_choices_text")
|
||||||
cls.with_choices = Choiceful._meta.get_field("with_choices")
|
cls.with_choices = Choiceful._meta.get_field("with_choices")
|
||||||
cls.choices_from_enum = Choiceful._meta.get_field("choices_from_enum")
|
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):
|
def test_choices(self):
|
||||||
self.assertIsNone(self.no_choices.choices)
|
self.assertIsNone(self.no_choices.choices)
|
||||||
self.assertEqual(self.empty_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.with_choices.choices, [(1, "A")])
|
||||||
|
self.assertEqual(
|
||||||
|
self.choices_from_iterator.choices, [(0, "0"), (1, "1"), (2, "2")]
|
||||||
|
)
|
||||||
|
|
||||||
def test_flatchoices(self):
|
def test_flatchoices(self):
|
||||||
self.assertEqual(self.no_choices.flatchoices, [])
|
self.assertEqual(self.no_choices.flatchoices, [])
|
||||||
self.assertEqual(self.empty_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.with_choices.flatchoices, [(1, "A")])
|
||||||
|
self.assertEqual(
|
||||||
|
self.choices_from_iterator.flatchoices, [(0, "0"), (1, "1"), (2, "2")]
|
||||||
|
)
|
||||||
|
|
||||||
def test_check(self):
|
def test_check(self):
|
||||||
self.assertEqual(Choiceful.check(), [])
|
self.assertEqual(Choiceful.check(), [])
|
||||||
@ -196,6 +207,7 @@ class ChoicesTests(SimpleTestCase):
|
|||||||
def test_choices_from_enum(self):
|
def test_choices_from_enum(self):
|
||||||
# Choices class was transparently resolved when given as argument.
|
# 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.choices, Choiceful.Suit.choices)
|
||||||
|
self.assertEqual(self.choices_from_enum.flatchoices, Choiceful.Suit.choices)
|
||||||
|
|
||||||
|
|
||||||
class GetFieldDisplayTests(SimpleTestCase):
|
class GetFieldDisplayTests(SimpleTestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user