summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorAdnan Umer <u.adnan@outlook.com>2018-04-18 22:30:25 +0500
committerTim Graham <timograham@gmail.com>2018-04-19 13:48:27 -0400
commit534d8d875eebac6aee278f0ffa6cc59760dac546 (patch)
treecf8aad986bf3f8814ef17edd397204d7337b6d6a /tests/prefetch_related
parentf2026ca5e29273771f4dba75ca99b8dd5812e047 (diff)
downloaddjango-534d8d875eebac6aee278f0ffa6cc59760dac546.tar.gz
Fixed #28600 -- Added prefetch_related() support to RawQuerySet.
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/tests.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index e92d7f349f..5a701bffec 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -14,7 +14,7 @@ from .models import (
)
-class PrefetchRelatedTests(TestCase):
+class TestDataMixin:
@classmethod
def setUpTestData(cls):
cls.book1 = Book.objects.create(title='Poems')
@@ -38,6 +38,8 @@ class PrefetchRelatedTests(TestCase):
cls.reader1.books_read.add(cls.book1, cls.book4)
cls.reader2.books_read.add(cls.book2, cls.book4)
+
+class PrefetchRelatedTests(TestDataMixin, TestCase):
def assertWhereContains(self, sql, needle):
where_idx = sql.index('WHERE')
self.assertEqual(
@@ -281,6 +283,38 @@ class PrefetchRelatedTests(TestCase):
self.assertWhereContains(sql, self.author1.id)
+class RawQuerySetTests(TestDataMixin, TestCase):
+ def test_basic(self):
+ with self.assertNumQueries(2):
+ books = Book.objects.raw(
+ "SELECT * FROM prefetch_related_book WHERE id = %s",
+ (self.book1.id,)
+ ).prefetch_related('authors')
+ book1 = list(books)[0]
+
+ with self.assertNumQueries(0):
+ self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3])
+
+ def test_prefetch_before_raw(self):
+ with self.assertNumQueries(2):
+ books = Book.objects.prefetch_related('authors').raw(
+ "SELECT * FROM prefetch_related_book WHERE id = %s",
+ (self.book1.id,)
+ )
+ book1 = list(books)[0]
+
+ with self.assertNumQueries(0):
+ self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3])
+
+ def test_clear(self):
+ with self.assertNumQueries(5):
+ with_prefetch = Author.objects.raw(
+ "SELECT * FROM prefetch_related_author"
+ ).prefetch_related('books')
+ without_prefetch = with_prefetch.prefetch_related(None)
+ [list(a.books.all()) for a in without_prefetch]
+
+
class CustomPrefetchTests(TestCase):
@classmethod
def traverse_qs(cls, obj_iter, path):