2010-09-27 23:13:43 +08:00
|
|
|
import datetime
|
2010-10-11 20:55:17 +08:00
|
|
|
|
|
|
|
from django.test import TestCase, skipIfDBFeature
|
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)"""
|
2016-04-08 10:04:45 +08:00
|
|
|
Donut.objects.create(
|
|
|
|
name="Date Test 2007",
|
|
|
|
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
|
|
|
|
),
|
|
|
|
)
|
|
|
|
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),
|
2016-04-08 10:04:45 +08:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
Donut.objects.create(
|
|
|
|
name="Apple Fritter",
|
|
|
|
consumed_at=datetime.datetime(
|
|
|
|
year=2007, month=4, day=20, hour=16, minute=19, second=59
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
["Apple Fritter", "Date Test 2007"],
|
|
|
|
list(
|
|
|
|
Donut.objects.filter(consumed_at__year=2007)
|
|
|
|
.order_by("name")
|
|
|
|
.values_list("name", flat=True)
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2016-04-08 10:04:45 +08:00
|
|
|
)
|
2010-09-27 23:13:43 +08:00
|
|
|
self.assertEqual(0, Donut.objects.filter(consumed_at__year=2005).count())
|
|
|
|
self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count())
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
def test_textfields_str(self):
|
|
|
|
"""TextField values returned from the database should be str."""
|
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)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(newd.review, str)
|
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."""
|
2022-03-23 19:15:36 +08:00
|
|
|
dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=datetime.timezone.utc)
|
2010-10-11 20:55:17 +08:00
|
|
|
d = Donut(name="Bear claw", consumed_at=dt)
|
2016-01-17 19:26:39 +08:00
|
|
|
# MySQL backend does not support timezone-aware datetimes.
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
d.save()
|
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:
|
2015-04-27 17:51:46 +08:00
|
|
|
self.assertIsInstance(b.baked_date, datetime.date)
|
|
|
|
self.assertNotIsInstance(b.baked_date, datetime.datetime)
|