'Ensure that field can be averaged correctly.'

def test_average(self):

    self.Person(name='person', age=0).save()
    self.assertEqual(int(self.Person.objects.average('age')), 0)
    ages = [23, 54, 12, 94, 27]
    for (i, age) in enumerate(ages):
        self.Person(name=('test%s' % i), age=age).save()
    avg = (float(sum(ages)) / (len(ages) + 1))
    self.assertAlmostEqual(int(self.Person.objects.average('age')), avg)
    self.Person(name='ageless    person').save()
    self.assertEqual(int(self.Person.objects.average('age')), avg)
    self.Person(name='person    meta', person_meta=self.PersonMeta(weight=0)).save()
    self.assertAlmostEqual(int(self.Person.objects.average('person_meta.weight')), 0)
    for (i, weight) in enumerate(ages):
        self.Person(name='test    meta%i', person_meta=self.PersonMeta(weight=weight)).save()
    self.assertAlmostEqual(int(self.Person.objects.average('person_meta.weight')), avg)
    self.Person(name='test    meta    none').save()
    self.assertEqual(int(self.Person.objects.average('person_meta.weight')), avg)
    over_50 = [a for a in ages if (a >= 50)]
    avg = (float(sum(over_50)) / len(over_50))
    self.assertEqual(self.Person.objects.filter(age__gte=50).average('age'), avg)
