Refs #21204 -- Added more QuerySet.defer()/only() tests for invalid fields.

This commit is contained in:
Simon Charette 2022-08-30 07:02:08 +02:00 committed by Mariusz Felisiak
parent 60613ef516
commit 5d12650ed9
1 changed files with 36 additions and 18 deletions

View File

@ -1,5 +1,5 @@
from django.core.exceptions import FieldError from django.core.exceptions import FieldDoesNotExist, FieldError
from django.test import TestCase from django.test import SimpleTestCase, TestCase
from .models import ( from .models import (
BigChild, BigChild,
@ -130,22 +130,6 @@ class DeferTests(AssertionMixin, TestCase):
self.assertEqual(obj.related_id, self.s1.pk) self.assertEqual(obj.related_id, self.s1.pk)
self.assertEqual(obj.name, "p1") 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): def test_defer_foreign_keys_are_deferred_and_not_traversed(self):
# select_related() overrides defer(). # select_related() overrides defer().
with self.assertNumQueries(1): with self.assertNumQueries(1):
@ -299,3 +283,37 @@ class TestDefer2(AssertionMixin, TestCase):
# access of any of them. # access of any of them.
self.assertEqual(rf2.name, "new foo") self.assertEqual(rf2.name, "new foo")
self.assertEqual(rf2.value, "new bar") 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]