-
Couldn't load subscription status.
- Fork 1.2k
Raise TypeError when __in-operator used with a Document
#1237
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
Merged
wojcikstefan
merged 5 commits into
MongoEngine:master
from
malthejorgensen:feature/in-operator-catch-no-list
Dec 13, 2016
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4bcd9b
[Dev] _import_class: Add support for BaseDocument
malthejorgensen 0a5ef2e
Raise TypeError when __in-query used with Document
malthejorgensen 4c00f0b
queryset/transform.py: flake8 compliance
malthejorgensen b5eb453
tests/queryset/queryset.py: Python 2.6 compat
malthejorgensen de8d65d
[Dev] flake8: Raise max_complexity to 47
malthejorgensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4963,6 +4963,56 @@ class Data(Document): | |
| self.assertEqual(i, 249) | ||
| self.assertEqual(j, 249) | ||
|
|
||
| def test_in_operator_on_non_iterable(self): | ||
| """Ensure that using the `__in` operator on a non-iterable raises an | ||
| error. | ||
| """ | ||
| class User(Document): | ||
| name = StringField() | ||
|
|
||
| class BlogPost(Document): | ||
| content = StringField() | ||
| authors = ListField(ReferenceField(User)) | ||
|
|
||
| User.drop_collection() | ||
| BlogPost.drop_collection() | ||
|
|
||
| author = User(name='Test User') | ||
| author.save() | ||
| post = BlogPost(content='Had a good coffee today...', authors=[author]) | ||
| post.save() | ||
|
|
||
| blog_posts = BlogPost.objects(authors__in=[author]) | ||
| self.assertEqual(list(blog_posts), [post]) | ||
|
|
||
| # Using the `__in`-operator with a non-iterable should raise a TypeError | ||
| self.assertRaises(TypeError, BlogPost.objects(authors__in=author.id).count) | ||
|
|
||
| def test_in_operator_on_document(self): | ||
| """Ensure that using the `__in` operator on a `Document` raises an | ||
| error. | ||
| """ | ||
| class User(Document): | ||
| name = StringField() | ||
|
|
||
| class BlogPost(Document): | ||
| content = StringField() | ||
| authors = ListField(ReferenceField(User)) | ||
|
|
||
| User.drop_collection() | ||
| BlogPost.drop_collection() | ||
|
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. These are redundant - by convention we should just take care of dropping existing collections at the beginning of each test (which you already do). |
||
|
|
||
| author = User(name='Test User') | ||
| author.save() | ||
| post = BlogPost(content='Had a good coffee today...', authors=[author]) | ||
| post.save() | ||
|
|
||
| blog_posts = BlogPost.objects(authors__in=[author]) | ||
| self.assertEqual(list(blog_posts), [post]) | ||
|
|
||
| # Using the `__in`-operator with a `Document` should raise a TypeError | ||
| self.assertRaises(TypeError, BlogPost.objects(authors__in=author).count) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If all of the above truly expect an iterable, then we should raise an error for any non-iterable, not just a
BaseDocument.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.
Well, the problem is actually that
BaseDocumentis iterable.Uh oh!
There was an error while loading. Please reload this page.
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.
Both the checks mentioned in this StackOverflow thread will say that BaseDocument is iterable. (
for k in userwill iterate over each field of the documentuser)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.
Ahh, I missed that, thanks for clearing that up! I think in such case we can change this condition to:
The comment is crucial, too, given that this is a pretty unintuitive
isinstancecheck.What do you think @malthejorgensen ?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.
I agree, I'll make the change