def test_serialise_with_naive_datetime(self): """ This tests that naive datetimes are saved as UTC """ # Time is in America/Chicago time log = Log(time=self.WAGTAIL_05_RELEASE_DATETIME, data="Wagtail 0.5 released") log_json = json.loads(log.to_json()) # Now check that the time is stored correctly with the timezone information at the end self.assertEqual(log_json['time'], '2014-08-01T16:01:42Z')
def test_serialise_with_aware_datetime(self): """ This tests that aware datetimes are converted to as UTC """ # make an aware datetime, consisting of WAGTAIL_05_RELEASE_DATETIME # in a timezone 1hr west of UTC one_hour_west = timezone.get_fixed_timezone(-60) local_time = timezone.make_aware(self.WAGTAIL_05_RELEASE_DATETIME, one_hour_west) log = Log(time=local_time, data="Wagtail 0.5 released") log_json = json.loads(log.to_json()) # Now check that the time is stored correctly with the timezone information at the end self.assertEqual(log_json['time'], '2014-08-01T12:01:42Z')
def test_deserialise_with_local_datetime(self): """ This tests that a datetime without timezone information is interpreted as a local time """ log = Log.from_json('{"data": "Wagtail 0.5 released", "time": "2014-08-01T11:01:42", "pk": null}') expected_time = timezone.make_aware(self.WAGTAIL_05_RELEASE_DATETIME, timezone.get_default_timezone()) self.assertEqual(log.time, expected_time) self.assertEqual(log.time.tzinfo, expected_time.tzinfo)
def test_serialise_with_aware_datetime(self): """ This tests that aware datetimes are converted to as UTC """ # make an aware datetime, consisting of WAGTAIL_05_RELEASE_DATETIME # in a timezone 1hr west of UTC try: one_hour_west = timezone.get_fixed_timezone(-60) except AttributeError: # use deprecated-in-Django-1.7 class constructor from django.utils.tzinfo import FixedOffset one_hour_west = FixedOffset(-60) local_time = timezone.make_aware(self.WAGTAIL_05_RELEASE_DATETIME, one_hour_west) log = Log(time=local_time, data="Wagtail 0.5 released") log_json = json.loads(log.to_json()) # Now check that the time is stored correctly with the timezone information at the end self.assertEqual(log_json['time'], '2014-08-01T12:01:42Z')
def test_deserialise_with_utc_datetime(self): """ This tests that a datetimes saved as UTC are converted back correctly """ # Time is in UTC log = Log.from_json('{"data": "Wagtail 0.5 released", "time": "2014-08-01T16:01:42Z", "pk": null}') # Naive and aware timezones cannot be compared so make the release date timezone-aware before comparison expected_time = timezone.make_aware(self.WAGTAIL_05_RELEASE_DATETIME, timezone.get_default_timezone()) # Check that the datetime is correct and was converted back into the correct timezone self.assertEqual(log.time, expected_time) self.assertEqual(log.time.tzinfo, expected_time.tzinfo)
def test_date_filters(self): tmbg = Band(name="They Might Be Giants", albums=[ Album(name="Flood", release_date=datetime.date(1990, 1, 1)), Album(name="John Henry", release_date=datetime.date(1994, 7, 21)), Album(name="Factory Showroom", release_date=datetime.date(1996, 3, 30)), Album(name="The Complete Dial-A-Song", release_date=None), ]) logs = FakeQuerySet(Log, [ Log(time=datetime.datetime(1979, 7, 1, 1, 1, 1), data="nobody died"), Log(time=datetime.datetime(1980, 2, 2, 2, 2, 2), data="one person died"), Log(time=None, data="nothing happened") ]) self.assertEqual( tmbg.albums.get( release_date__range=(datetime.date(1994, 1, 1), datetime.date(1994, 12, 31))).name, "John Henry") self.assertEqual( logs.get(time__range=( datetime.datetime(1980, 1, 1, 1, 1, 1), datetime.datetime(1980, 12, 31, 23, 59, 59))).data, "one person died") self.assertEqual( tmbg.albums.get( release_date__date=datetime.date(1994, 7, 21)).name, "John Henry") self.assertEqual( logs.get(time__date=datetime.date(1980, 2, 2)).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__year='1994').name, "John Henry") self.assertEqual(logs.get(time__year=1980).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__month=7).name, "John Henry") self.assertEqual(logs.get(time__month='2').data, "one person died") self.assertEqual( tmbg.albums.get(release_date__day='21').name, "John Henry") self.assertEqual(logs.get(time__day=2).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__week=29).name, "John Henry") self.assertEqual(logs.get(time__week='5').data, "one person died") self.assertEqual( tmbg.albums.get(release_date__week_day=5).name, "John Henry") self.assertEqual(logs.get(time__week_day=7).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__quarter=3).name, "John Henry") self.assertEqual(logs.get(time__quarter=1).data, "one person died") self.assertEqual( logs.get(time__time=datetime.time(2, 2, 2)).data, "one person died") self.assertEqual(logs.get(time__hour=2).data, "one person died") self.assertEqual(logs.get(time__minute='2').data, "one person died") self.assertEqual(logs.get(time__second=2).data, "one person died")
def test_deserialise_with_null_datetime(self): log = Log.from_json('{"data": "Someone scanned a QR code", "time": null, "pk": null}') self.assertEqual(log.time, None)
def test_serialise_with_null_datetime(self): log = Log(time=None, data="Someone scanned a QR code") log_json = json.loads(log.to_json()) self.assertEqual(log_json['time'], None)