|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +# |
| 3 | +# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +import os |
| 23 | +import sys |
| 24 | +import unittest |
| 25 | +from datetime import datetime, date |
| 26 | +from unittest import TestCase, mock |
| 27 | + |
| 28 | +import time_machine |
| 29 | + |
| 30 | + |
| 31 | +class DateTodayTest(TestCase): |
| 32 | + """ |
| 33 | + `date.today()` returns the current local date, as advertised in the |
| 34 | + documentation [1]. Thus, it depends on the system time zone. |
| 35 | +
|
| 36 | + The following test cases demonstrate that the test suite previously |
| 37 | + failed around midnight, where the UTC vs. non-UTC days overlapped, |
| 38 | + and when running on machines with non-UTC time zone. |
| 39 | +
|
| 40 | + On the other hand, `datetime.utcnow().date()` works equally well in all |
| 41 | + situations, so we want to use that within the SQLAlchemy test cases. |
| 42 | +
|
| 43 | + Funny enough, the problem is not observable on Linux? |
| 44 | +
|
| 45 | + [1] https://docs.python.org/3/library/datetime.html#datetime.date.today |
| 46 | + """ |
| 47 | + |
| 48 | + @mock.patch.dict(os.environ, {"TZ": "UTC"}) |
| 49 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 50 | + def test_date_today_depends_on_system_timezone_success_on_utc(self): |
| 51 | + today_local = date.today() |
| 52 | + today_utc = datetime.utcnow().date() |
| 53 | + self.assertEqual(today_local, today_utc) |
| 54 | + self.assertEqual(today_local, date(2022, 7, 21)) |
| 55 | + self.assertEqual(today_utc, date(2022, 7, 21)) |
| 56 | + |
| 57 | + @unittest.skipIf(sys.platform == "linux", "Problem not observable on Linux") |
| 58 | + @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"}) |
| 59 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 60 | + def test_date_today_depends_on_system_timezone_failure_on_non_utc(self): |
| 61 | + today_local = date.today() |
| 62 | + today_utc = datetime.utcnow().date() |
| 63 | + self.assertNotEqual(today_local, today_utc) |
| 64 | + self.assertEqual(today_local, date(2022, 7, 22)) |
| 65 | + self.assertEqual(today_utc, date(2022, 7, 21)) |
| 66 | + |
| 67 | + @mock.patch.dict(os.environ, {"TZ": "UTC"}) |
| 68 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 69 | + def test_date_today_utc(self): |
| 70 | + today_local = date.today() |
| 71 | + self.assertEqual(today_local, date(2022, 7, 21)) |
| 72 | + |
| 73 | + @unittest.skipIf(sys.platform == "linux", "Problem not observable on Linux") |
| 74 | + @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"}) |
| 75 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 76 | + def test_date_today_non_utc(self): |
| 77 | + today_local = date.today() |
| 78 | + self.assertEqual(today_local, date(2022, 7, 22)) |
| 79 | + |
| 80 | + @mock.patch.dict(os.environ, {"TZ": "UTC"}) |
| 81 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 82 | + def test_utcnow_date_utc(self): |
| 83 | + today_utc = datetime.utcnow().date() |
| 84 | + self.assertEqual(today_utc, date(2022, 7, 21)) |
| 85 | + |
| 86 | + @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"}) |
| 87 | + @time_machine.travel("2022-07-22T00:42:00+0200") |
| 88 | + def test_utcnow_date_non_utc(self): |
| 89 | + today_utc = datetime.utcnow().date() |
| 90 | + self.assertEqual(today_utc, date(2022, 7, 21)) |
0 commit comments