|
3 | 3 | from django.db.models import (
|
4 | 4 | Exists,
|
5 | 5 | OuterRef,
|
6 |
| - Subquery, |
7 | 6 | )
|
8 | 7 | from django.test import SimpleTestCase, TestCase
|
9 | 8 | from django.test.utils import isolate_apps
|
@@ -131,60 +130,54 @@ class MyModel(models.Model):
|
131 | 130 | )
|
132 | 131 |
|
133 | 132 |
|
134 |
| -class SubqueryExistsTest(TestCase): |
135 |
| - def setUp(self): |
| 133 | +class SubqueryExistsTests(TestCase): |
| 134 | + def setUpTestData(self): |
136 | 135 | # 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 |
| - |
| 136 | + address1 = Address(city="New York", state="NY", zip_code=10001) |
| 137 | + address2 = Address(city="Boston", state="MA", zip_code=20002) |
| 138 | + author1 = Author(name="Alice", age=30, address=address1) |
| 139 | + author2 = Author(name="Bob", age=40, address=address2) |
| 140 | + book1 = Book.objects.create(name="Book 1", author=author1) |
| 141 | + book2 = Book.objects.create(name="Book 2", author=author2) |
| 142 | + Book.objects.create(name="Book 3", author=author2) |
| 143 | + Book.objects.create(name="Book 4", author=author2) |
| 144 | + Book.objects.create(name="Book 5", author=author1) |
147 | 145 | library1 = Library.objects.create(
|
148 |
| - name="Central Library", location="Downtown", best_seller="Book A" |
| 146 | + name="Central Library", location="Downtown", best_seller="Book 1" |
149 | 147 | )
|
150 | 148 | library2 = Library.objects.create(
|
151 |
| - name="Community Library", location="Suburbs", best_seller="Book A" |
| 149 | + name="Community Library", location="Suburbs", best_seller="Book 1" |
152 | 150 | )
|
153 |
| - |
154 | 151 | # Add books to libraries
|
155 | 152 | library1.books.add(book1, book2)
|
156 | 153 | library2.books.add(book2)
|
157 | 154 |
|
158 | 155 | def test_exists_subquery(self):
|
159 | 156 | subquery = Book.objects.filter(
|
160 |
| - author__name=OuterRef("name"), author__address__city="Boston" |
| 157 | + author__name=OuterRef("author__name"), author__address__city="Boston" |
161 | 158 | )
|
162 |
| - queryset = Author.objects.filter(Exists(subquery)) |
163 |
| - |
164 |
| - self.assertEqual(queryset.count(), 1) |
| 159 | + queryset = Book.objects.filter(Exists(subquery)).order_by("name") |
| 160 | + self.assertEqual(queryset.count(), 3) |
| 161 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
165 | 162 |
|
166 | 163 | 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 |
| - |
| 164 | + subquery = Book.objects.filter(author__age__gt=35).values("author__name") |
| 165 | + queryset = Book.objects.filter(author__name__in=subquery).order_by("name") |
170 | 166 | self.assertEqual(queryset.count(), 3)
|
171 |
| - self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) |
| 167 | + self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name) |
172 | 168 |
|
173 | 169 | def test_range_query(self):
|
174 |
| - queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") |
175 |
| - |
| 170 | + queryset = Book.objects.filter(author__age__range=(25, 30)).order_by("author__name") |
176 | 171 | self.assertEqual(queryset.count(), 2)
|
177 |
| - self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) |
| 172 | + self.assertQuerySetEqual(queryset, ["Alice", "Alice"], lambda book: book.author.name) |
178 | 173 |
|
179 | 174 | def test_exists_with_foreign_object(self):
|
180 | 175 | subquery = Library.objects.filter(best_seller=OuterRef("name"))
|
181 | 176 | queryset = Book.objects.filter(Exists(subquery))
|
182 |
| - |
183 | 177 | self.assertEqual(queryset.count(), 1)
|
184 |
| - self.assertEqual(queryset.first().name, "Book A") |
| 178 | + self.assertEqual(queryset.first().name, "Book 1") |
185 | 179 |
|
186 | 180 | def test_foreign_field_with_ranges(self):
|
187 | 181 | queryset = Library.objects.filter(books__author__age__range=(25, 35))
|
188 |
| - |
189 | 182 | self.assertEqual(queryset.count(), 1)
|
190 | 183 | self.assertEqual(queryset.first().name, "Central Library")
|
0 commit comments