summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorFrançois Freitag <mail@franek.fr>2017-03-03 18:45:31 -0800
committerTim Graham <timograham@gmail.com>2017-05-04 09:17:29 -0400
commitc0a2b9508aa2c6eaa22e9787010276edca887f0b (patch)
tree011a500063926e0f754d342afff242caa28e4efd /tests/prefetch_related
parent16121da78d7a522bf6b6e5fe704eb655b9ab442d (diff)
downloaddjango-c0a2b9508aa2c6eaa22e9787010276edca887f0b.tar.gz
Fixed #27554 -- Fixed prefetch_related() crash when fetching relations in nested Prefetches.
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/tests.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index 5ebd39db87..5289661ad7 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -1361,3 +1361,37 @@ class Ticket25546Tests(TestCase):
self.assertEqual(book1.first_authors[0].happy_place, [self.author1_address1])
self.assertEqual(book1.first_authors[1].happy_place, [])
self.assertEqual(book2.first_authors[0].happy_place, [self.author2_address1])
+
+
+class ReadPrefetchedObjectsCacheTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.book1 = Book.objects.create(title='Les confessions Volume I')
+ cls.book2 = Book.objects.create(title='Candide')
+ cls.author1 = AuthorWithAge.objects.create(name='Rousseau', first_book=cls.book1, age=70)
+ cls.author2 = AuthorWithAge.objects.create(name='Voltaire', first_book=cls.book2, age=65)
+ cls.book1.authors.add(cls.author1)
+ cls.book2.authors.add(cls.author2)
+ FavoriteAuthors.objects.create(author=cls.author1, likes_author=cls.author2)
+
+ def test_retrieves_results_from_prefetched_objects_cache(self):
+ """
+ When intermediary results are prefetched without a destination
+ attribute, they are saved in the RelatedManager's cache
+ (_prefetched_objects_cache). prefetch_related() uses this cache
+ (#27554).
+ """
+ authors = AuthorWithAge.objects.prefetch_related(
+ Prefetch(
+ 'author',
+ queryset=Author.objects.prefetch_related(
+ # Results are saved in the RelatedManager's cache
+ # (_prefetched_objects_cache) and do not replace the
+ # RelatedManager on Author instances (favorite_authors)
+ Prefetch('favorite_authors__first_book'),
+ ),
+ ),
+ )
+ with self.assertNumQueries(4):
+ # AuthorWithAge -> Author -> FavoriteAuthors, Book
+ self.assertQuerysetEqual(authors, ['<AuthorWithAge: Rousseau>', '<AuthorWithAge: Voltaire>'])