Skip to content

Commit 92afc21

Browse files
timgrahamWaVEV
authored andcommitted
add subqueries tests
1 parent 5156577 commit 92afc21

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

tests/model_fields_/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@ class Author(EmbeddedModel):
120120
class Book(models.Model):
121121
name = models.CharField(max_length=100)
122122
author = EmbeddedModelField(Author)
123+
124+
125+
class Library(models.Model):
126+
name = models.CharField(max_length=100)
127+
books = models.ManyToManyField("Book", related_name="libraries")
128+
location = models.CharField(max_length=100, null=True, blank=True)
129+
best_seller = models.CharField(max_length=100, null=True, blank=True)

tests/model_fields_/test_embedded_model.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
from django.core.exceptions import ValidationError
44
from django.db import models
5-
from django.db.models import ExpressionWrapper, F, Max, Sum
5+
from django.db.models import (
6+
Exists,
7+
ExpressionWrapper,
8+
F,
9+
Max,
10+
OuterRef,
11+
Subquery,
12+
Sum,
13+
)
614
from django.test import SimpleTestCase, TestCase
715
from django.test.utils import isolate_apps
816

@@ -15,6 +23,7 @@
1523
Book,
1624
Data,
1725
Holder,
26+
Library,
1827
)
1928
from .utils import truncate_ms
2029

@@ -179,3 +188,61 @@ class MyModel(models.Model):
179188
msg,
180189
"Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
181190
)
191+
192+
class SubqueryExistsTest(TestCase):
193+
def setUp(self):
194+
# 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+
205+
library1 = Library.objects.create(
206+
name="Central Library", location="Downtown", best_seller="Book A"
207+
)
208+
library2 = Library.objects.create(
209+
name="Community Library", location="Suburbs", best_seller="Book A"
210+
)
211+
212+
# Add books to libraries
213+
library1.books.add(book1, book2)
214+
library2.books.add(book2)
215+
216+
def test_exists_subquery(self):
217+
subquery = Book.objects.filter(
218+
author__name=OuterRef("name"), author__address__city="Boston"
219+
)
220+
queryset = Author.objects.filter(Exists(subquery))
221+
222+
self.assertEqual(queryset.count(), 1)
223+
224+
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+
228+
self.assertEqual(queryset.count(), 3)
229+
self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name)
230+
231+
def test_range_query(self):
232+
queryset = Author.objects.filter(age__range=(25, 45)).order_by("name")
233+
234+
self.assertEqual(queryset.count(), 2)
235+
self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name)
236+
237+
def test_exists_with_foreign_object(self):
238+
subquery = Library.objects.filter(best_seller=OuterRef("name"))
239+
queryset = Book.objects.filter(Exists(subquery))
240+
241+
self.assertEqual(queryset.count(), 1)
242+
self.assertEqual(queryset.first().name, "Book A")
243+
244+
def test_foreign_field_with_ranges(self):
245+
queryset = Library.objects.filter(books__author__age__range=(25, 35))
246+
247+
self.assertEqual(queryset.count(), 1)
248+
self.assertEqual(queryset.first().name, "Central Library")

0 commit comments

Comments
 (0)