summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorAlexey Nigin <nigin@umich.edu>2020-12-13 19:02:45 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-12-16 10:52:59 +0100
commitb9ba85a7ce582f4821a21119a6c130dacab496c4 (patch)
treec6f64907e268a5c3e13a640f59da690d3e5842fd /tests/prefetch_related
parentbebd4cfa8f5e0d2dff2de5e50d86e849a40f4bb2 (diff)
downloaddjango-b9ba85a7ce582f4821a21119a6c130dacab496c4.tar.gz
Fixed #32089 -- Fixed prefetch_related_objects() when some objects are already fetched.
Thanks Dennis Kliban for the report and Adam Johnson for the initial patch. Co-authored-by: Adam Johnson <me@adamj.eu>
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/test_prefetch_related_objects.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/prefetch_related/test_prefetch_related_objects.py b/tests/prefetch_related/test_prefetch_related_objects.py
index c0aa433b91..20d4da5a0a 100644
--- a/tests/prefetch_related/test_prefetch_related_objects.py
+++ b/tests/prefetch_related/test_prefetch_related_objects.py
@@ -97,6 +97,16 @@ class PrefetchRelatedObjectsTests(TestCase):
with self.assertNumQueries(0):
self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3])
+ def test_prefetch_object_twice(self):
+ book1 = Book.objects.get(id=self.book1.id)
+ book2 = Book.objects.get(id=self.book2.id)
+ with self.assertNumQueries(1):
+ prefetch_related_objects([book1], Prefetch('authors'))
+ with self.assertNumQueries(1):
+ prefetch_related_objects([book1, book2], Prefetch('authors'))
+ with self.assertNumQueries(0):
+ self.assertCountEqual(book2.authors.all(), [self.author1])
+
def test_prefetch_object_to_attr(self):
book1 = Book.objects.get(id=self.book1.id)
with self.assertNumQueries(1):
@@ -105,6 +115,22 @@ class PrefetchRelatedObjectsTests(TestCase):
with self.assertNumQueries(0):
self.assertCountEqual(book1.the_authors, [self.author1, self.author2, self.author3])
+ def test_prefetch_object_to_attr_twice(self):
+ book1 = Book.objects.get(id=self.book1.id)
+ book2 = Book.objects.get(id=self.book2.id)
+ with self.assertNumQueries(1):
+ prefetch_related_objects(
+ [book1],
+ Prefetch('authors', to_attr='the_authors'),
+ )
+ with self.assertNumQueries(1):
+ prefetch_related_objects(
+ [book1, book2],
+ Prefetch('authors', to_attr='the_authors'),
+ )
+ with self.assertNumQueries(0):
+ self.assertCountEqual(book2.the_authors, [self.author1])
+
def test_prefetch_queryset(self):
book1 = Book.objects.get(id=self.book1.id)
with self.assertNumQueries(1):