-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Querysets are now lest restrictive when querying duplicate fields (#332…
…, #333)
- Loading branch information
Showing
3 changed files
with
28 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,9 +68,11 @@ class TestDoc(Document): | |
x = IntField() | ||
y = StringField() | ||
|
||
# Check than an error is raised when conflicting queries are anded | ||
query = (Q(x__lt=7) & Q(x__lt=3)).to_query(TestDoc) | ||
self.assertEqual(query, {'$and': [ {'x': {'$lt': 7}}, {'x': {'$lt': 3}} ]}) | ||
self.assertEqual(query, {'$and': [{'x': {'$lt': 7}}, {'x': {'$lt': 3}}]}) | ||
|
||
query = (Q(y="a") & Q(x__lt=7) & Q(x__lt=3)).to_query(TestDoc) | ||
self.assertEqual(query, {'$and': [{'y': "a"}, {'x': {'$lt': 7}}, {'x': {'$lt': 3}}]}) | ||
|
||
# Check normal cases work without an error | ||
query = Q(x__lt=7) & Q(x__gt=3) | ||
|
@@ -323,10 +325,26 @@ class User(Document): | |
pk = ObjectId() | ||
User(email='[email protected]', pk=pk).save() | ||
|
||
self.assertEqual(1, User.objects.filter( | ||
Q(email='[email protected]') | | ||
Q(name='John Doe') | ||
).limit(2).filter(pk=pk).count()) | ||
self.assertEqual(1, User.objects.filter(Q(email='[email protected]') | | ||
Q(name='John Doe')).limit(2).filter(pk=pk).count()) | ||
|
||
def test_chained_q_or_filtering(self): | ||
|
||
class Post(EmbeddedDocument): | ||
name = StringField(required=True) | ||
|
||
class Item(Document): | ||
postables = ListField(EmbeddedDocumentField(Post)) | ||
|
||
Item.drop_collection() | ||
|
||
Item(postables=[Post(name="a"), Post(name="b")]).save() | ||
Item(postables=[Post(name="a"), Post(name="c")]).save() | ||
Item(postables=[Post(name="a"), Post(name="b"), Post(name="c")]).save() | ||
|
||
self.assertEqual(Item.objects(Q(postables__name="a") & Q(postables__name="b")).count(), 2) | ||
self.assertEqual(Item.objects.filter(postables__name="a").filter(postables__name="b").count(), 2) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
ee72535
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.
👍