Fixed #14246: Modified aggregation_regress tests so that they will pass on a variety (sqlite, Postgres, MySQL/MyISAM) of DBs.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13712 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2010-09-10 17:57:42 +00:00
parent 003ed01bb3
commit 037b579b5e
1 changed files with 10 additions and 4 deletions

View File

@ -135,7 +135,6 @@ class AggregationTests(TestCase):
contact_id=3, contact_id=3,
id=2, id=2,
isbn=u'067232959', isbn=u'067232959',
manufacture_cost=11.545,
mean_auth_age=45.0, mean_auth_age=45.0,
name='Sams Teach Yourself Django in 24 Hours', name='Sams Teach Yourself Django in 24 Hours',
pages=528, pages=528,
@ -144,6 +143,8 @@ class AggregationTests(TestCase):
publisher_id=2, publisher_id=2,
rating=3.0 rating=3.0
) )
# Different DB backends return different types for the extra select computation
self.assertTrue(obj.manufacture_cost == 11.545 or obj.manufacture_cost == Decimal('11.545'))
# Order of the annotate/extra in the query doesn't matter # Order of the annotate/extra in the query doesn't matter
obj = Book.objects.extra(select={'manufacture_cost' : 'price * .5'}).annotate(mean_auth_age=Avg('authors__age')).get(pk=2) obj = Book.objects.extra(select={'manufacture_cost' : 'price * .5'}).annotate(mean_auth_age=Avg('authors__age')).get(pk=2)
@ -151,7 +152,6 @@ class AggregationTests(TestCase):
contact_id=3, contact_id=3,
id=2, id=2,
isbn=u'067232959', isbn=u'067232959',
manufacture_cost=11.545,
mean_auth_age=45.0, mean_auth_age=45.0,
name=u'Sams Teach Yourself Django in 24 Hours', name=u'Sams Teach Yourself Django in 24 Hours',
pages=528, pages=528,
@ -160,14 +160,18 @@ class AggregationTests(TestCase):
publisher_id=2, publisher_id=2,
rating=3.0 rating=3.0
) )
# Different DB backends return different types for the extra select computation
self.assertTrue(obj.manufacture_cost == 11.545 or obj.manufacture_cost == Decimal('11.545'))
# Values queries can be combined with annotate and extra # Values queries can be combined with annotate and extra
obj = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).values().get(pk=2) obj = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).values().get(pk=2)
manufacture_cost = obj['manufacture_cost']
self.assertTrue(manufacture_cost == 11.545 or manufacture_cost == Decimal('11.545'))
del obj['manufacture_cost']
self.assertEqual(obj, { self.assertEqual(obj, {
"contact_id": 3, "contact_id": 3,
"id": 2, "id": 2,
"isbn": u"067232959", "isbn": u"067232959",
"manufacture_cost": 11.545,
"mean_auth_age": 45.0, "mean_auth_age": 45.0,
"name": u"Sams Teach Yourself Django in 24 Hours", "name": u"Sams Teach Yourself Django in 24 Hours",
"pages": 528, "pages": 528,
@ -180,11 +184,13 @@ class AggregationTests(TestCase):
# The order of the (empty) values, annotate and extra clauses doesn't # The order of the (empty) values, annotate and extra clauses doesn't
# matter # matter
obj = Book.objects.values().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2) obj = Book.objects.values().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2)
manufacture_cost = obj['manufacture_cost']
self.assertTrue(manufacture_cost == 11.545 or manufacture_cost == Decimal('11.545'))
del obj['manufacture_cost']
self.assertEqual(obj, { self.assertEqual(obj, {
'contact_id': 3, 'contact_id': 3,
'id': 2, 'id': 2,
'isbn': u'067232959', 'isbn': u'067232959',
'manufacture_cost': 11.545,
'mean_auth_age': 45.0, 'mean_auth_age': 45.0,
'name': u'Sams Teach Yourself Django in 24 Hours', 'name': u'Sams Teach Yourself Django in 24 Hours',
'pages': 528, 'pages': 528,