PageRenderTime 103ms CodeModel.GetById 84ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/datatypes/tests.py

https://code.google.com/p/mango-py/
Python | 94 lines | 63 code | 19 blank | 12 comment | 2 complexity | bf24db2bf104972cfcd386537d7df28f MD5 | raw file
 1import datetime
 2
 3from django.conf import settings
 4from django.db import DEFAULT_DB_ALIAS
 5from django.test import TestCase, skipIfDBFeature
 6from django.utils import tzinfo
 7
 8from models import Donut, RumBaba
 9
10class DataTypesTestCase(TestCase):
11
12    def test_boolean_type(self):
13        d = Donut(name='Apple Fritter')
14        self.assertFalse(d.is_frosted)
15        self.assertTrue(d.has_sprinkles is None)
16        d.has_sprinkles = True
17        self.assertTrue(d.has_sprinkles)
18
19        d.save()
20
21        d2 = Donut.objects.get(name='Apple Fritter')
22        self.assertFalse(d2.is_frosted)
23        self.assertTrue(d2.has_sprinkles)
24
25    def test_date_type(self):
26        d = Donut(name='Apple Fritter')
27        d.baked_date = datetime.date(year=1938, month=6, day=4)
28        d.baked_time = datetime.time(hour=5, minute=30)
29        d.consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59)
30        d.save()
31
32        d2 = Donut.objects.get(name='Apple Fritter')
33        self.assertEqual(d2.baked_date, datetime.date(1938, 6, 4))
34        self.assertEqual(d2.baked_time, datetime.time(5, 30))
35        self.assertEqual(d2.consumed_at, datetime.datetime(2007, 4, 20, 16, 19, 59))
36
37    def test_time_field(self):
38        #Test for ticket #12059: TimeField wrongly handling datetime.datetime object.
39        d = Donut(name='Apple Fritter')
40        d.baked_time = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59)
41        d.save()
42
43        d2 = Donut.objects.get(name='Apple Fritter')
44        self.assertEqual(d2.baked_time, datetime.time(16, 19, 59))
45
46    def test_year_boundaries(self):
47        """Year boundary tests (ticket #3689)"""
48        d = Donut.objects.create(name='Date Test 2007',
49             baked_date=datetime.datetime(year=2007, month=12, day=31),
50             consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59))
51        d1 = Donut.objects.create(name='Date Test 2006',
52            baked_date=datetime.datetime(year=2006, month=1, day=1),
53            consumed_at=datetime.datetime(year=2006, month=1, day=1))
54
55        self.assertEqual("Date Test 2007",
56                         Donut.objects.filter(baked_date__year=2007)[0].name)
57
58        self.assertEqual("Date Test 2006",
59                         Donut.objects.filter(baked_date__year=2006)[0].name)
60
61        d2 = Donut.objects.create(name='Apple Fritter',
62            consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59))
63
64        self.assertEqual([u'Apple Fritter', u'Date Test 2007'],
65            list(Donut.objects.filter(consumed_at__year=2007).order_by('name').values_list('name', flat=True)))
66
67        self.assertEqual(0, Donut.objects.filter(consumed_at__year=2005).count())
68        self.assertEqual(0, Donut.objects.filter(consumed_at__year=2008).count())
69
70    def test_textfields_unicode(self):
71        """Regression test for #10238: TextField values returned from the
72        database should be unicode."""
73        d = Donut.objects.create(name=u'Jelly Donut', review=u'Outstanding')
74        newd = Donut.objects.get(id=d.id)
75        self.assertTrue(isinstance(newd.review, unicode))
76
77    @skipIfDBFeature('supports_timezones')
78    def test_error_on_timezone(self):
79        """Regression test for #8354: the MySQL and Oracle backends should raise
80        an error if given a timezone-aware datetime object."""
81        dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0))
82        d = Donut(name='Bear claw', consumed_at=dt)
83        self.assertRaises(ValueError, d.save)
84        # ValueError: MySQL backend does not support timezone-aware datetimes.
85
86    def test_datefield_auto_now_add(self):
87        """Regression test for #10970, auto_now_add for DateField should store
88        a Python datetime.date, not a datetime.datetime"""
89        b = RumBaba.objects.create()
90        # Verify we didn't break DateTimeField behavior
91        self.assertTrue(isinstance(b.baked_timestamp, datetime.datetime))
92        # We need to test this this way because datetime.datetime inherits
93        # from datetime.date:
94        self.assertTrue(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime))