diff --git a/tests/custom_lookups/models.py b/tests/custom_lookups/models.py
index 9841b36..df63fdd 100644
a
|
b
|
|
| 1 | import calendar, time |
1 | 2 | from django.db import models |
2 | 3 | from django.utils.encoding import python_2_unicode_compatible |
3 | 4 | |
… |
… |
class Author(models.Model):
|
11 | 12 | |
12 | 13 | def __str__(self): |
13 | 14 | return self.name |
| 15 | |
| 16 | @python_2_unicode_compatible |
| 17 | class UnixTimestamp(models.Model): |
| 18 | ts = models.PositiveIntegerField() |
| 19 | |
| 20 | def save(self, *args, **kwargs): |
| 21 | if self.ts is None: |
| 22 | self.ts = calendar.timegm(time.gmtime()) |
| 23 | super(UnixTimestamp, self).save(*args, **kwargs) |
| 24 | |
| 25 | def __str__(self): |
| 26 | return self.name |
diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py
index c89b268..3e225c5 100644
a
|
b
|
|
1 | 1 | from __future__ import unicode_literals |
2 | 2 | |
3 | | from datetime import date |
| 3 | from datetime import date, datetime |
4 | 4 | import unittest |
5 | 5 | |
6 | 6 | from django.core.exceptions import FieldError |
7 | 7 | from django.db import models |
8 | 8 | from django.db import connection |
9 | | from django.test import TestCase |
10 | | from .models import Author |
| 9 | from django.test import TestCase, override_settings |
| 10 | from .models import Author, UnixTimestamp |
11 | 11 | |
12 | 12 | |
13 | 13 | class Div3Lookup(models.Lookup): |
… |
… |
class InMonth(models.lookups.Lookup):
|
150 | 150 | (lhs, rhs, lhs, rhs), params) |
151 | 151 | |
152 | 152 | |
| 153 | class DateTimeTransform(models.Transform): |
| 154 | lookup_name = 'as_datetime' |
| 155 | |
| 156 | @property |
| 157 | def output_field(self): |
| 158 | return models.DateTimeField() |
| 159 | |
| 160 | def as_sql(self, qn, connection): |
| 161 | lhs, params = qn.compile(self.lhs) |
| 162 | return 'from_unixtime({})'.format(lhs), params |
| 163 | |
| 164 | |
153 | 165 | class LookupTests(TestCase): |
154 | 166 | def test_basic_lookup(self): |
155 | 167 | a1 = Author.objects.create(name='a1', age=1) |
… |
… |
class LookupTests(TestCase):
|
229 | 241 | models.IntegerField._unregister_lookup(Div3Transform) |
230 | 242 | |
231 | 243 | |
| 244 | @override_settings(USE_TZ=True) |
| 245 | class DateTimeLookupTests(TestCase): |
| 246 | @unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific SQL used") |
| 247 | def test_datetime_output_field(self): |
| 248 | models.PositiveIntegerField.register_lookup(DateTimeTransform) |
| 249 | try: |
| 250 | ut = UnixTimestamp.objects.create() |
| 251 | year_one = datetime(1, 1, 1) |
| 252 | self.assertQuerysetEqual( |
| 253 | UnixTimestamp.objects.filter(ts__as_datetime__gt=year_one), |
| 254 | [ut], lambda x: x) |
| 255 | finally: |
| 256 | models.PositiveIntegerField._unregister_lookup(DateTimeTransform) |
| 257 | |
| 258 | |
232 | 259 | class YearLteTests(TestCase): |
233 | 260 | def setUp(self): |
234 | 261 | models.DateField.register_lookup(YearTransform) |