summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2022-01-09 00:58:41 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-01-25 06:12:04 +0100
commitedbf930287cb72e9afab1f7208c24b1146b0c4ec (patch)
treea7f758adc4bd3367b1a342f2287961c10b0ba741 /tests/prefetch_related
parentc27932ec938217d4fbb0adad23c0d0708f83f690 (diff)
downloaddjango-edbf930287cb72e9afab1f7208c24b1146b0c4ec.tar.gz
Fixed #29984 -- Added QuerySet.iterator() support for prefetching related objects.
Co-authored-by: Raphael Kimmig <raphael.kimmig@ampad.de> Co-authored-by: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/tests.py35
1 files changed, 34 insertions, 1 deletions
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index 5040f3d886..18f5de746f 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -7,7 +7,8 @@ from django.db.models import Prefetch, QuerySet, prefetch_related_objects
from django.db.models.query import get_prefetcher
from django.db.models.sql import Query
from django.test import TestCase, override_settings
-from django.test.utils import CaptureQueriesContext
+from django.test.utils import CaptureQueriesContext, ignore_warnings
+from django.utils.deprecation import RemovedInDjango50Warning
from .models import (
Article, Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book,
@@ -316,6 +317,38 @@ class PrefetchRelatedTests(TestDataMixin, TestCase):
['Anne', 'Charlotte', 'Emily', 'Jane'],
)
+ def test_m2m_prefetching_iterator_with_chunks(self):
+ with self.assertNumQueries(3):
+ authors = [
+ b.authors.first()
+ for b in Book.objects.prefetch_related('authors').iterator(chunk_size=2)
+ ]
+ self.assertEqual(
+ authors,
+ [self.author1, self.author1, self.author3, self.author4],
+ )
+
+ @ignore_warnings(category=RemovedInDjango50Warning)
+ def test_m2m_prefetching_iterator_without_chunks(self):
+ # prefetch_related() is ignored.
+ with self.assertNumQueries(5):
+ authors = [
+ b.authors.first()
+ for b in Book.objects.prefetch_related('authors').iterator()
+ ]
+ self.assertEqual(
+ authors,
+ [self.author1, self.author1, self.author3, self.author4],
+ )
+
+ def test_m2m_prefetching_iterator_without_chunks_warning(self):
+ msg = (
+ 'Using QuerySet.iterator() after prefetch_related() without '
+ 'specifying chunk_size is deprecated.'
+ )
+ with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
+ Book.objects.prefetch_related('authors').iterator()
+
class RawQuerySetTests(TestDataMixin, TestCase):
def test_basic(self):