diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-07-19 13:55:32 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-07-23 20:12:08 +0200 |
| commit | d89053585e11e869efcc9debb1c311b47b5e20ea (patch) | |
| tree | 94c606567d2611fb47914a59800af3d9ab2da6be /tests | |
| parent | 8323691de0ba120dbdc8055063574df2b0c0afa4 (diff) | |
Improved error message when index in __getitem__() is invalid.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/forms_tests/tests/test_forms.py | 9 | ||||
| -rw-r--r-- | tests/pagination/tests.py | 3 | ||||
| -rw-r--r-- | tests/queries/tests.py | 5 |
3 files changed, 16 insertions, 1 deletions
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 18fb4a94de..45f5405fee 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -746,6 +746,15 @@ Java</label></li> [str(bf[1]), str(bf[2]), str(bf[3])], ) + def test_boundfield_invalid_index(self): + class TestForm(Form): + name = ChoiceField(choices=[]) + + field = TestForm()['name'] + msg = 'BoundField indices must be integers or slices, not str.' + with self.assertRaisesMessage(TypeError, msg): + field['foo'] + def test_boundfield_bool(self): """BoundField without any choices (subwidgets) evaluates to True.""" class TestForm(Form): diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py index ef5108e42b..95310d26bd 100644 --- a/tests/pagination/tests.py +++ b/tests/pagination/tests.py @@ -366,7 +366,8 @@ class ModelPaginationTests(TestCase): # Make sure object_list queryset is not evaluated by an invalid __getitem__ call. # (this happens from the template engine when using eg: {% page_obj.has_previous %}) self.assertIsNone(p.object_list._result_cache) - with self.assertRaises(TypeError): + msg = 'Page indices must be integers or slices, not str.' + with self.assertRaisesMessage(TypeError, msg): p['has_previous'] self.assertIsNone(p.object_list._result_cache) self.assertNotIsInstance(p.object_list, list) diff --git a/tests/queries/tests.py b/tests/queries/tests.py index c49a0231ac..6d3dcbdb1f 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -2436,6 +2436,11 @@ class QuerySetSupportsPythonIdioms(TestCase): with self.assertRaisesMessage(AssertionError, "Negative indexing is not supported."): Article.objects.all()[0:-5] + def test_invalid_index(self): + msg = 'QuerySet indices must be integers or slices, not str.' + with self.assertRaisesMessage(TypeError, msg): + Article.objects.all()['foo'] + def test_can_get_number_of_items_in_queryset_using_standard_len(self): self.assertEqual(len(Article.objects.filter(name__exact='Article 1')), 1) |
