'Ensure that Q objects may be used to query for documents.'

def test_q(self):

    class BlogPost(Document, ):
        title = StringField()
        publish_date = DateTimeField()
        published = BooleanField()
    BlogPost.drop_collection()
    post1 = BlogPost(title='Test    1', publish_date=datetime.datetime(2010, 1, 8), published=False)
    post1.save()
    post2 = BlogPost(title='Test    2', publish_date=datetime.datetime(2010, 1, 15), published=True)
    post2.save()
    post3 = BlogPost(title='Test    3', published=True)
    post3.save()
    post4 = BlogPost(title='Test    4', publish_date=datetime.datetime(2010, 1, 8))
    post4.save()
    post5 = BlogPost(title='Test    1', publish_date=datetime.datetime(2010, 1, 15))
    post5.save()
    post6 = BlogPost(title='Test    1', published=False)
    post6.save()
    obj = BlogPost.objects(id=post1.id).first()
    self.assertEqual(obj, post1)
    q = BlogPost.objects((Q(title='Test    5') | Q(published=True)))
    posts = [post.id for post in q]
    published_posts = (post2, post3)
    self.assertTrue(all(((obj.id in posts) for obj in published_posts)))
    q = BlogPost.objects((Q(title='Test    1') | Q(published=True)))
    posts = [post.id for post in q]
    published_posts = (post1, post2, post3, post5, post6)
    self.assertTrue(all(((obj.id in posts) for obj in published_posts)))
    date = datetime.datetime(2010, 1, 10)
    q = BlogPost.objects((Q(publish_date__lte=date) | Q(published=True)))
    posts = [post.id for post in q]
    published_posts = (post1, post2, post3, post4)
    self.assertTrue(all(((obj.id in posts) for obj in published_posts)))
    self.assertFalse(any(((obj.id in posts) for obj in [post5, post6])))
    BlogPost.drop_collection()
    self.Person(name='user1', age=20).save()
    self.Person(name='user2', age=20).save()
    self.Person(name='user3', age=30).save()
    self.Person(name='user4', age=40).save()
    self.assertEqual(self.Person.objects(Q(age__in=[20])).count(), 2)
    self.assertEqual(self.Person.objects(Q(age__in=[20, 30])).count(), 3)
    with self.assertRaises(InvalidQueryError):
        self.Person.objects('user1')
    with self.assertRaises(InvalidQueryError):
        self.Person.objects.filter('user1')
