mirror of
https://github.com/django/django.git
synced 2025-10-25 14:46:09 +00:00
Fixed #36426 -- Added support for further iterables in prefetch_related_objects().
Thanks Sarah Boyce for the review.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from collections import deque
|
||||
|
||||
from django.db.models import Prefetch, prefetch_related_objects
|
||||
from django.test import TestCase
|
||||
|
||||
@@ -221,3 +223,32 @@ class PrefetchRelatedObjectsTests(TestCase):
|
||||
|
||||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(book1.authors.all(), [self.author1, self.author2])
|
||||
|
||||
def test_prefetch_related_objects_with_various_iterables(self):
|
||||
book = self.book1
|
||||
|
||||
class MyIterable:
|
||||
def __iter__(self):
|
||||
yield book
|
||||
|
||||
cases = {
|
||||
"set": {book},
|
||||
"tuple": (book,),
|
||||
"dict_values": {"a": book}.values(),
|
||||
"frozenset": frozenset([book]),
|
||||
"deque": deque([book]),
|
||||
"custom iterator": MyIterable(),
|
||||
}
|
||||
for case_type, case in cases.items():
|
||||
with self.subTest(case=case_type):
|
||||
# Clear the prefetch cache.
|
||||
book._prefetched_objects_cache = {}
|
||||
with self.assertNumQueries(1):
|
||||
prefetch_related_objects(case, "authors")
|
||||
with self.assertNumQueries(0):
|
||||
self.assertCountEqual(
|
||||
book.authors.all(), [self.author1, self.author2, self.author3]
|
||||
)
|
||||
|
||||
def test_prefetch_related_objects_empty(self):
|
||||
prefetch_related_objects([], "authors")
|
||||
|
||||
Reference in New Issue
Block a user