Debian Codegraph @

tests/prefetch_related/test_prefetch_related_objects.py

raw
1 from django.db.models import Prefetch, prefetch_related_objects
2 from django.test import TestCase
3
4 from .models import Author, Book, House, Reader, Room
5
6
7 class PrefetchRelatedObjectsTests(TestCase):
8 """
9 Since prefetch_related_objects() is just the inner part of
10 prefetch_related(), only do basic tests to ensure its API hasn't changed.
11 """
12
13 @classmethod
14 def setUpTestData(cls):
15 cls.book1 = Book.objects.create(title="Poems")
16 cls.book2 = Book.objects.create(title="Jane Eyre")
17 cls.book3 = Book.objects.create(title="Wuthering Heights")
18 cls.book4 = Book.objects.create(title="Sense and Sensibility")
19
20 cls.author1 = Author.objects.create(name="Charlotte", first_book=cls.book1)
21 cls.author2 = Author.objects.create(name="Anne", first_book=cls.book1)
22 cls.author3 = Author.objects.create(name="Emily", first_book=cls.book1)
23 cls.author4 = Author.objects.create(name="Jane", first_book=cls.book4)
24
25 cls.book1.authors.add(cls.author1, cls.author2, cls.author3)
26 cls.book2.authors.add(cls.author1)
27 cls.book3.authors.add(cls.author3)
28 cls.book4.authors.add(cls.author4)
29
30 cls.reader1 = Reader.objects.create(name="Amy")
31 cls.reader2 = Reader.objects.create(name="Belinda")
32
33 cls.reader1.books_read.add(cls.book1, cls.book4)
34 cls.reader2.books_read.add(cls.book2, cls.book4)
35
36 cls.house1 = House.objects.create(name="b1", address="1")
37 cls.house2 = House.objects.create(name="b2", address="2")
38
39 cls.room1 = Room.objects.create(name="a1", house=cls.house1)
40 cls.room2 = Room.objects.create(name="a2", house=cls.house2)
41
42 cls.house1.main_room = cls.room1
43 cls.house1.save()
44 cls.house2.main_room = cls.room2
45 cls.house2.save()
46
47 def test_unknown(self):
48 book1 = Book.objects.get(id=self.book1.id)
49 with self.assertRaises(AttributeError):
50 prefetch_related_objects([book1], "unknown_attribute")
51
52 def test_m2m_forward(self):
53 book1 = Book.objects.get(id=self.book1.id)
54 with self.assertNumQueries(1):
55 prefetch_related_objects([book1], "authors")
56
57 with self.assertNumQueries(0):
58 self.assertCountEqual(
59 book1.authors.all(), [self.author1, self.author2, self.author3]
60 )
61
62 def test_m2m_reverse(self):
63 author1 = Author.objects.get(id=self.author1.id)
64 with self.assertNumQueries(1):
65 prefetch_related_objects([author1], "books")
66
67 with self.assertNumQueries(0):
68 self.assertCountEqual(author1.books.all(), [self.book1, self.book2])
69
70 def test_foreignkey_forward(self):
71 authors = list(Author.objects.all())
72 with self.assertNumQueries(1) as ctx:
73 prefetch_related_objects(authors, "first_book")
74 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
75
76 with self.assertNumQueries(0):
77 [author.first_book for author in authors]
78
79 authors = list(Author.objects.all())
80 with self.assertNumQueries(1) as ctx:
81 prefetch_related_objects(
82 authors,
83 Prefetch("first_book", queryset=Book.objects.order_by("-title")),
84 )
85 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
86
87 def test_foreignkey_reverse(self):
88 books = list(Book.objects.all())
89 with self.assertNumQueries(1) as ctx:
90 prefetch_related_objects(books, "first_time_authors")
91 self.assertIn("ORDER BY", ctx.captured_queries[0]["sql"])
92
93 with self.assertNumQueries(0):
94 [list(book.first_time_authors.all()) for book in books]
95
96 books = list(Book.objects.all())
97 with self.assertNumQueries(1) as ctx:
98 prefetch_related_objects(
99 books,
100 Prefetch(
101 "first_time_authors",
102 queryset=Author.objects.order_by("-name"),
103 ),
104 )
105 self.assertIn("ORDER BY", ctx.captured_queries[0]["sql"])
106
107 def test_one_to_one_forward(self):
108 houses = list(House.objects.all())
109 with self.assertNumQueries(1) as ctx:
110 prefetch_related_objects(houses, "main_room")
111 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
112
113 with self.assertNumQueries(0):
114 [house.main_room for house in houses]
115
116 houses = list(House.objects.all())
117 with self.assertNumQueries(1) as ctx:
118 prefetch_related_objects(
119 houses,
120 Prefetch("main_room", queryset=Room.objects.order_by("-name")),
121 )
122 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
123
124 def test_one_to_one_reverse(self):
125 rooms = list(Room.objects.all())
126 with self.assertNumQueries(1) as ctx:
127 prefetch_related_objects(rooms, "main_room_of")
128 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
129
130 with self.assertNumQueries(0):
131 [room.main_room_of for room in rooms]
132
133 rooms = list(Room.objects.all())
134 with self.assertNumQueries(1) as ctx:
135 prefetch_related_objects(
136 rooms,
137 Prefetch("main_room_of", queryset=House.objects.order_by("-name")),
138 )
139 self.assertNotIn("ORDER BY", ctx.captured_queries[0]["sql"])
140
141 def test_m2m_then_m2m(self):
142 """A m2m can be followed through another m2m."""
143 authors = list(Author.objects.all())
144 with self.assertNumQueries(2):
145 prefetch_related_objects(authors, "books__read_by")
146
147 with self.assertNumQueries(0):
148 self.assertEqual(
149 [
150 [[str(r) for r in b.read_by.all()] for b in a.books.all()]
151 for a in authors
152 ],
153 [
154 [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
155 [["Amy"]], # Anne - Poems
156 [["Amy"], []], # Emily - Poems, Wuthering Heights
157 [["Amy", "Belinda"]], # Jane - Sense and Sense
158 ],
159 )
160
161 def test_prefetch_object(self):
162 book1 = Book.objects.get(id=self.book1.id)
163 with self.assertNumQueries(1):
164 prefetch_related_objects([book1], Prefetch("authors"))
165
166 with self.assertNumQueries(0):
167 self.assertCountEqual(
168 book1.authors.all(), [self.author1, self.author2, self.author3]
169 )
170
171 def test_prefetch_object_twice(self):
172 book1 = Book.objects.get(id=self.book1.id)
173 book2 = Book.objects.get(id=self.book2.id)
174 with self.assertNumQueries(1):
175 prefetch_related_objects([book1], Prefetch("authors"))
176 with self.assertNumQueries(1):
177 prefetch_related_objects([book1, book2], Prefetch("authors"))
178 with self.assertNumQueries(0):
179 self.assertCountEqual(book2.authors.all(), [self.author1])
180
181 def test_prefetch_object_to_attr(self):
182 book1 = Book.objects.get(id=self.book1.id)
183 with self.assertNumQueries(1):
184 prefetch_related_objects(
185 [book1], Prefetch("authors", to_attr="the_authors")
186 )
187
188 with self.assertNumQueries(0):
189 self.assertCountEqual(
190 book1.the_authors, [self.author1, self.author2, self.author3]
191 )
192
193 def test_prefetch_object_to_attr_twice(self):
194 book1 = Book.objects.get(id=self.book1.id)
195 book2 = Book.objects.get(id=self.book2.id)
196 with self.assertNumQueries(1):
197 prefetch_related_objects(
198 [book1],
199 Prefetch("authors", to_attr="the_authors"),
200 )
201 with self.assertNumQueries(1):
202 prefetch_related_objects(
203 [book1, book2],
204 Prefetch("authors", to_attr="the_authors"),
205 )
206 with self.assertNumQueries(0):
207 self.assertCountEqual(book2.the_authors, [self.author1])
208
209 def test_prefetch_queryset(self):
210 book1 = Book.objects.get(id=self.book1.id)
211 with self.assertNumQueries(1):
212 prefetch_related_objects(
213 [book1],
214 Prefetch(
215 "authors",
216 queryset=Author.objects.filter(
217 id__in=[self.author1.id, self.author2.id]
218 ),
219 ),
220 )
221
222 with self.assertNumQueries(0):
223 self.assertCountEqual(book1.authors.all(), [self.author1, self.author2])