2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2010-09-27 23:13:43 +08:00
|
|
|
import datetime
|
2010-10-11 20:55:17 +08:00
|
|
|
|
|
|
|
from django.test import TestCase, skipIfDBFeature
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import utc
|
2010-09-27 23:13:43 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from .models import Donut, RumBaba
|
|
|
|
|
2010-09-27 23:13:43 +08:00
|
|
|
|
|
|
|
class DataTypesTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_boolean_type(self):
|
|
|
|
d = Donut(name='Apple Fritter')
|
|
|
|
self.assertFalse(d.is_frosted)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(d.has_sprinkles)
|
2010-09-27 23:13:43 +08:00
|
|
|
d.has_sprinkles = True
|
|
|
|
self.assertTrue(d.has_sprinkles)
|
|
|
|
|
|
|
|
d.save()
|
|
|
|
|
|
|
|
d2 = Donut.objects.get(name='Apple Fritter')
|
|
|
|
self.assertFalse(d2.is_frosted)
|
|
|
|
self.assertTrue(d2.has_sprinkles)
|
|
|
|
|
|
|
|
def test_date_type(self):
|
|
|
|
d = Donut(name='Apple Fritter')
|
|
|
|
d.baked_date = datetime.date(year=1938, month=6, day=4)
|
|
|
|
d.baked_time = datetime.time(hour=5, minute=30)
|
|
|
|
d.consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59)
|
|
|
|
d.save()
|
|
|
|
|
|
|
|
d2 = Donut.objects.get(name='Apple Fritter')
|
|
|
|
self.assertEqual(d2.baked_date, datetime.date(1938, 6, 4))
|
|
|
|
self.assertEqual(d2.baked_time, datetime.time(5, 30))
|
|
|
|
self.assertEqual(d2.consumed_at, datetime.datetime(2007, 4, 20, 16, 19, 59))
|
|
|
|
|
|
|
|
def test_time_field(self):
|
2013-11-03 05:02:56 +08:00
|
|
|
# Test for ticket #12059: TimeField wrongly handling datetime.datetime object.
|
2010-09-27 23:13:43 +08:00
|
|
|
d = Donut(name='Apple Fritter')
|
|
|
|
d.baked_time = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59)
|
|
|
|
d.save()
|
|
|
|
|
|
|
|
d2 = Donut.objects.get(name='Apple Fritter')
|
|
|
|
self.assertEqual(d2.baked_time, datetime.time(16, 19, 59))
|
|
|
|
|
|
|
|
def test_year_boundaries(self):
|
2010-10-09 00:14:10 +08:00
|
|
|
"""Year boundary tests (ticket #3689)"""
|
2013-09-09 03:20:01 +08:00
|
|
|
Donut.objects.create(name='Date Test 2007',
|
2010-09-27 23:13:43 +08:00
|
|
|
baked_date=datetime.datetime(year=2007, month=12, day=31),
|
|
|
|
consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59))
|
2013-09-09 03:20:01 +08:00
|
|
|
Donut.objects.create(name='Date Test 2006',
|
2010-09-27 23:13:43 +08:00
|
|
|
baked_date=datetime.datetime(year=2006, month=1, day=1),
|
|
|
|
consumed_at=datetime.datetime(year=2006, month=1, day=1))
|
|
|
|
|
|
|
|
self.assertEqual("Date Test 2007",
|
|
|
|
Donut.objects.filter(baked_date__year=2007)[0].name)
|
|
|
|
|
|
|
|
self.assertEqual("Date Test 2006",
|
|
|
|
Donut.objects.filter(baked_date__year=2006)[0].name)
|
|
|
|
|
2013-09-09 03:20:01 +08:00
|
|
|
Donut.objects.create(name='Apple Fritter',
|
2013-11-04 02:17:58 +08:00
|
|
|
consumed_at=datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59))
|
2010-09-27 23:13:43 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(['Apple Fritter', 'Date Test 2007'],
|
2010-09-27 23:13:43 +08:00
|
|
|
list(Donut.objects.filter(consumed_at__year=2007).order_by('name').values_list('name', flat=True)))
|
|
|
|
|
|
|
|
self.assertEqual(0, Donut.objects.filter(consumed_at__year=2005).count())
|
|
|
|
self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count())
|
|
|
|
|
|
|
|
def test_textfields_unicode(self):
|
2010-10-09 00:14:10 +08:00
|
|
|
"""Regression test for #10238: TextField values returned from the
|
|
|
|
database should be unicode."""
|
2012-06-08 00:08:47 +08:00
|
|
|
d = Donut.objects.create(name='Jelly Donut', review='Outstanding')
|
2010-09-27 23:13:43 +08:00
|
|
|
newd = Donut.objects.get(id=d.id)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(newd.review, six.text_type)
|
2010-09-27 23:13:43 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
@skipIfDBFeature('supports_timezones')
|
|
|
|
def test_error_on_timezone(self):
|
2010-11-13 02:39:30 +08:00
|
|
|
"""Regression test for #8354: the MySQL and Oracle backends should raise
|
|
|
|
an error if given a timezone-aware datetime object."""
|
2011-11-18 21:01:06 +08:00
|
|
|
dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=utc)
|
2010-10-11 20:55:17 +08:00
|
|
|
d = Donut(name='Bear claw', consumed_at=dt)
|
|
|
|
self.assertRaises(ValueError, d.save)
|
|
|
|
# ValueError: MySQL backend does not support timezone-aware datetimes.
|
2010-10-09 00:14:10 +08:00
|
|
|
|
|
|
|
def test_datefield_auto_now_add(self):
|
|
|
|
"""Regression test for #10970, auto_now_add for DateField should store
|
|
|
|
a Python datetime.date, not a datetime.datetime"""
|
|
|
|
b = RumBaba.objects.create()
|
|
|
|
# Verify we didn't break DateTimeField behavior
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(b.baked_timestamp, datetime.datetime)
|
2014-04-27 01:18:45 +08:00
|
|
|
# We need to test this way because datetime.datetime inherits
|
2010-10-09 00:14:10 +08:00
|
|
|
# from datetime.date:
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime)
|