|
1 | 1 | from django.core.exceptions import ValidationError
|
2 | 2 | from django.db import models
|
| 3 | +from django.db.models import ( |
| 4 | + Exists, |
| 5 | + OuterRef, |
| 6 | + Subquery, |
| 7 | +) |
3 | 8 | from django.test import SimpleTestCase, TestCase
|
4 | 9 | from django.test.utils import isolate_apps
|
5 | 10 |
|
|
11 | 16 | Book,
|
12 | 17 | Data,
|
13 | 18 | Holder,
|
| 19 | + Library, |
14 | 20 | )
|
15 | 21 |
|
16 | 22 |
|
@@ -123,3 +129,62 @@ class MyModel(models.Model):
|
123 | 129 | self.assertEqual(
|
124 | 130 | msg, "Embedded models cannot have relational fields (Target.key is a ForeignKey)."
|
125 | 131 | )
|
| 132 | + |
| 133 | + |
| 134 | +class SubqueryExistsTest(TestCase): |
| 135 | + def setUp(self): |
| 136 | + # Create test data |
| 137 | + address1 = Address.objects.create(city="New York", state="NY", zip_code=10001) |
| 138 | + address2 = Address.objects.create(city="Boston", state="MA", zip_code=20002) |
| 139 | + author1 = Author.objects.create(name="Alice", age=30, address=address1) |
| 140 | + author2 = Author.objects.create(name="Bob", age=40, address=address2) |
| 141 | + book1 = Book.objects.create(name="Book A", author=author1) |
| 142 | + book2 = Book.objects.create(name="Book B", author=author2) |
| 143 | + Book.objects.create(name="Book C", author=author2) |
| 144 | + Book.objects.create(name="Book D", author=author2) |
| 145 | + Book.objects.create(name="Book E", author=author1) |
| 146 | + |
| 147 | + library1 = Library.objects.create( |
| 148 | + name="Central Library", location="Downtown", best_seller="Book A" |
| 149 | + ) |
| 150 | + library2 = Library.objects.create( |
| 151 | + name="Community Library", location="Suburbs", best_seller="Book A" |
| 152 | + ) |
| 153 | + |
| 154 | + # Add books to libraries |
| 155 | + library1.books.add(book1, book2) |
| 156 | + library2.books.add(book2) |
| 157 | + |
| 158 | + def test_exists_subquery(self): |
| 159 | + subquery = Book.objects.filter( |
| 160 | + author__name=OuterRef("name"), author__address__city="Boston" |
| 161 | + ) |
| 162 | + queryset = Author.objects.filter(Exists(subquery)) |
| 163 | + |
| 164 | + self.assertEqual(queryset.count(), 1) |
| 165 | + |
| 166 | + def test_in_subquery(self): |
| 167 | + subquery = Author.objects.filter(age__gt=35).values("name") |
| 168 | + queryset = Book.objects.filter(author__name__in=Subquery(subquery)).order_by("name") |
| 169 | + |
| 170 | + self.assertEqual(queryset.count(), 3) |
| 171 | + self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) |
| 172 | + |
| 173 | + def test_range_query(self): |
| 174 | + queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") |
| 175 | + |
| 176 | + self.assertEqual(queryset.count(), 2) |
| 177 | + self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) |
| 178 | + |
| 179 | + def test_exists_with_foreign_object(self): |
| 180 | + subquery = Library.objects.filter(best_seller=OuterRef("name")) |
| 181 | + queryset = Book.objects.filter(Exists(subquery)) |
| 182 | + |
| 183 | + self.assertEqual(queryset.count(), 1) |
| 184 | + self.assertEqual(queryset.first().name, "Book A") |
| 185 | + |
| 186 | + def test_foreign_field_with_ranges(self): |
| 187 | + queryset = Library.objects.filter(books__author__age__range=(25, 35)) |
| 188 | + |
| 189 | + self.assertEqual(queryset.count(), 1) |
| 190 | + self.assertEqual(queryset.first().name, "Central Library") |
0 commit comments