diff options
| author | areski <areski@gmail.com> | 2014-07-27 23:39:40 +0200 |
|---|---|---|
| committer | Florian Apolloner <florian@apolloner.eu> | 2014-07-30 21:36:01 +0200 |
| commit | e22ad1c3250a59d046d73bf34ae39c0c40e03c98 (patch) | |
| tree | 5808bb699629b1e1de39790861696fd465553d2b /tests/model_fields | |
| parent | 5736631233f509de8013df7f71c7698783886a28 (diff) | |
[1.7.x] Fixed #23112 -- Field.get_choices tries to index an iterable
Backport of 97a38de230371c0b6ad8a86abba8425186c147c7 from master.
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/models.py | 23 | ||||
| -rw-r--r-- | tests/model_fields/tests.py | 28 |
2 files changed, 50 insertions, 1 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 3888223ad9..b534f218ce 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -45,6 +45,29 @@ class Whiz(models.Model): c = models.IntegerField(choices=CHOICES, null=True) +class Counter: + def __init__(self): + self.n = 1 + + def __iter__(self): + return self + + def next(self): # Python 3: def __next__(self) + if self.n > 5: + raise StopIteration + else: + self.n += 1 + return (self.n, 'val-'+str(self.n)) + + +class WhizIter(models.Model): + c = models.IntegerField(choices=Counter(), null=True) + + +class WhizIterEmpty(models.Model): + c = models.CharField(choices=(x for x in []), blank=True, max_length=1) + + class BigD(models.Model): d = models.DecimalField(max_digits=38, decimal_places=30) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 404591874b..03aae7544c 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -25,7 +25,8 @@ from .models import ( Foo, Bar, Whiz, BigD, BigS, BigIntegerModel, Post, NullBooleanModel, BooleanModel, PrimaryKeyCharModel, DataModel, Document, RenamedField, DateTimeModel, VerboseNameField, FksToBooleans, FkToChar, FloatModel, - SmallIntegerModel, IntegerModel, PositiveSmallIntegerModel, PositiveIntegerModel) + SmallIntegerModel, IntegerModel, PositiveSmallIntegerModel, PositiveIntegerModel, + WhizIter, WhizIterEmpty) class BasicFieldTests(test.TestCase): @@ -375,6 +376,31 @@ class ChoicesTests(test.TestCase): self.assertEqual(Whiz(c=None).get_c_display(), None) # Blank value self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value + def test_iterator_choices(self): + """ + Check that get_choices works with Iterators (#23112). + """ + self.assertEqual(WhizIter(c=1).c, 1) # A nested value + self.assertEqual(WhizIter(c=9).c, 9) # Invalid value + self.assertEqual(WhizIter(c=None).c, None) # Blank value + self.assertEqual(WhizIter(c='').c, '') # Empty value + + def test_empty_iterator_choices(self): + """ + Check that get_choices works with empty iterators (#23112). + """ + self.assertEqual(WhizIterEmpty(c="a").c, "a") # A nested value + self.assertEqual(WhizIterEmpty(c="b").c, "b") # Invalid value + self.assertEqual(WhizIterEmpty(c=None).c, None) # Blank value + self.assertEqual(WhizIterEmpty(c='').c, '') # Empty value + + def test_charfield_get_choices_with_blank_iterator(self): + """ + Check that get_choices works with an empty Iterator + """ + f = models.CharField(choices=(x for x in [])) + self.assertEqual(f.get_choices(include_blank=True), [('', '---------')]) + class SlugFieldTests(test.TestCase): def test_slugfield_max_length(self): |
