|
| 1 | +import sys |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +try: |
| 6 | + from importlib import resources |
| 7 | +except ImportError: |
| 8 | + import importlib_resources as resources |
| 9 | + |
| 10 | + |
| 11 | +if sys.version_info < (3, 6): |
| 12 | + # pytest-subtests does not support Python < 3.6, but having the tests |
| 13 | + # separated into clean subtests is nice but not required, so we will create |
| 14 | + # a stub that does nothing but at least doesn't fail for lack of a fixture. |
| 15 | + import contextlib |
| 16 | + |
| 17 | + class _SubTestStub: |
| 18 | + @contextlib.contextmanager |
| 19 | + def test(self, **kwargs): |
| 20 | + yield |
| 21 | + |
| 22 | + _sub_test_stub = _SubTestStub() |
| 23 | + |
| 24 | + @pytest.fixture |
| 25 | + def subtests(): |
| 26 | + yield _sub_test_stub |
| 27 | + |
| 28 | + |
| 29 | +def get_magic(zone_name): |
| 30 | + components = zone_name.split("/") |
| 31 | + package_name = ".".join(["tzdata.zoneinfo"] + components[:-1]) |
| 32 | + resource_name = components[-1] |
| 33 | + |
| 34 | + with resources.open_binary(package_name, resource_name) as f: |
| 35 | + return f.read(4) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "zone_name", |
| 40 | + [ |
| 41 | + "Africa/Cairo", |
| 42 | + "Africa/Casablanca", |
| 43 | + "Africa/Lome", |
| 44 | + "America/Argentina/San_Luis", |
| 45 | + "America/Denver", |
| 46 | + "America/Los_Angeles", |
| 47 | + "America/New_York", |
| 48 | + "America/Thunder_Bay", |
| 49 | + "Antarctica/South_Pole", |
| 50 | + "Asia/Calcutta", |
| 51 | + "Asia/Damascus", |
| 52 | + "Asia/Seoul", |
| 53 | + "Atlantic/Reykjavik", |
| 54 | + "Australia/Perth", |
| 55 | + "Egypt", |
| 56 | + "Etc/GMT-9", |
| 57 | + "Europe/Dublin", |
| 58 | + "Europe/London", |
| 59 | + "Europe/Prague", |
| 60 | + "Hongkong", |
| 61 | + "Indian/Cocos", |
| 62 | + "Indian/Mayotte", |
| 63 | + "Mexico/BajaNorte", |
| 64 | + "Pacific/Guam", |
| 65 | + "Pacific/Kiritimati", |
| 66 | + "US/Eastern", |
| 67 | + "UTC", |
| 68 | + ], |
| 69 | +) |
| 70 | +def test_zone_valid(zone_name): |
| 71 | + """Test an assortment of hard-coded zone names. |
| 72 | +
|
| 73 | + This test checks that the zone resource can be loaded and that it starts |
| 74 | + with the 4-byte magic indicating a TZif file. |
| 75 | + """ |
| 76 | + magic = get_magic(zone_name) |
| 77 | + assert magic == b"TZif" |
| 78 | + |
| 79 | + |
| 80 | +def test_load_zones(subtests): |
| 81 | + with resources.open_text("tzdata", "zones") as f: |
| 82 | + zones = [z.strip() for z in f] |
| 83 | + |
| 84 | + for zone in zones: |
| 85 | + with subtests.test(zone=zone): |
| 86 | + magic = get_magic(zone) |
| 87 | + assert magic == b"TZif" |
0 commit comments