'Ensure that bulk insert works'

def test_bulk_insert(self):

    class Comment(EmbeddedDocument, ):
        name = StringField()
    class Post(EmbeddedDocument, ):
        comments = ListField(EmbeddedDocumentField(Comment))
    class Blog(Document, ):
        title = StringField(unique=True)
        tags = ListField(StringField())
        posts = ListField(EmbeddedDocumentField(Post))
    Blog.drop_collection()
    connection = get_connection()
    info = connection.test.command('buildInfo')
    mongodb_version = tuple([int(i) for i in info['version'].split('.')])
    self.assertEqual(0, Blog.objects.count())
    with query_counter() as q:
        self.assertEqual(q, 0)
        comment1 = Comment(name='testa')
        comment2 = Comment(name='testb')
        post1 = Post(comments=[comment1, comment2])
        post2 = Post(comments=[comment2, comment2])
        blogs = []
        for i in range(1, 100):
            blogs.append(Blog(title=('post    %s' % i), posts=[post1, post2]))
        Blog.objects.insert(blogs, load_bulk=False)
        if (mongodb_version < (2, 6)):
            self.assertEqual(q, 1)
        else:
            self.assertEqual(q, 99)
    Blog.drop_collection()
    Blog.ensure_indexes()
    with query_counter() as q:
        self.assertEqual(q, 0)
        Blog.objects.insert(blogs)
        if (mongodb_version < (2, 6)):
            self.assertEqual(q, 2)
        else:
            self.assertEqual(q, 100)
    Blog.drop_collection()
    comment1 = Comment(name='testa')
    comment2 = Comment(name='testb')
    post1 = Post(comments=[comment1, comment2])
    post2 = Post(comments=[comment2, comment2])
    blog1 = Blog(title='code', posts=[post1, post2])
    blog2 = Blog(title='mongodb', posts=[post2, post1])
    (blog1, blog2) = Blog.objects.insert([blog1, blog2])
    self.assertEqual(blog1.title, 'code')
    self.assertEqual(blog2.title, 'mongodb')
    self.assertEqual(Blog.objects.count(), 2)
    with self.assertRaises(OperationError):
        blog = Blog.objects.first()
        Blog.objects.insert(blog)
    with self.assertRaises(OperationError):
        blogs = Blog.objects
        Blog.objects.insert(blogs)
    new_post = Blog(title='code123', id=ObjectId())
    Blog.objects.insert(new_post)
    class Author(Document, ):
        pass
    with self.assertRaises(OperationError):
        Blog.objects.insert(Author())
    with self.assertRaises(OperationError):
        Blog.objects.insert('HELLO    WORLD')
    Blog.drop_collection()
    blog1 = Blog(title='code', posts=[post1, post2])
    blog1 = Blog.objects.insert(blog1)
    self.assertEqual(blog1.title, 'code')
    self.assertEqual(Blog.objects.count(), 1)
    Blog.drop_collection()
    blog1 = Blog(title='code', posts=[post1, post2])
    obj_id = Blog.objects.insert(blog1, load_bulk=False)
    self.assertEqual(obj_id.__class__.__name__, 'ObjectId')
    Blog.drop_collection()
    post3 = Post(comments=[comment1, comment1])
    blog1 = Blog(title='foo', posts=[post1, post2])
    blog2 = Blog(title='bar', posts=[post2, post3])
    blog3 = Blog(title='baz', posts=[post1, post2])
    Blog.objects.insert([blog1, blog2])
    with self.assertRaises(NotUniqueError):
        Blog.objects.insert([blog2, blog3])
    self.assertEqual(Blog.objects.count(), 2)
    Blog.objects.insert([blog2, blog3], write_concern={'w': 0, 'continue_on_error': True})
    self.assertEqual(Blog.objects.count(), 3)
