Code

Ticket #12818: 12818.diff

File 12818.diff, 4.2 KB (added by SmileyChris, 4 years ago)
Line 
1diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
2index a9b1aa3..8ddb736 100644
3--- a/django/db/backends/sqlite3/base.py
4+++ b/django/db/backends/sqlite3/base.py
5@@ -63,13 +63,17 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6 class DatabaseOperations(BaseDatabaseOperations):
7     def date_extract_sql(self, lookup_type, field_name):
8         # sqlite doesn't support extract, so we fake it with the user-defined
9-        # function django_extract that's registered in connect().
10-        return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name)
11+        # function django_extract that's registered in connect(). Note that
12+        # single quotes are used because this is a string (and could otherwise
13+        # cause a collision with a field name).
14+        return "django_extract('%s', %s)" % (lookup_type.lower(), field_name)
15 
16     def date_trunc_sql(self, lookup_type, field_name):
17         # sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined
18-        # function django_date_trunc that's registered in connect().
19-        return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
20+        # function django_date_trunc that's registered in connect(). Note that
21+        # single quotes are used because this is a string (and could otherwise
22+        # cause a collision with a field name).
23+        return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
24 
25     def drop_foreignkey_sql(self):
26         return ""
27diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
28index 61b7b1a..423bead 100644
29--- a/tests/regressiontests/backends/models.py
30+++ b/tests/regressiontests/backends/models.py
31@@ -15,6 +15,11 @@ class Person(models.Model):
32     def __unicode__(self):
33         return u'%s %s' % (self.first_name, self.last_name)
34 
35+class SchoolClass(models.Model):
36+    year = models.PositiveIntegerField()
37+    day = models.CharField(max_length=9, blank=True)
38+    last_updated = models.DateTimeField()
39+
40 qn = connection.ops.quote_name
41 
42 __test__ = {'API_TESTS': """
43diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
44index e4fa824..7b1fcb9 100644
45--- a/tests/regressiontests/backends/tests.py
46+++ b/tests/regressiontests/backends/tests.py
47@@ -1,9 +1,12 @@
48 # -*- coding: utf-8 -*-
49 # Unit and doctests for specific database backends.
50+import datetime
51+import models
52 import unittest
53 from django.db import backend, connection, DEFAULT_DB_ALIAS
54 from django.db.backends.signals import connection_created
55 from django.conf import settings
56+from django.test import TestCase
57 
58 class Callproc(unittest.TestCase):
59 
60@@ -33,6 +36,39 @@ class LongString(unittest.TestCase):
61             row = c.fetchone()
62             c.execute('DROP TABLE ltext')
63             self.assertEquals(long_str, row[0].read())
64+           
65+class SqliteTest(TestCase):
66+
67+    def test_django_date_trunc(self):
68+        """
69+        Test the custom ``django_date_trunc method``, in particular against
70+        fields which clash with strings passed to it (e.g. 'year') - see
71+        #12818__.
72+
73+        __: http://code.djangoproject.com/ticket/12818
74+
75+        """
76+        if settings.DATABASE_ENGINE != 'sqlite3':
77+            return
78+        updated = datetime.datetime(2010, 2, 20)
79+        models.SchoolClass.objects.create(year=2009, last_updated=updated)
80+        years = models.SchoolClass.objects.dates('last_updated', 'year')
81+        self.assertEqual(list(years), [datetime.datetime(2010, 1, 1, 0, 0)])   
82+
83+    def test_django_extract(self):
84+        """
85+        Test the custom ``django_extract method``, in particular against fields
86+        which clash with strings passed to it (e.g. 'day') - see #12818__.
87+
88+        __: http://code.djangoproject.com/ticket/12818
89+
90+        """
91+        if settings.DATABASE_ENGINE != 'sqlite3':
92+            return
93+        updated = datetime.datetime(2010, 2, 20)
94+        models.SchoolClass.objects.create(year=2009, last_updated=updated)
95+        classes = models.SchoolClass.objects.filter(last_updated__day=20)
96+        self.assertEqual(len(classes), 1)
97 
98 def connection_created_test(sender, **kwargs):
99     print 'connection_created signal'