Ticket #23420: datetime_custom_lookup_test.patch

File datetime_custom_lookup_test.patch, 2.7 KB (added by Andy Chosak, 10 years ago)
  • tests/custom_lookups/models.py

    diff --git a/tests/custom_lookups/models.py b/tests/custom_lookups/models.py
    index 9841b36..df63fdd 100644
    a b  
     1import calendar, time
    12from django.db import models
    23from django.utils.encoding import python_2_unicode_compatible
    34
    class Author(models.Model):  
    1112
    1213    def __str__(self):
    1314        return self.name
     15
     16@python_2_unicode_compatible
     17class 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
  • tests/custom_lookups/tests.py

    diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py
    index c89b268..3e225c5 100644
    a b  
    11from __future__ import unicode_literals
    22
    3 from datetime import date
     3from datetime import date, datetime
    44import unittest
    55
    66from django.core.exceptions import FieldError
    77from django.db import models
    88from django.db import connection
    9 from django.test import TestCase
    10 from .models import Author
     9from django.test import TestCase, override_settings
     10from .models import Author, UnixTimestamp
    1111
    1212
    1313class Div3Lookup(models.Lookup):
    class InMonth(models.lookups.Lookup):  
    150150                (lhs, rhs, lhs, rhs), params)
    151151
    152152
     153class 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
    153165class LookupTests(TestCase):
    154166    def test_basic_lookup(self):
    155167        a1 = Author.objects.create(name='a1', age=1)
    class LookupTests(TestCase):  
    229241            models.IntegerField._unregister_lookup(Div3Transform)
    230242
    231243
     244@override_settings(USE_TZ=True)
     245class 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
    232259class YearLteTests(TestCase):
    233260    def setUp(self):
    234261        models.DateField.register_lookup(YearTransform)
Back to Top