test for ticket 12059 timefield wrongly handling datetime datetime object year boundary tests ticket 3689 donut objects create namedate test 2007 bakeddatedatetime datetimeyear2007 month12 day31 consumedatdatetime datetime year2007 month12 day31 hour23 minute59 second59 donut objects create namedate test 2006 bakeddatedatetime datetimeyear2006 month1 day1 consumedatdatetime datetimeyear2006 month1 day1 self assertequal date test 2007 donut objects filterbakeddateyear20070 name self assertequal date test 2006 donut objects filterbakeddateyear20060 name donut objects create nameapple fritter consumedatdatetime datetime year2007 month4 day20 hour16 minute19 second59 self assertequal apple fritter date test 2007 list donut objects filterconsumedatyear2007 orderbyname valueslistname flattrue self assertequal0 donut objects filterconsumedatyear2005 count self assertequal0 donut objects filterconsumedatyear2008 count def testtextfieldsstrself regression test for 8354 the mysql and oracle backends should raise an error if given a timezoneaware datetime object dt datetime datetime2008 8 31 16 20 tzinfodatetime timezone utc d donutnamebear claw consumedatdt mysql backend does not support timezoneaware datetimes with self assertraisesvalueerror d save def testdatefieldautonowaddself verify we didn t break datetimefield behavior we need to test this way because datetime datetime inherits from datetime date test for ticket 12059 timefield wrongly handling datetime datetime object year boundary tests ticket 3689 textfield values returned from the database should be str regression test for 8354 the mysql and oracle backends should raise an error if given a timezone aware datetime object mysql backend does not support timezone aware datetimes regression test for 10970 auto_now_add for datefield should store a python datetime date not a datetime datetime verify we didn t break datetimefield behavior we need to test this way because datetime datetime inherits from datetime date
import datetime

from django.test import TestCase, skipIfDBFeature

from .models import Donut, RumBaba


class DataTypesTestCase(TestCase):
    def test_boolean_type(self):
        d = Donut(name="Apple Fritter")
        self.assertFalse(d.is_frosted)
        self.assertIsNone(d.has_sprinkles)
        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):
        
        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):
        
        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",
            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
        )

        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)
            ),
        )
        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_str(self):
        
        d = Donut.objects.create(name="Jelly Donut", review="Outstanding")
        newd = Donut.objects.get(id=d.id)
        self.assertIsInstance(newd.review, str)

    @skipIfDBFeature("supports_timezones")
    def test_error_on_timezone(self):
        
        dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=datetime.timezone.utc)
        d = Donut(name="Bear claw", consumed_at=dt)
        
        with self.assertRaises(ValueError):
            d.save()

    def test_datefield_auto_now_add(self):
        
        b = RumBaba.objects.create()
        
        self.assertIsInstance(b.baked_timestamp, datetime.datetime)
        
        
        self.assertIsInstance(b.baked_date, datetime.date)
        self.assertNotIsInstance(b.baked_date, datetime.datetime)