|
8 | 8 | F,
|
9 | 9 | Max,
|
10 | 10 | OuterRef,
|
11 |
| - Subquery, |
12 | 11 | Sum,
|
13 | 12 | )
|
14 | 13 | from django.test import SimpleTestCase, TestCase
|
@@ -189,60 +188,54 @@ class MyModel(models.Model):
|
189 | 188 | "Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
|
190 | 189 | )
|
191 | 190 |
|
192 |
| -class SubqueryExistsTest(TestCase): |
193 |
| - def setUp(self): |
| 191 | +class SubqueryExistsTests(TestCase): |
| 192 | + def setUpTestData(self): |
194 | 193 | # Create test data
|
195 |
| - address1 = Address.objects.create(city="New York", state="NY", zip_code=10001) |
196 |
| - address2 = Address.objects.create(city="Boston", state="MA", zip_code=20002) |
197 |
| - author1 = Author.objects.create(name="Alice", age=30, address=address1) |
198 |
| - author2 = Author.objects.create(name="Bob", age=40, address=address2) |
199 |
| - book1 = Book.objects.create(name="Book A", author=author1) |
200 |
| - book2 = Book.objects.create(name="Book B", author=author2) |
201 |
| - Book.objects.create(name="Book C", author=author2) |
202 |
| - Book.objects.create(name="Book D", author=author2) |
203 |
| - Book.objects.create(name="Book E", author=author1) |
204 |
| - |
| 194 | + address1 = Address(city="New York", state="NY", zip_code=10001) |
| 195 | + address2 = Address(city="Boston", state="MA", zip_code=20002) |
| 196 | + author1 = Author(name="Alice", age=30, address=address1) |
| 197 | + author2 = Author(name="Bob", age=40, address=address2) |
| 198 | + book1 = Book.objects.create(name="Book 1", author=author1) |
| 199 | + book2 = Book.objects.create(name="Book 2", author=author2) |
| 200 | + Book.objects.create(name="Book 3", author=author2) |
| 201 | + Book.objects.create(name="Book 4", author=author2) |
| 202 | + Book.objects.create(name="Book 5", author=author1) |
205 | 203 | library1 = Library.objects.create(
|
206 |
| - name="Central Library", location="Downtown", best_seller="Book A" |
| 204 | + name="Central Library", location="Downtown", best_seller="Book 1" |
207 | 205 | )
|
208 | 206 | library2 = Library.objects.create(
|
209 |
| - name="Community Library", location="Suburbs", best_seller="Book A" |
| 207 | + name="Community Library", location="Suburbs", best_seller="Book 1" |
210 | 208 | )
|
211 |
| - |
212 | 209 | # Add books to libraries
|
213 | 210 | library1.books.add(book1, book2)
|
214 | 211 | library2.books.add(book2)
|
215 | 212 |
|
216 | 213 | def test_exists_subquery(self):
|
217 | 214 | subquery = Book.objects.filter(
|
218 |
| - author__name=OuterRef("name"), author__address__city="Boston" |
| 215 | + author__name=OuterRef("author__name"), author__address__city="Boston" |
219 | 216 | )
|
220 |
| - queryset = Author.objects.filter(Exists(subquery)) |
221 |
| - |
222 |
| - self.assertEqual(queryset.count(), 1) |
| 217 | + queryset = Book.objects.filter(Exists(subquery)).order_by("name") |
| 218 | + self.assertEqual(queryset.count(), 3) |
| 219 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
223 | 220 |
|
224 | 221 | def test_in_subquery(self):
|
225 |
| - subquery = Author.objects.filter(age__gt=35).values("name") |
226 |
| - queryset = Book.objects.filter(author__name__in=Subquery(subquery)).order_by("name") |
227 |
| - |
| 222 | + subquery = Book.objects.filter(author__age__gt=35).values("author__name") |
| 223 | + queryset = Book.objects.filter(author__name__in=subquery).order_by("name") |
228 | 224 | self.assertEqual(queryset.count(), 3)
|
229 |
| - self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) |
| 225 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
230 | 226 |
|
231 | 227 | def test_range_query(self):
|
232 |
| - queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") |
233 |
| - |
| 228 | + queryset = Book.objects.filter(author__age__range=(25, 30)).order_by("author__name") |
234 | 229 | self.assertEqual(queryset.count(), 2)
|
235 |
| - self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) |
| 230 | + self.assertQuerySetEqual(queryset, ["Alice", "Alice"], lambda book: book.author.name) |
236 | 231 |
|
237 | 232 | def test_exists_with_foreign_object(self):
|
238 | 233 | subquery = Library.objects.filter(best_seller=OuterRef("name"))
|
239 | 234 | queryset = Book.objects.filter(Exists(subquery))
|
240 |
| - |
241 | 235 | self.assertEqual(queryset.count(), 1)
|
242 |
| - self.assertEqual(queryset.first().name, "Book A") |
| 236 | + self.assertEqual(queryset.first().name, "Book 1") |
243 | 237 |
|
244 | 238 | def test_foreign_field_with_ranges(self):
|
245 | 239 | queryset = Library.objects.filter(books__author__age__range=(25, 35))
|
246 |
| - |
247 | 240 | self.assertEqual(queryset.count(), 1)
|
248 | 241 | self.assertEqual(queryset.first().name, "Central Library")
|
0 commit comments