Skip to content

Commit 81a376f

Browse files
committed
Add automated tests for contents
Since apparently this was silently failing, it's probably best to actually add some tests here. There are two testing strategies here: 1. Test that every file in the zones list (and the zones list itself) is included in the package and contains a TZif file. 2. Test a smattering of hard-coded zones - this is in case the zones list populates incorrectly for some reason (e.g. an empty list). I didn't want to exhaustively list all the zones because that seems too much to maintain, but I figure some hard-coded zones from each of the general areas should be a good canary in the coal mine.
1 parent da02ffa commit 81a376f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

tests/test_contents.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
try:
2+
from importlib import resources
3+
except ImportError:
4+
import importlib_resources as resources
5+
6+
import pytest
7+
8+
9+
def get_magic(zone_name):
10+
components = zone_name.split("/")
11+
package_name = ".".join(["tzdata.zoneinfo"] + components[:-1])
12+
resource_name = components[-1]
13+
14+
with resources.open_binary(package_name, resource_name) as f:
15+
return f.read(4)
16+
17+
18+
@pytest.mark.parametrize(
19+
"zone_name",
20+
[
21+
"Africa/Cairo",
22+
"Africa/Casablanca",
23+
"Africa/Lome",
24+
"America/Argentina/San_Luis",
25+
"America/Denver",
26+
"America/Los_Angeles",
27+
"America/New_York",
28+
"America/Thunder_Bay",
29+
"Antarctica/South_Pole",
30+
"Asia/Calcutta",
31+
"Asia/Damascus",
32+
"Asia/Seoul",
33+
"Atlantic/Reykjavik",
34+
"Australia/Perth",
35+
"Egypt",
36+
"Etc/GMT-9",
37+
"Europe/Dublin",
38+
"Europe/London",
39+
"Europe/Prague",
40+
"Hongkong",
41+
"Indian/Cocos",
42+
"Indian/Mayotte",
43+
"Mexico/BajaNorte",
44+
"Pacific/Guam",
45+
"Pacific/Kiritimati",
46+
"US/Eastern",
47+
"UTC",
48+
],
49+
)
50+
def test_zone_valid(zone_name):
51+
"""Test an assortment of hard-coded zone names.
52+
53+
This test checks that the zone resource can be loaded and that it starts
54+
with the 4-byte magic indicating a TZif file.
55+
"""
56+
magic = get_magic(zone_name)
57+
assert magic == b"TZif"
58+
59+
60+
def test_load_zones(subtests):
61+
with resources.open_text("tzdata", "zones") as f:
62+
zones = [z.strip() for z in f]
63+
64+
for zone in zones:
65+
with subtests.test(zone=zone):
66+
magic = get_magic(zone)
67+
assert magic == b"TZif"

tox.ini

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ minversion = 3.3.0
33
isolated_build = True
44
skip_missing_interpreters = true
55

6+
[testenv]
7+
description = Test that the tzdata contents are accessible
8+
deps =
9+
pytest
10+
pytest-subtests
11+
importlib_resources; python_version<'3.7'
12+
commands =
13+
pytest {toxinidir} {posargs}
14+
615
[testenv:update]
716
description = Update the tzdata contents
817
skip_install = True
@@ -30,7 +39,7 @@ deps =
3039
commands =
3140
black .
3241
isort update.py
33-
42+
isort --atomic -rc tests
3443

3544
[testenv:build]
3645
description = Build a wheel and source distribution

0 commit comments

Comments
 (0)