Skip to content

Commit 0aa6e73

Browse files
WaVEVtimgraham
authored andcommitted
add subquery tests for EmbeddedModelField
1 parent 37af20e commit 0aa6e73

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-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: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
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+
Sum,
12+
)
613
from django.test import SimpleTestCase, TestCase
714
from django.test.utils import isolate_apps
815

@@ -15,6 +22,7 @@
1522
Book,
1623
Data,
1724
Holder,
25+
Library,
1826
)
1927
from .utils import truncate_ms
2028

@@ -214,3 +222,45 @@ class MyModel(models.Model):
214222
msg,
215223
"Embedded models must be a subclass of django_mongodb_backend.models.EmbeddedModel.",
216224
)
225+
226+
227+
class SubqueryExistsTests(TestCase):
228+
def setUpTestData(self):
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)
238+
library1 = Library.objects.create(
239+
name="Central Library", location="Downtown", best_seller="Book 1"
240+
)
241+
library2 = Library.objects.create(
242+
name="Community Library", location="Suburbs", best_seller="Book 1"
243+
)
244+
library1.books.add(book1, book2)
245+
library2.books.add(book2)
246+
247+
def test_exists_subquery(self):
248+
subquery = Book.objects.filter(
249+
author__name=OuterRef("author__name"), author__address__city="Boston"
250+
)
251+
qs = Book.objects.filter(Exists(subquery)).order_by("name")
252+
self.assertQuerySetEqual(qs, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
253+
254+
def test_in_subquery(self):
255+
names = Book.objects.filter(author__age__gt=35).values("author__name")
256+
qs = Book.objects.filter(author__name__in=names).order_by("name")
257+
self.assertQuerySetEqual(qs, ["Book 2", "Book 3", "Book 4"], lambda book: book.name)
258+
259+
def test_exists_with_foreign_object(self):
260+
subquery = Library.objects.filter(best_seller=OuterRef("name"))
261+
qs = Book.objects.filter(Exists(subquery))
262+
self.assertEqual(qs.first().name, "Book 1")
263+
264+
def test_foreign_field_with_range(self):
265+
qs = Library.objects.filter(books__author__age__range=(25, 35))
266+
self.assertEqual(qs.first().name, "Central Library")

0 commit comments

Comments
 (0)