summaryrefslogtreecommitdiff
path: root/tests/raw_query/tests.py
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2023-11-29 09:35:34 +0000
committerJacob Walls <jacobtylerwalls@gmail.com>2025-10-16 14:52:22 -0400
commite097e8a12f21a4e92594830f1ad1942b31916d0f (patch)
tree43f448bf968f0c6c1a48577cbc4d1ba5b920624a /tests/raw_query/tests.py
parentf6bd90c84050a1c74fe2161cced00e7282cb845c (diff)
Fixed #28586 -- Added model field fetch modes.
May your database queries be much reduced with minimal effort. co-authored-by: Andreas Pelme <andreas@pelme.se> co-authored-by: Simon Charette <charette.s@gmail.com> co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Diffstat (limited to 'tests/raw_query/tests.py')
-rw-r--r--tests/raw_query/tests.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/raw_query/tests.py b/tests/raw_query/tests.py
index 853b7ee20e..f66afbf28b 100644
--- a/tests/raw_query/tests.py
+++ b/tests/raw_query/tests.py
@@ -1,7 +1,8 @@
from datetime import date
from decimal import Decimal
-from django.core.exceptions import FieldDoesNotExist
+from django.core.exceptions import FieldDoesNotExist, FieldFetchBlocked
+from django.db.models import FETCH_PEERS, RAISE
from django.db.models.query import RawQuerySet
from django.test import TestCase, skipUnlessDBFeature
@@ -158,6 +159,22 @@ class RawQueryTests(TestCase):
books = Book.objects.all()
self.assertSuccessfulRawQuery(Book, query, books)
+ def test_fk_fetch_mode_peers(self):
+ query = "SELECT * FROM raw_query_book"
+ books = list(Book.objects.fetch_mode(FETCH_PEERS).raw(query))
+ with self.assertNumQueries(1):
+ books[0].author
+ books[1].author
+
+ def test_fk_fetch_mode_raise(self):
+ query = "SELECT * FROM raw_query_book"
+ books = list(Book.objects.fetch_mode(RAISE).raw(query))
+ msg = "Fetching of Book.author blocked."
+ with self.assertRaisesMessage(FieldFetchBlocked, msg) as cm:
+ books[0].author
+ self.assertIsNone(cm.exception.__cause__)
+ self.assertTrue(cm.exception.__suppress_context__)
+
def test_db_column_handler(self):
"""
Test of a simple raw query against a model containing a field with
@@ -294,6 +311,23 @@ class RawQueryTests(TestCase):
with self.assertRaisesMessage(FieldDoesNotExist, msg):
list(Author.objects.raw(query))
+ def test_missing_fields_fetch_mode_peers(self):
+ query = "SELECT id, first_name, dob FROM raw_query_author"
+ authors = list(Author.objects.fetch_mode(FETCH_PEERS).raw(query))
+ with self.assertNumQueries(1):
+ authors[0].last_name
+ authors[1].last_name
+
+ def test_missing_fields_fetch_mode_raise(self):
+ query = "SELECT id, first_name, dob FROM raw_query_author"
+ authors = list(Author.objects.fetch_mode(RAISE).raw(query))
+ msg = "Fetching of Author.last_name blocked."
+ with self.assertRaisesMessage(FieldFetchBlocked, msg) as cm:
+ authors[0].last_name
+ self.assertIsNone(cm.exception.__cause__)
+ self.assertTrue(cm.exception.__suppress_context__)
+ self.assertTrue(cm.exception.__suppress_context__)
+
def test_annotations(self):
query = (
"SELECT a.*, count(b.id) as book_count "