Skip to content

Commit 5d7fb5a

Browse files
committed
edits
1 parent 92afc21 commit 5d7fb5a

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

tests/model_fields_/test_embedded_model.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
F,
99
Max,
1010
OuterRef,
11-
Subquery,
1211
Sum,
1312
)
1413
from django.test import SimpleTestCase, TestCase
@@ -189,60 +188,54 @@ class MyModel(models.Model):
189188
"Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
190189
)
191190

192-
class SubqueryExistsTest(TestCase):
193-
def setUp(self):
191+
class SubqueryExistsTests(TestCase):
192+
def setUpTestData(self):
194193
# 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-
194+
address1 = Address(city="New York", state="NY", zip_code=10001)
195+
address2 = Address(city="Boston", state="MA", zip_code=20002)
196+
author1 = Author(name="Alice", age=30, address=address1)
197+
author2 = Author(name="Bob", age=40, address=address2)
198+
book1 = Book.objects.create(name="Book 1", author=author1)
199+
book2 = Book.objects.create(name="Book 2", author=author2)
200+
Book.objects.create(name="Book 3", author=author2)
201+
Book.objects.create(name="Book 4", author=author2)
202+
Book.objects.create(name="Book 5", author=author1)
205203
library1 = Library.objects.create(
206-
name="Central Library", location="Downtown", best_seller="Book A"
204+
name="Central Library", location="Downtown", best_seller="Book 1"
207205
)
208206
library2 = Library.objects.create(
209-
name="Community Library", location="Suburbs", best_seller="Book A"
207+
name="Community Library", location="Suburbs", best_seller="Book 1"
210208
)
211-
212209
# Add books to libraries
213210
library1.books.add(book1, book2)
214211
library2.books.add(book2)
215212

216213
def test_exists_subquery(self):
217214
subquery = Book.objects.filter(
218-
author__name=OuterRef("name"), author__address__city="Boston"
215+
author__name=OuterRef("author__name"), author__address__city="Boston"
219216
)
220-
queryset = Author.objects.filter(Exists(subquery))
221-
222-
self.assertEqual(queryset.count(), 1)
217+
queryset = Book.objects.filter(Exists(subquery)).order_by("name")
218+
self.assertEqual(queryset.count(), 3)
219+
self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
223220

224221
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-
222+
subquery = Book.objects.filter(author__age__gt=35).values("author__name")
223+
queryset = Book.objects.filter(author__name__in=subquery).order_by("name")
228224
self.assertEqual(queryset.count(), 3)
229-
self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name)
225+
self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
230226

231227
def test_range_query(self):
232-
queryset = Author.objects.filter(age__range=(25, 45)).order_by("name")
233-
228+
queryset = Book.objects.filter(author__age__range=(25, 30)).order_by("author__name")
234229
self.assertEqual(queryset.count(), 2)
235-
self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name)
230+
self.assertQuerySetEqual(queryset, ["Alice", "Alice"], lambda book: book.author.name)
236231

237232
def test_exists_with_foreign_object(self):
238233
subquery = Library.objects.filter(best_seller=OuterRef("name"))
239234
queryset = Book.objects.filter(Exists(subquery))
240-
241235
self.assertEqual(queryset.count(), 1)
242-
self.assertEqual(queryset.first().name, "Book A")
236+
self.assertEqual(queryset.first().name, "Book 1")
243237

244238
def test_foreign_field_with_ranges(self):
245239
queryset = Library.objects.filter(books__author__age__range=(25, 35))
246-
247240
self.assertEqual(queryset.count(), 1)
248241
self.assertEqual(queryset.first().name, "Central Library")

0 commit comments

Comments
 (0)