|
8 | 8 | F,
|
9 | 9 | Max,
|
10 | 10 | OuterRef,
|
11 |
| - Subquery, |
12 | 11 | Sum,
|
13 | 12 | )
|
14 | 13 | from django.test import SimpleTestCase, TestCase
|
@@ -224,60 +223,54 @@ class MyModel(models.Model):
|
224 | 223 | "Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
|
225 | 224 | )
|
226 | 225 |
|
227 |
| -class SubqueryExistsTest(TestCase): |
228 |
| - def setUp(self): |
| 226 | +class SubqueryExistsTests(TestCase): |
| 227 | + def setUpTestData(self): |
229 | 228 | # Create test data
|
230 |
| - address1 = Address.objects.create(city="New York", state="NY", zip_code=10001) |
231 |
| - address2 = Address.objects.create(city="Boston", state="MA", zip_code=20002) |
232 |
| - author1 = Author.objects.create(name="Alice", age=30, address=address1) |
233 |
| - author2 = Author.objects.create(name="Bob", age=40, address=address2) |
234 |
| - book1 = Book.objects.create(name="Book A", author=author1) |
235 |
| - book2 = Book.objects.create(name="Book B", author=author2) |
236 |
| - Book.objects.create(name="Book C", author=author2) |
237 |
| - Book.objects.create(name="Book D", author=author2) |
238 |
| - Book.objects.create(name="Book E", author=author1) |
239 |
| - |
| 229 | + address1 = Address(city="New York", state="NY", zip_code=10001) |
| 230 | + address2 = Address(city="Boston", state="MA", zip_code=20002) |
| 231 | + author1 = Author(name="Alice", age=30, address=address1) |
| 232 | + author2 = Author(name="Bob", age=40, address=address2) |
| 233 | + book1 = Book.objects.create(name="Book 1", author=author1) |
| 234 | + book2 = Book.objects.create(name="Book 2", author=author2) |
| 235 | + Book.objects.create(name="Book 3", author=author2) |
| 236 | + Book.objects.create(name="Book 4", author=author2) |
| 237 | + Book.objects.create(name="Book 5", author=author1) |
240 | 238 | library1 = Library.objects.create(
|
241 |
| - name="Central Library", location="Downtown", best_seller="Book A" |
| 239 | + name="Central Library", location="Downtown", best_seller="Book 1" |
242 | 240 | )
|
243 | 241 | library2 = Library.objects.create(
|
244 |
| - name="Community Library", location="Suburbs", best_seller="Book A" |
| 242 | + name="Community Library", location="Suburbs", best_seller="Book 1" |
245 | 243 | )
|
246 |
| - |
247 | 244 | # Add books to libraries
|
248 | 245 | library1.books.add(book1, book2)
|
249 | 246 | library2.books.add(book2)
|
250 | 247 |
|
251 | 248 | def test_exists_subquery(self):
|
252 | 249 | subquery = Book.objects.filter(
|
253 |
| - author__name=OuterRef("name"), author__address__city="Boston" |
| 250 | + author__name=OuterRef("author__name"), author__address__city="Boston" |
254 | 251 | )
|
255 |
| - queryset = Author.objects.filter(Exists(subquery)) |
256 |
| - |
257 |
| - self.assertEqual(queryset.count(), 1) |
| 252 | + queryset = Book.objects.filter(Exists(subquery)).order_by("name") |
| 253 | + self.assertEqual(queryset.count(), 3) |
| 254 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
258 | 255 |
|
259 | 256 | def test_in_subquery(self):
|
260 |
| - subquery = Author.objects.filter(age__gt=35).values("name") |
261 |
| - queryset = Book.objects.filter(author__name__in=Subquery(subquery)).order_by("name") |
262 |
| - |
| 257 | + subquery = Book.objects.filter(author__age__gt=35).values("author__name") |
| 258 | + queryset = Book.objects.filter(author__name__in=subquery).order_by("name") |
263 | 259 | self.assertEqual(queryset.count(), 3)
|
264 |
| - self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) |
| 260 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
265 | 261 |
|
266 | 262 | def test_range_query(self):
|
267 |
| - queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") |
268 |
| - |
| 263 | + queryset = Book.objects.filter(author__age__range=(25, 30)).order_by("author__name") |
269 | 264 | self.assertEqual(queryset.count(), 2)
|
270 |
| - self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) |
| 265 | + self.assertQuerySetEqual(queryset, ["Alice", "Alice"], lambda book: book.author.name) |
271 | 266 |
|
272 | 267 | def test_exists_with_foreign_object(self):
|
273 | 268 | subquery = Library.objects.filter(best_seller=OuterRef("name"))
|
274 | 269 | queryset = Book.objects.filter(Exists(subquery))
|
275 |
| - |
276 | 270 | self.assertEqual(queryset.count(), 1)
|
277 |
| - self.assertEqual(queryset.first().name, "Book A") |
| 271 | + self.assertEqual(queryset.first().name, "Book 1") |
278 | 272 |
|
279 | 273 | def test_foreign_field_with_ranges(self):
|
280 | 274 | queryset = Library.objects.filter(books__author__age__range=(25, 35))
|
281 |
| - |
282 | 275 | self.assertEqual(queryset.count(), 1)
|
283 | 276 | self.assertEqual(queryset.first().name, "Central Library")
|
0 commit comments