diff options
author | Alexey Nigin <nigin@umich.edu> | 2020-12-13 19:02:45 -0500 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-12-16 10:52:59 +0100 |
commit | b9ba85a7ce582f4821a21119a6c130dacab496c4 (patch) | |
tree | c6f64907e268a5c3e13a640f59da690d3e5842fd /tests/prefetch_related | |
parent | bebd4cfa8f5e0d2dff2de5e50d86e849a40f4bb2 (diff) | |
download | django-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.py | 26 |
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): |