| | 1 | from datetime import date |
| | 2 | from django.conf import settings |
| | 3 | from django.db import connection |
| | 4 | from django.db.models import Count |
| | 5 | from django.test import TestCase |
| | 6 | from models import TestModel |
| | 7 | |
| | 8 | |
| | 9 | class TestBug11975(TestCase): |
| | 10 | |
| | 11 | def setUp(self): |
| | 12 | settings.DEBUG = True |
| | 13 | |
| | 14 | def testResult(self): |
| | 15 | t = TestModel() |
| | 16 | t.date = date.today() |
| | 17 | t.save() |
| | 18 | t = TestModel() |
| | 19 | t.date = date(2010, 1, 20) |
| | 20 | t.save() |
| | 21 | result = TestModel.objects.extra( |
| | 22 | select={'year':connection.ops.date_trunc_sql('year', "bug11975_testmodel.date")} |
| | 23 | ).values('year').order_by().annotate(total=Count('pk')) |
| | 24 | expected = [{'total': 2, 'year': u'2010-01-01 00:00:00'}] |
| | 25 | self.assertNotEqual(expected, result) |
| | 26 | failsql = u'SELECT (django_date_trunc("year", "bug11975_testmodel"."date")) AS "year", COUNT("bug11975_testmodel"."id") AS "total" FROM "bug11975_testmodel" GROUP BY django_date_trunc("year", "bug11975_testmodel"."date")' |
| | 27 | self.assertNotEquals(failsql, result.query.__str__()) |