diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2023-08-31 02:57:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-30 22:57:40 -0300 |
| commit | 500e01073adda32d5149624ee9a5cb7aa3d3583f (patch) | |
| tree | f9416872a811aa39646deaf002414e0a7841b6d1 /tests/invalid_models_tests | |
| parent | 68a8996bdfce2d191decd7b1c1a2b9fdea8e4b2f (diff) | |
Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.
Diffstat (limited to 'tests/invalid_models_tests')
| -rw-r--r-- | tests/invalid_models_tests/test_ordinary_fields.py | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 063c99e8bd..6014448013 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -199,7 +199,8 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be an iterable (e.g., a list or tuple).", + "'choices' must be a mapping (e.g. a dictionary) or an iterable " + "(e.g. a list or tuple).", obj=field, id="fields.E004", ), @@ -217,8 +218,9 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be an iterable containing (actual value, " - "human readable name) tuples.", + "'choices' must be a mapping of actual values to human readable " + "names or an iterable containing (actual value, human readable " + "name) tuples.", obj=field, id="fields.E005", ), @@ -260,8 +262,9 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be an iterable containing (actual " - "value, human readable name) tuples.", + "'choices' must be a mapping of actual values to human " + "readable names or an iterable containing (actual value, " + "human readable name) tuples.", obj=field, id="fields.E005", ), @@ -309,8 +312,9 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be an iterable containing (actual value, " - "human readable name) tuples.", + "'choices' must be a mapping of actual values to human readable " + "names or an iterable containing (actual value, human readable " + "name) tuples.", obj=field, id="fields.E005", ), @@ -337,8 +341,9 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be an iterable containing (actual value, " - "human readable name) tuples.", + "'choices' must be a mapping of actual values to human readable " + "names or an iterable containing (actual value, human readable " + "name) tuples.", obj=field, id="fields.E005", ), @@ -386,6 +391,26 @@ class CharFieldTests(TestCase): ], ) + def test_choices_callable(self): + def get_choices(): + return [(i, i) for i in range(3)] + + class Model(models.Model): + field = models.CharField(max_length=10, choices=get_choices) + + field = Model._meta.get_field("field") + self.assertEqual( + field.check(), + [ + Error( + "'choices' must be a mapping (e.g. a dictionary) or an iterable " + "(e.g. a list or tuple).", + obj=field, + id="fields.E004", + ), + ], + ) + def test_bad_db_index_value(self): class Model(models.Model): field = models.CharField(max_length=10, db_index="bad") @@ -854,7 +879,8 @@ class IntegerFieldTests(SimpleTestCase): field.check(), [ Error( - "'choices' must be an iterable (e.g., a list or tuple).", + "'choices' must be a mapping (e.g. a dictionary) or an iterable " + "(e.g. a list or tuple).", obj=field, id="fields.E004", ), @@ -872,8 +898,9 @@ class IntegerFieldTests(SimpleTestCase): field.check(), [ Error( - "'choices' must be an iterable containing (actual value, human " - "readable name) tuples.", + "'choices' must be a mapping of actual values to human readable " + "names or an iterable containing (actual value, human readable " + "name) tuples.", obj=field, id="fields.E005", ), |
