Ticket #12818: 12818.2.diff

File 12818.2.diff, 4.0 KB (added by Chris Beaven, 15 years ago)
  • django/db/backends/sqlite3/base.py

    diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
    index a9b1aa3..8ddb736 100644
    a b class DatabaseFeatures(BaseDatabaseFeatures):  
    6363class DatabaseOperations(BaseDatabaseOperations):
    6464    def date_extract_sql(self, lookup_type, field_name):
    6565        # sqlite doesn't support extract, so we fake it with the user-defined
    66         # function django_extract that's registered in connect().
    67         return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name)
     66        # function django_extract that's registered in connect(). Note that
     67        # single quotes are used because this is a string (and could otherwise
     68        # cause a collision with a field name).
     69        return "django_extract('%s', %s)" % (lookup_type.lower(), field_name)
    6870
    6971    def date_trunc_sql(self, lookup_type, field_name):
    7072        # sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined
    71         # function django_date_trunc that's registered in connect().
    72         return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
     73        # function django_date_trunc that's registered in connect(). Note that
     74        # single quotes are used because this is a string (and could otherwise
     75        # cause a collision with a field name).
     76        return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
    7377
    7478    def drop_foreignkey_sql(self):
    7579        return ""
  • tests/regressiontests/backends/models.py

    diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
    index 61b7b1a..423bead 100644
    a b class Person(models.Model):  
    1515    def __unicode__(self):
    1616        return u'%s %s' % (self.first_name, self.last_name)
    1717
     18class SchoolClass(models.Model):
     19    year = models.PositiveIntegerField()
     20    day = models.CharField(max_length=9, blank=True)
     21    last_updated = models.DateTimeField()
     22
    1823qn = connection.ops.quote_name
    1924
    2025__test__ = {'API_TESTS': """
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index e4fa824..7c5856e 100644
    a b  
    11# -*- coding: utf-8 -*-
    22# Unit and doctests for specific database backends.
     3import datetime
     4import models
    35import unittest
    46from django.db import backend, connection, DEFAULT_DB_ALIAS
    57from django.db.backends.signals import connection_created
    68from django.conf import settings
     9from django.test import TestCase
    710
    811class Callproc(unittest.TestCase):
    912
    class LongString(unittest.TestCase):  
    3336            row = c.fetchone()
    3437            c.execute('DROP TABLE ltext')
    3538            self.assertEquals(long_str, row[0].read())
     39           
     40class DateQuotingTest(TestCase):
     41
     42    def test_django_date_trunc(self):
     43        """
     44        Test the custom ``django_date_trunc method``, in particular against
     45        fields which clash with strings passed to it (e.g. 'year') - see
     46        #12818__.
     47
     48        __: http://code.djangoproject.com/ticket/12818
     49
     50        """
     51        updated = datetime.datetime(2010, 2, 20)
     52        models.SchoolClass.objects.create(year=2009, last_updated=updated)
     53        years = models.SchoolClass.objects.dates('last_updated', 'year')
     54        self.assertEqual(list(years), [datetime.datetime(2010, 1, 1, 0, 0)])   
     55
     56    def test_django_extract(self):
     57        """
     58        Test the custom ``django_extract method``, in particular against fields
     59        which clash with strings passed to it (e.g. 'day') - see #12818__.
     60
     61        __: http://code.djangoproject.com/ticket/12818
     62
     63        """
     64        updated = datetime.datetime(2010, 2, 20)
     65        models.SchoolClass.objects.create(year=2009, last_updated=updated)
     66        classes = models.SchoolClass.objects.filter(last_updated__day=20)
     67        self.assertEqual(len(classes), 1)
    3668
    3769def connection_created_test(sender, **kwargs):
    3870    print 'connection_created signal'
Back to Top