summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests/test_ordinary_fields.py
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2019-09-04 09:21:08 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-09-09 10:28:18 +0200
commitb6251956b69512bf230322bd7a49b629ca8455c6 (patch)
treea120d0fd618bbc70405821a94903248639f7ae35 /tests/invalid_models_tests/test_ordinary_fields.py
parentfee75d2aed4e58ada6567c464cfd22e89dc65f4a (diff)
Fixed #30757 -- Added a system check to ensure max_length fits the longest choice.
Diffstat (limited to 'tests/invalid_models_tests/test_ordinary_fields.py')
-rw-r--r--tests/invalid_models_tests/test_ordinary_fields.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py
index 5bb1847a70..2ef0907a6a 100644
--- a/tests/invalid_models_tests/test_ordinary_fields.py
+++ b/tests/invalid_models_tests/test_ordinary_fields.py
@@ -304,6 +304,32 @@ class CharFieldTests(SimpleTestCase):
self.assertEqual(Model._meta.get_field('field').check(), [])
+ def test_choices_in_max_length(self):
+ class Model(models.Model):
+ field = models.CharField(
+ max_length=2, choices=[
+ ('ABC', 'Value Too Long!'), ('OK', 'Good')
+ ],
+ )
+ group = models.CharField(
+ max_length=2, choices=[
+ ('Nested', [('OK', 'Good'), ('Longer', 'Longer')]),
+ ('Grouped', [('Bad', 'Bad')]),
+ ],
+ )
+
+ for name, choice_max_length in (('field', 3), ('group', 6)):
+ with self.subTest(name):
+ field = Model._meta.get_field(name)
+ self.assertEqual(field.check(), [
+ Error(
+ "'max_length' is too small to fit the longest value "
+ "in 'choices' (%d characters)." % choice_max_length,
+ obj=field,
+ id='fields.E009',
+ ),
+ ])
+
def test_bad_db_index_value(self):
class Model(models.Model):
field = models.CharField(max_length=10, db_index='bad')