Raise TypeError when __in-operator used with a Document (#1237)
This commit is contained in:
committed by
Stefan Wójcik
parent
65914fb2b2
commit
76524b7498
@@ -4963,6 +4963,56 @@ class QuerySetTest(unittest.TestCase):
|
||||
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()
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user