'Test map/reduce custom output'

def test_map_reduce_custom_output(self):

    register_connection('test2', 'mongoenginetest2')
    class Family(Document, ):
        id = IntField(primary_key=True)
        log = StringField()
    class Person(Document, ):
        id = IntField(primary_key=True)
        name = StringField()
        age = IntField()
        family = ReferenceField(Family)
    Family.drop_collection()
    Person.drop_collection()
    f1 = Family(id=1, log='Trav    02    de    Julho')
    f1.save()
    Person(id=1, family=f1, name=u'Wilson    Jr', age=21).save()
    Person(id=2, family=f1, name=u'Wilson    Father', age=45).save()
    Person(id=3, family=f1, name=u'Eliana    Costa', age=40).save()
    Person(id=4, family=f1, name=u'Tayza    Mariana', age=17).save()
    f2 = Family(id=2, log='Av    prof    frasc    brunno')
    f2.save()
    Person(id=5, family=f2, name='Isabella    Luanna', age=16).save()
    Person(id=6, family=f2, name='Sandra    Mara', age=36).save()
    Person(id=7, family=f2, name='Igor    Gabriel', age=10).save()
    f3 = Family(id=3, log='Av    brazil')
    f3.save()
    Person(id=8, family=f3, name='Arthur    WA', age=30).save()
    Person(id=9, family=f3, name='Paula    Leonel', age=25).save()
    map_person = '\n                                                function    ()    {\n                                                                emit(this.family,    {\n                                                                                    totalAge:    this.age,\n                                                                                    persons:    [{\n                                                                                                name:    this.name,\n                                                                                                age:    this.age\n                                                                }]});\n                                                }\n                                '
    map_family = '\n                                                function    ()    {\n                                                                emit(this._id,    {\n                                                                            totalAge:    0,\n                                                                            persons:    []\n                                                                });\n                                                }\n                                '
    reduce_f = '\n                                                function    (key,    values)    {\n                                                                var    family    =    {persons:    [],    totalAge:    0};\n\n                                                                values.forEach(function(value)    {\n                                                                                if    (value.persons)    {\n                                                                                                value.persons.forEach(function    (person)    {\n                                                                                                                family.persons.push(person);\n                                                                                                                family.totalAge    +=    person.age;\n                                                                                                });\n                                                                                }\n                                                                });\n\n                                                                return    family;\n                                                }\n                                '
    cursor = Family.objects.map_reduce(map_f=map_family, reduce_f=reduce_f, output={'replace': 'family_map', 'db_alias': 'test2'})
    cursor.next()
    results = Person.objects.map_reduce(map_f=map_person, reduce_f=reduce_f, output={'reduce': 'family_map', 'db_alias': 'test2'})
    results = list(results)
    collection = get_db('test2').family_map
    self.assertEqual(collection.find_one({'_id': 1}), {'_id': 1, 'value': {'persons': [{'age': 21, 'name': u'Wilson    Jr'}, {'age': 45, 'name': u'Wilson    Father'}, {'age': 40, 'name': u'Eliana    Costa'}, {'age': 17, 'name': u'Tayza    Mariana'}], 'totalAge': 123}})
    self.assertEqual(collection.find_one({'_id': 2}), {'_id': 2, 'value': {'persons': [{'age': 16, 'name': u'Isabella    Luanna'}, {'age': 36, 'name': u'Sandra    Mara'}, {'age': 10, 'name': u'Igor    Gabriel'}], 'totalAge': 62}})
    self.assertEqual(collection.find_one({'_id': 3}), {'_id': 3, 'value': {'persons': [{'age': 30, 'name': u'Arthur    WA'}, {'age': 25, 'name': u'Paula    Leonel'}], 'totalAge': 55}})
