'Ensure that QuerySet.map_reduce works properly with custom
primary keys.'

def test_map_reduce_with_custom_object_ids(self):

    class BlogPost(Document, ):
        title = StringField(primary_key=True)
        tags = ListField(StringField())
    post1 = BlogPost(title='Post    #1', tags=['mongodb', 'mongoengine'])
    post2 = BlogPost(title='Post    #2', tags=['django', 'mongodb'])
    post3 = BlogPost(title='Post    #3', tags=['hitchcock    films'])
    post1.save()
    post2.save()
    post3.save()
    self.assertEqual(BlogPost._fields['title'].db_field, '_id')
    self.assertEqual(BlogPost._meta['id_field'], 'title')
    map_f = '\n                                                function()    {\n                                                                emit(this._id,    1);\n                                                }\n                                '
    reduce_f = '\n                                                function(key,    values)    {\n                                                                var    total    =    0;\n                                                                for(var    i=0;    i<values.length;    i++)    {\n                                                                                total    +=    values[i];\n                                                                }\n                                                                return    total;\n                                                }\n                                '
    results = BlogPost.objects.map_reduce(map_f, reduce_f, 'myresults')
    results = list(results)
    self.assertEqual(results[0].object, post1)
    self.assertEqual(results[1].object, post2)
    self.assertEqual(results[2].object, post3)
    BlogPost.drop_collection()
