From 5d12650ed9269acb3cba97fd70e8df2e35a55a54 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 30 Aug 2022 07:02:08 +0200 Subject: [PATCH] Refs #21204 -- Added more QuerySet.defer()/only() tests for invalid fields. --- tests/defer/tests.py | 54 +++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/tests/defer/tests.py b/tests/defer/tests.py index d2cff258b4..fe9637c7f1 100644 --- a/tests/defer/tests.py +++ b/tests/defer/tests.py @@ -1,5 +1,5 @@ -from django.core.exceptions import FieldError -from django.test import TestCase +from django.core.exceptions import FieldDoesNotExist, FieldError +from django.test import SimpleTestCase, TestCase from .models import ( BigChild, @@ -130,22 +130,6 @@ class DeferTests(AssertionMixin, TestCase): self.assertEqual(obj.related_id, self.s1.pk) self.assertEqual(obj.name, "p1") - def test_defer_select_related_raises_invalid_query(self): - msg = ( - "Field Primary.related cannot be both deferred and traversed " - "using select_related at the same time." - ) - with self.assertRaisesMessage(FieldError, msg): - Primary.objects.defer("related").select_related("related")[0] - - def test_only_select_related_raises_invalid_query(self): - msg = ( - "Field Primary.related cannot be both deferred and traversed using " - "select_related at the same time." - ) - with self.assertRaisesMessage(FieldError, msg): - Primary.objects.only("name").select_related("related")[0] - def test_defer_foreign_keys_are_deferred_and_not_traversed(self): # select_related() overrides defer(). with self.assertNumQueries(1): @@ -299,3 +283,37 @@ class TestDefer2(AssertionMixin, TestCase): # access of any of them. self.assertEqual(rf2.name, "new foo") self.assertEqual(rf2.value, "new bar") + + +class InvalidDeferTests(SimpleTestCase): + def test_invalid_defer(self): + msg = "Primary has no field named 'missing'" + with self.assertRaisesMessage(FieldDoesNotExist, msg): + list(Primary.objects.defer("missing")) + msg = "Secondary has no field named 'missing'" + with self.assertRaisesMessage(FieldDoesNotExist, msg): + list(Primary.objects.defer("related__missing")) + + def test_invalid_only(self): + msg = "Primary has no field named 'missing'" + with self.assertRaisesMessage(FieldDoesNotExist, msg): + list(Primary.objects.only("missing")) + msg = "Secondary has no field named 'missing'" + with self.assertRaisesMessage(FieldDoesNotExist, msg): + list(Primary.objects.only("related__missing")) + + def test_defer_select_related_raises_invalid_query(self): + msg = ( + "Field Primary.related cannot be both deferred and traversed using " + "select_related at the same time." + ) + with self.assertRaisesMessage(FieldError, msg): + Primary.objects.defer("related").select_related("related")[0] + + def test_only_select_related_raises_invalid_query(self): + msg = ( + "Field Primary.related cannot be both deferred and traversed using " + "select_related at the same time." + ) + with self.assertRaisesMessage(FieldError, msg): + Primary.objects.only("name").select_related("related")[0]