Skip to content

Commit a6f2f2f

Browse files
timgrahamWaVEV
authored andcommitted
add subqueries tests
1 parent 37af20e commit a6f2f2f

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 FieldDoesNotExist, 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

@@ -214,3 +223,61 @@ class MyModel(models.Model):
214223
msg,
215224
"Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
216225
)
226+
227+
class SubqueryExistsTest(TestCase):
228+
def setUp(self):
229+
# 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+
240+
library1 = Library.objects.create(
241+
name="Central Library", location="Downtown", best_seller="Book A"
242+
)
243+
library2 = Library.objects.create(
244+
name="Community Library", location="Suburbs", best_seller="Book A"
245+
)
246+
247+
# Add books to libraries
248+
library1.books.add(book1, book2)
249+
library2.books.add(book2)
250+
251+
def test_exists_subquery(self):
252+
subquery = Book.objects.filter(
253+
author__name=OuterRef("name"), author__address__city="Boston"
254+
)
255+
queryset = Author.objects.filter(Exists(subquery))
256+
257+
self.assertEqual(queryset.count(), 1)
258+
259+
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+
263+
self.assertEqual(queryset.count(), 3)
264+
self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name)
265+
266+
def test_range_query(self):
267+
queryset = Author.objects.filter(age__range=(25, 45)).order_by("name")
268+
269+
self.assertEqual(queryset.count(), 2)
270+
self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name)
271+
272+
def test_exists_with_foreign_object(self):
273+
subquery = Library.objects.filter(best_seller=OuterRef("name"))
274+
queryset = Book.objects.filter(Exists(subquery))
275+
276+
self.assertEqual(queryset.count(), 1)
277+
self.assertEqual(queryset.first().name, "Book A")
278+
279+
def test_foreign_field_with_ranges(self):
280+
queryset = Library.objects.filter(books__author__age__range=(25, 35))
281+
282+
self.assertEqual(queryset.count(), 1)
283+
self.assertEqual(queryset.first().name, "Central Library")

0 commit comments

Comments
 (0)