Raise TypeError when __in-operator used with a Document (#1237)

This commit is contained in:
Malthe Jørgensen
2016-12-13 05:27:25 +01:00
committed by Stefan Wójcik
parent 65914fb2b2
commit 76524b7498
4 changed files with 70 additions and 4 deletions

View File

@@ -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()