|
2 | 2 |
|
3 | 3 | from django.core.exceptions import FieldDoesNotExist, ValidationError
|
4 | 4 | from django.db import models
|
5 |
| -from django.db.models import ExpressionWrapper, F, Max, Sum |
| 5 | +from django.db.models import ( |
| 6 | + Exists, |
| 7 | + ExpressionWrapper, |
| 8 | + F, |
| 9 | + Max, |
| 10 | + OuterRef, |
| 11 | + Sum, |
| 12 | +) |
6 | 13 | from django.test import SimpleTestCase, TestCase
|
7 | 14 | from django.test.utils import isolate_apps
|
8 | 15 |
|
|
15 | 22 | Book,
|
16 | 23 | Data,
|
17 | 24 | Holder,
|
| 25 | + Library, |
18 | 26 | )
|
19 | 27 | from .utils import truncate_ms
|
20 | 28 |
|
@@ -214,3 +222,45 @@ class MyModel(models.Model):
|
214 | 222 | msg,
|
215 | 223 | "Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
|
216 | 224 | )
|
| 225 | + |
| 226 | + |
| 227 | +class SubqueryExistsTests(TestCase): |
| 228 | + def setUpTestData(self): |
| 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) |
| 238 | + library1 = Library.objects.create( |
| 239 | + name="Central Library", location="Downtown", best_seller="Book 1" |
| 240 | + ) |
| 241 | + library2 = Library.objects.create( |
| 242 | + name="Community Library", location="Suburbs", best_seller="Book 1" |
| 243 | + ) |
| 244 | + library1.books.add(book1, book2) |
| 245 | + library2.books.add(book2) |
| 246 | + |
| 247 | + def test_exists_subquery(self): |
| 248 | + subquery = Book.objects.filter( |
| 249 | + author__name=OuterRef("author__name"), author__address__city="Boston" |
| 250 | + ) |
| 251 | + qs = Book.objects.filter(Exists(subquery)).order_by("name") |
| 252 | + self.assertQuerySetEqual(qs, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
| 253 | + |
| 254 | + def test_in_subquery(self): |
| 255 | + names = Book.objects.filter(author__age__gt=35).values("author__name") |
| 256 | + qs = Book.objects.filter(author__name__in=names).order_by("name") |
| 257 | + self.assertQuerySetEqual(qs, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
| 258 | + |
| 259 | + def test_exists_with_foreign_object(self): |
| 260 | + subquery = Library.objects.filter(best_seller=OuterRef("name")) |
| 261 | + qs = Book.objects.filter(Exists(subquery)) |
| 262 | + self.assertEqual(qs.first().name, "Book 1") |
| 263 | + |
| 264 | + def test_foreign_field_with_range(self): |
| 265 | + qs = Library.objects.filter(books__author__age__range=(25, 35)) |
| 266 | + self.assertEqual(qs.first().name, "Central Library") |
0 commit comments