From aa3cb3f37265be37d892e2b391ff023e9caee2a4 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 8 Aug 2023 18:55:10 +0200 Subject: [PATCH] Added IntegerField tests for checks for invalid choices. --- .../test_ordinary_fields.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index e9e8a702e0a..063c99e8bd7 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -845,6 +845,41 @@ class IntegerFieldTests(SimpleTestCase): ], ) + def test_non_iterable_choices(self): + class Model(models.Model): + field = models.IntegerField(choices=123) + + field = Model._meta.get_field("field") + self.assertEqual( + field.check(), + [ + Error( + "'choices' must be an iterable (e.g., a list or tuple).", + obj=field, + id="fields.E004", + ), + ], + ) + + def test_non_iterable_choices_number(self): + """An integer isn't a valid choice pair.""" + + class Model(models.Model): + field = models.IntegerField(choices=[123]) + + field = Model._meta.get_field("field") + self.assertEqual( + field.check(), + [ + Error( + "'choices' must be an iterable containing (actual value, human " + "readable name) tuples.", + obj=field, + id="fields.E005", + ), + ], + ) + @isolate_apps("invalid_models_tests") class TimeFieldTests(SimpleTestCase):