File: regression

package info (click to toggle)
python-tz 2022.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,100 kB
  • sloc: python: 2,110; makefile: 16
file content (122 lines) | stat: -rwxr-xr-x 5,117 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3

# Copyright (C) 2012-2023 Canonical Ltd.
# Author: Benjamin Drung <benjamin.drung@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Parts taken from
# https://git.launchpad.net/qa-regression-testing/tree/scripts/test-tzdata.py

"""Regression tests for python3-tz."""

import sys
import unittest
from datetime import datetime, timedelta

import pytz


class TZDataTest(unittest.TestCase):
    """Regression tests for python3-tz."""

    def test_lp1965791_gaza(self) -> None:
        """Test Gaza 2022 DST change"""
        timezone = pytz.timezone("Asia/Gaza")
        offset_before = timezone.localize(datetime(2022, 3, 22))
        updated_before = timezone.localize(datetime(2022, 3, 27))
        offset_after = timezone.localize(datetime(2022, 3, 29))

        with self.subTest(testcase=offset_before):
            self.assertEqual(offset_before.utcoffset(), updated_before.utcoffset())
        with self.subTest(testcase=updated_before):
            utc_offset_difference = offset_after.utcoffset() - offset_before.utcoffset()
            self.assertEqual(utc_offset_difference, timedelta(seconds=3600))

    def test_lp1986984_chile(self) -> None:
        """Test Chile 2022 DST date"""
        # chile dst changed from 2022/09/04 to 2022/09/11
        timezone = pytz.timezone("America/Santiago")
        always_before = timezone.localize(datetime(2022, 9, 1))
        now_before = timezone.localize(datetime(2022, 9, 8))
        always_after = timezone.localize(datetime(2022, 9, 12))
        with self.subTest(testcase=always_before):
            self.assertEqual(always_before.utcoffset(), now_before.utcoffset())
        with self.subTest(testcase=now_before):
            utc_offset_difference = always_after.utcoffset() - now_before.utcoffset()
            self.assertEqual(utc_offset_difference, timedelta(seconds=3600))

    def test_lp1992692_gaza(self) -> None:
        """Test Gaza October 2022 DST date"""
        # gaza dst changed from 2022/10/28 to 2022/10/29
        timezone = pytz.timezone("Asia/Gaza")
        offset_before = timezone.localize(datetime(2022, 10, 1))
        updated_before = timezone.localize(datetime(2022, 10, 29))
        offset_after = timezone.localize(datetime(2022, 11, 1))
        # time falls back with this change
        offset_change = -3600

        with self.subTest(testcase=offset_before):
            self.assertEqual(offset_before.utcoffset(), updated_before.utcoffset())
        with self.subTest(testcase=updated_before):
            utc_offset_difference = offset_after.utcoffset() - offset_before.utcoffset()
            self.assertEqual(utc_offset_difference, timedelta(seconds=offset_change))

    def test_all_timezones_count(self) -> None:
        """Test all_timezones count to be reasonable."""
        zones = len(pytz.all_timezones)
        self.assertGreaterEqual(zones, 596, "less zones than 2022g-2")
        self.assertLess(zones, round(596 * 1.1), ">10% more zones than 2022g-2")
        self.assertEqual(len(pytz.all_timezones_set), zones)

    def test_common_timezones_count(self) -> None:
        """Test common_timezones count to be reasonable."""
        self.assertEqual(pytz.common_timezones_set - pytz.all_timezones_set, set())

    def _test_timezone(self, zone: str) -> None:
        """Test zone to load, have a name, and have a reasonable offset."""
        timezone = pytz.timezone(zone)
        self.assertEqual(str(timezone), zone)
        date = timezone.localize(datetime(2020, 10, 31, 12))

        tzname = date.tzname()
        assert tzname is not None
        self.assertGreaterEqual(len(tzname), 3, tzname)
        self.assertLessEqual(len(tzname), 5, tzname)

        utc_offset = date.utcoffset()
        assert utc_offset is not None
        self.assertEqual(int(utc_offset.total_seconds()) % 900, 0)
        self.assertLessEqual(utc_offset, timedelta(hours=14))

    def test_timezones(self) -> None:
        """Test all zones to load, have a name, and have a reasonable offset."""
        for zone in pytz.all_timezones:
            with self.subTest(zone=zone):
                self._test_timezone(zone)

    def test_olson_version(self) -> None:
        """Test IANA (nee Olson) database version was successful determined."""
        self.assertNotEqual(pytz.OLSON_VERSION, "unknown")
        self.assertTrue(pytz.OLSON_VERSION[0].isdigit(), pytz.OLSON_VERSION)


def main() -> None:
    """Run unit tests in verbose mode."""
    argv = sys.argv.copy()
    argv.insert(1, "-v")
    unittest.main(argv=argv)


if __name__ == "__main__":
    main()