diff options
| author | Andy Chosak <chosak@gmail.com> | 2014-10-08 15:12:42 -0400 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2014-11-03 15:59:17 +0200 |
| commit | 12e5b87b89074cd0380ddcdadd24a6c621178c07 (patch) | |
| tree | 92dd86e2072c9f66feab29dda5d82e80a3d2041b /tests | |
| parent | 74d0311d6b1fb3ab16072d5fad9de8b996e8af1c (diff) | |
[1.7.x] Fixed #23420 - broken warning for unbound naive datetime objects
Fixed issue with warning message displayed for unbound naive datetime
objects when USE_TZ is True. Adds unit test that demonstrates the issue
(discoverable when using a custom lookup in MySQL).
Backport of ceb1ffcc8d from master.
Conflicts:
tests/custom_lookups/tests.py
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/custom_lookups/models.py | 8 | ||||
| -rw-r--r-- | tests/custom_lookups/tests.py | 34 |
2 files changed, 39 insertions, 3 deletions
diff --git a/tests/custom_lookups/models.py b/tests/custom_lookups/models.py index 9841b36ce5..82a835e160 100644 --- a/tests/custom_lookups/models.py +++ b/tests/custom_lookups/models.py @@ -11,3 +11,11 @@ class Author(models.Model): def __str__(self): return self.name + + +@python_2_unicode_compatible +class MySQLUnixTimestamp(models.Model): + timestamp = models.PositiveIntegerField() + + def __str__(self): + return self.name diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index c89b268f6e..c3df3e8cd4 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -1,13 +1,14 @@ from __future__ import unicode_literals -from datetime import date +from datetime import date, datetime +import time import unittest from django.core.exceptions import FieldError from django.db import models from django.db import connection -from django.test import TestCase -from .models import Author +from django.test import TestCase, override_settings +from .models import Author, MySQLUnixTimestamp class Div3Lookup(models.Lookup): @@ -150,6 +151,18 @@ class InMonth(models.lookups.Lookup): (lhs, rhs, lhs, rhs), params) +class DateTimeTransform(models.Transform): + lookup_name = 'as_datetime' + + @property + def output_field(self): + return models.DateTimeField() + + def as_sql(self, qn, connection): + lhs, params = qn.compile(self.lhs) + return 'from_unixtime({})'.format(lhs), params + + class LookupTests(TestCase): def test_basic_lookup(self): a1 = Author.objects.create(name='a1', age=1) @@ -229,6 +242,21 @@ class LookupTests(TestCase): models.IntegerField._unregister_lookup(Div3Transform) +@override_settings(USE_TZ=True) +class DateTimeLookupTests(TestCase): + @unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific SQL used") + def test_datetime_output_field(self): + models.PositiveIntegerField.register_lookup(DateTimeTransform) + try: + ut = MySQLUnixTimestamp.objects.create(timestamp=time.time()) + y2k = datetime(2000, 1, 1) + self.assertQuerysetEqual( + MySQLUnixTimestamp.objects.filter(timestamp__as_datetime__gt=y2k), + [ut], lambda x: x) + finally: + models.PositiveIntegerField._unregister_lookup(DateTimeTransform) + + class YearLteTests(TestCase): def setUp(self): models.DateField.register_lookup(YearTransform) |
