'Ensure that query by array position works.'

def test_find_array_position(self):

    class Comment(EmbeddedDocument, ):
        name = StringField()
    class Post(EmbeddedDocument, ):
        comments = ListField(EmbeddedDocumentField(Comment))
    class Blog(Document, ):
        tags = ListField(StringField())
        posts = ListField(EmbeddedDocumentField(Post))
    Blog.drop_collection()
    Blog.objects.create(tags=['a', 'b'])
    self.assertEqual(Blog.objects(tags__0='a').count(), 1)
    self.assertEqual(Blog.objects(tags__0='b').count(), 0)
    self.assertEqual(Blog.objects(tags__1='a').count(), 0)
    self.assertEqual(Blog.objects(tags__1='b').count(), 1)
    Blog.drop_collection()
    comment1 = Comment(name='testa')
    comment2 = Comment(name='testb')
    post1 = Post(comments=[comment1, comment2])
    post2 = Post(comments=[comment2, comment2])
    blog1 = Blog.objects.create(posts=[post1, post2])
    blog2 = Blog.objects.create(posts=[post2, post1])
    blog = Blog.objects(posts__0__comments__0__name='testa').get()
    self.assertEqual(blog, blog1)
    blog = Blog.objects(posts__0__comments__0__name='testb').get()
    self.assertEqual(blog, blog2)
    query = Blog.objects(posts__1__comments__1__name='testb')
    self.assertEqual(query.count(), 2)
    query = Blog.objects(posts__1__comments__1__name='testa')
    self.assertEqual(query.count(), 0)
    query = Blog.objects(posts__0__comments__1__name='testa')
    self.assertEqual(query.count(), 0)
    Blog.drop_collection()
