Skip to content

Commit 10dbdc3

Browse files
committed
edits
1 parent 77d6286 commit 10dbdc3

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
@@ -3,7 +3,6 @@
33
from django.db.models import (
44
Exists,
55
OuterRef,
6-
Subquery,
76
)
87
from django.test import SimpleTestCase, TestCase
98
from django.test.utils import isolate_apps
@@ -131,60 +130,54 @@ class MyModel(models.Model):
131130
)
132131

133132

134-
class SubqueryExistsTest(TestCase):
135-
def setUp(self):
133+
class SubqueryExistsTests(TestCase):
134+
def setUpTestData(self):
136135
# 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)
147145
library1 = Library.objects.create(
148-
name="Central Library", location="Downtown", best_seller="Book A"
146+
name="Central Library", location="Downtown", best_seller="Book 1"
149147
)
150148
library2 = Library.objects.create(
151-
name="Community Library", location="Suburbs", best_seller="Book A"
149+
name="Community Library", location="Suburbs", best_seller="Book 1"
152150
)
153-
154151
# Add books to libraries
155152
library1.books.add(book1, book2)
156153
library2.books.add(book2)
157154

158155
def test_exists_subquery(self):
159156
subquery = Book.objects.filter(
160-
author__name=OuterRef("name"), author__address__city="Boston"
157+
author__name=OuterRef("author__name"), author__address__city="Boston"
161158
)
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)
165162

166163
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")
170166
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)
172168

173169
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")
176171
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)
178173

179174
def test_exists_with_foreign_object(self):
180175
subquery = Library.objects.filter(best_seller=OuterRef("name"))
181176
queryset = Book.objects.filter(Exists(subquery))
182-
183177
self.assertEqual(queryset.count(), 1)
184-
self.assertEqual(queryset.first().name, "Book A")
178+
self.assertEqual(queryset.first().name, "Book 1")
185179

186180
def test_foreign_field_with_ranges(self):
187181
queryset = Library.objects.filter(books__author__age__range=(25, 35))
188-
189182
self.assertEqual(queryset.count(), 1)
190183
self.assertEqual(queryset.first().name, "Central Library")

0 commit comments

Comments
 (0)