Skip to content

Commit d146960

Browse files
committed
edits
1 parent a6f2f2f commit d146960

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
@@ -224,60 +223,54 @@ class MyModel(models.Model):
224223
"Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
225224
)
226225

227-
class SubqueryExistsTest(TestCase):
228-
def setUp(self):
226+
class SubqueryExistsTests(TestCase):
227+
def setUpTestData(self):
229228
# Create test data
230-
address1 = Address.objects.create(city="New York", state="NY", zip_code=10001)
231-
address2 = Address.objects.create(city="Boston", state="MA", zip_code=20002)
232-
author1 = Author.objects.create(name="Alice", age=30, address=address1)
233-
author2 = Author.objects.create(name="Bob", age=40, address=address2)
234-
book1 = Book.objects.create(name="Book A", author=author1)
235-
book2 = Book.objects.create(name="Book B", author=author2)
236-
Book.objects.create(name="Book C", author=author2)
237-
Book.objects.create(name="Book D", author=author2)
238-
Book.objects.create(name="Book E", author=author1)
239-
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)
240238
library1 = Library.objects.create(
241-
name="Central Library", location="Downtown", best_seller="Book A"
239+
name="Central Library", location="Downtown", best_seller="Book 1"
242240
)
243241
library2 = Library.objects.create(
244-
name="Community Library", location="Suburbs", best_seller="Book A"
242+
name="Community Library", location="Suburbs", best_seller="Book 1"
245243
)
246-
247244
# Add books to libraries
248245
library1.books.add(book1, book2)
249246
library2.books.add(book2)
250247

251248
def test_exists_subquery(self):
252249
subquery = Book.objects.filter(
253-
author__name=OuterRef("name"), author__address__city="Boston"
250+
author__name=OuterRef("author__name"), author__address__city="Boston"
254251
)
255-
queryset = Author.objects.filter(Exists(subquery))
256-
257-
self.assertEqual(queryset.count(), 1)
252+
queryset = Book.objects.filter(Exists(subquery)).order_by("name")
253+
self.assertEqual(queryset.count(), 3)
254+
self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
258255

259256
def test_in_subquery(self):
260-
subquery = Author.objects.filter(age__gt=35).values("name")
261-
queryset = Book.objects.filter(author__name__in=Subquery(subquery)).order_by("name")
262-
257+
subquery = Book.objects.filter(author__age__gt=35).values("author__name")
258+
queryset = Book.objects.filter(author__name__in=subquery).order_by("name")
263259
self.assertEqual(queryset.count(), 3)
264-
self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name)
260+
self.assertQuerySetEqual(queryset, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
265261

266262
def test_range_query(self):
267-
queryset = Author.objects.filter(age__range=(25, 45)).order_by("name")
268-
263+
queryset = Book.objects.filter(author__age__range=(25, 30)).order_by("author__name")
269264
self.assertEqual(queryset.count(), 2)
270-
self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name)
265+
self.assertQuerySetEqual(queryset, ["Alice", "Alice"], lambda book: book.author.name)
271266

272267
def test_exists_with_foreign_object(self):
273268
subquery = Library.objects.filter(best_seller=OuterRef("name"))
274269
queryset = Book.objects.filter(Exists(subquery))
275-
276270
self.assertEqual(queryset.count(), 1)
277-
self.assertEqual(queryset.first().name, "Book A")
271+
self.assertEqual(queryset.first().name, "Book 1")
278272

279273
def test_foreign_field_with_ranges(self):
280274
queryset = Library.objects.filter(books__author__age__range=(25, 35))
281-
282275
self.assertEqual(queryset.count(), 1)
283276
self.assertEqual(queryset.first().name, "Central Library")

0 commit comments

Comments
 (0)