Fixed #9596 -- Added date transform for DateTimeField.

This commit is contained in:
Jon Dufresne 2015-03-07 13:20:29 -08:00 committed by Tim Graham
parent 076a63e672
commit 44f3ee7716
10 changed files with 155 additions and 50 deletions

View file

@ -18,7 +18,8 @@ from django.db.models.fields import (
TimeField, URLField,
)
from django.db.models.fields.files import FileField, ImageField
from django.utils import six
from django.test.utils import requires_tz_support
from django.utils import six, timezone
from django.utils.functional import lazy
from .models import (
@ -274,6 +275,48 @@ class DateTimeFieldTests(test.TestCase):
self.assertEqual(obj.dt, datetim)
self.assertEqual(obj.t, tim)
@test.override_settings(USE_TZ=False)
def test_lookup_date_without_use_tz(self):
d = datetime.date(2014, 3, 12)
dt1 = datetime.datetime(2014, 3, 12, 21, 22, 23, 240000)
dt2 = datetime.datetime(2014, 3, 11, 21, 22, 23, 240000)
t = datetime.time(21, 22, 23, 240000)
m = DateTimeModel.objects.create(d=d, dt=dt1, t=t)
# Other model with different datetime.
DateTimeModel.objects.create(d=d, dt=dt2, t=t)
self.assertEqual(m, DateTimeModel.objects.get(dt__date=d))
@requires_tz_support
@test.skipUnlessDBFeature('has_zoneinfo_database')
@test.override_settings(USE_TZ=True, TIME_ZONE='America/Vancouver')
def test_lookup_date_with_use_tz(self):
d = datetime.date(2014, 3, 12)
# The following is equivalent to UTC 2014-03-12 18:34:23.24000.
dt1 = datetime.datetime(
2014, 3, 12, 10, 22, 23, 240000,
tzinfo=timezone.get_current_timezone()
)
# The following is equivalent to UTC 2014-03-13 05:34:23.24000.
dt2 = datetime.datetime(
2014, 3, 12, 21, 22, 23, 240000,
tzinfo=timezone.get_current_timezone()
)
t = datetime.time(21, 22, 23, 240000)
m1 = DateTimeModel.objects.create(d=d, dt=dt1, t=t)
m2 = DateTimeModel.objects.create(d=d, dt=dt2, t=t)
# In Vancouver, we expect both results.
self.assertQuerysetEqual(
DateTimeModel.objects.filter(dt__date=d),
[repr(m1), repr(m2)],
ordered=False
)
with self.settings(TIME_ZONE='UTC'):
# But in UTC, the __date only matches one of them.
self.assertQuerysetEqual(
DateTimeModel.objects.filter(dt__date=d),
[repr(m1)]
)
class BooleanFieldTests(test.TestCase):
def _test_get_db_prep_lookup(self, f):