diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2015-03-07 13:20:29 -0800 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-06-02 08:49:10 -0400 |
| commit | 44f3ee77166bd5c0e8a4604f2d96015268dce100 (patch) | |
| tree | 733d47ee8d00d3934ae86d8b26275ad4fde4d371 /tests/model_fields | |
| parent | 076a63e672370ff1735e7942cd1ae0990bcbd263 (diff) | |
Fixed #9596 -- Added date transform for DateTimeField.
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/tests.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 8238aeaa93..3e59f6465b 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -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): |
