|
2 | 2 |
|
3 | 3 | from django.core.exceptions import 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 | + Subquery, |
| 12 | + Sum, |
| 13 | +) |
6 | 14 | from django.test import SimpleTestCase, TestCase
|
7 | 15 | from django.test.utils import isolate_apps
|
8 | 16 |
|
|
15 | 23 | Book,
|
16 | 24 | Data,
|
17 | 25 | Holder,
|
| 26 | + Library, |
18 | 27 | )
|
19 | 28 | from .utils import truncate_ms
|
20 | 29 |
|
@@ -179,3 +188,61 @@ class MyModel(models.Model):
|
179 | 188 | msg,
|
180 | 189 | "Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
|
181 | 190 | )
|
| 191 | + |
| 192 | +class SubqueryExistsTest(TestCase): |
| 193 | + def setUp(self): |
| 194 | + # 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 | + |
| 205 | + library1 = Library.objects.create( |
| 206 | + name="Central Library", location="Downtown", best_seller="Book A" |
| 207 | + ) |
| 208 | + library2 = Library.objects.create( |
| 209 | + name="Community Library", location="Suburbs", best_seller="Book A" |
| 210 | + ) |
| 211 | + |
| 212 | + # Add books to libraries |
| 213 | + library1.books.add(book1, book2) |
| 214 | + library2.books.add(book2) |
| 215 | + |
| 216 | + def test_exists_subquery(self): |
| 217 | + subquery = Book.objects.filter( |
| 218 | + author__name=OuterRef("name"), author__address__city="Boston" |
| 219 | + ) |
| 220 | + queryset = Author.objects.filter(Exists(subquery)) |
| 221 | + |
| 222 | + self.assertEqual(queryset.count(), 1) |
| 223 | + |
| 224 | + 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 | + |
| 228 | + self.assertEqual(queryset.count(), 3) |
| 229 | + self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) |
| 230 | + |
| 231 | + def test_range_query(self): |
| 232 | + queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") |
| 233 | + |
| 234 | + self.assertEqual(queryset.count(), 2) |
| 235 | + self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) |
| 236 | + |
| 237 | + def test_exists_with_foreign_object(self): |
| 238 | + subquery = Library.objects.filter(best_seller=OuterRef("name")) |
| 239 | + queryset = Book.objects.filter(Exists(subquery)) |
| 240 | + |
| 241 | + self.assertEqual(queryset.count(), 1) |
| 242 | + self.assertEqual(queryset.first().name, "Book A") |
| 243 | + |
| 244 | + def test_foreign_field_with_ranges(self): |
| 245 | + queryset = Library.objects.filter(books__author__age__range=(25, 35)) |
| 246 | + |
| 247 | + self.assertEqual(queryset.count(), 1) |
| 248 | + self.assertEqual(queryset.first().name, "Central Library") |
0 commit comments