-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EMF add subqueries tests #226
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from django.db.models import ( | ||
Exists, | ||
OuterRef, | ||
Subquery, | ||
) | ||
from django.test import SimpleTestCase, TestCase | ||
from django.test.utils import isolate_apps | ||
|
||
|
@@ -11,6 +16,7 @@ | |
Book, | ||
Data, | ||
Holder, | ||
Library, | ||
) | ||
|
||
|
||
|
@@ -123,3 +129,62 @@ class MyModel(models.Model): | |
self.assertEqual( | ||
msg, "Embedded models cannot have relational fields (Target.key is a ForeignKey)." | ||
) | ||
|
||
|
||
class SubqueryExistsTest(TestCase): | ||
def setUp(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use setUpTestData? |
||
# Create test data | ||
address1 = Address.objects.create(city="New York", state="NY", zip_code=10001) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Embedded models should be saved to the database in their own collection (i.e. no |
||
address2 = Address.objects.create(city="Boston", state="MA", zip_code=20002) | ||
author1 = Author.objects.create(name="Alice", age=30, address=address1) | ||
author2 = Author.objects.create(name="Bob", age=40, address=address2) | ||
book1 = Book.objects.create(name="Book A", author=author1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to title it "Book 1" rather than switching between letters and numbers. |
||
book2 = Book.objects.create(name="Book B", author=author2) | ||
Book.objects.create(name="Book C", author=author2) | ||
Book.objects.create(name="Book D", author=author2) | ||
Book.objects.create(name="Book E", author=author1) | ||
|
||
library1 = Library.objects.create( | ||
name="Central Library", location="Downtown", best_seller="Book A" | ||
) | ||
library2 = Library.objects.create( | ||
name="Community Library", location="Suburbs", best_seller="Book A" | ||
) | ||
|
||
# Add books to libraries | ||
library1.books.add(book1, book2) | ||
library2.books.add(book2) | ||
|
||
def test_exists_subquery(self): | ||
subquery = Book.objects.filter( | ||
author__name=OuterRef("name"), author__address__city="Boston" | ||
) | ||
queryset = Author.objects.filter(Exists(subquery)) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please omit the blank lines. |
||
self.assertEqual(queryset.count(), 1) | ||
|
||
def test_in_subquery(self): | ||
subquery = Author.objects.filter(age__gt=35).values("name") | ||
queryset = Book.objects.filter(author__name__in=Subquery(subquery)).order_by("name") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I tested, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmh yes, don't remember why i added that |
||
|
||
self.assertEqual(queryset.count(), 3) | ||
self.assertQuerySetEqual(queryset, ["Book B", "Book C", "Book D"], lambda book: book.name) | ||
|
||
def test_range_query(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why in this class? Is it because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test could be put in a better place. Will move |
||
queryset = Author.objects.filter(age__range=(25, 45)).order_by("name") | ||
|
||
self.assertEqual(queryset.count(), 2) | ||
self.assertQuerySetEqual(queryset, ["Alice", "Bob"], lambda author: author.name) | ||
|
||
def test_exists_with_foreign_object(self): | ||
subquery = Library.objects.filter(best_seller=OuterRef("name")) | ||
queryset = Book.objects.filter(Exists(subquery)) | ||
|
||
self.assertEqual(queryset.count(), 1) | ||
self.assertEqual(queryset.first().name, "Book A") | ||
|
||
def test_foreign_field_with_ranges(self): | ||
queryset = Library.objects.filter(books__author__age__range=(25, 35)) | ||
|
||
self.assertEqual(queryset.count(), 1) | ||
self.assertEqual(queryset.first().name, "Central Library") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests