Debian Codegraph @

recurring_ical_events/test/test_zoneinfo_issue_57.py

raw symbols SCIP
1 """Test that zoneinfo timezones can be used.
2
3 See also Issue https://github.com/niccokunzmann/python-recurring-ical-events/issues/57
4 """
5
6 import sys
7 from datetime import datetime, timedelta
8
9 import pytest
10 import pytz
11 from icalendar import Calendar, Event, vDDDTypes
12
13 import recurring_ical_events
14 import recurring_ical_events.util
15
16
17 def test_zoneinfo_example_yields_events(ZoneInfo): # noqa: N803
18 """Test that there is no error.
19
20 Source code is taken from Issue 57.
21 """
22 tz = ZoneInfo("Europe/London")
23
24 cal = Calendar()
25 event = Event()
26 cal.add_component(event)
27
28 dt = datetime(2021, 6, 24, 21, 15).astimezone().astimezone(tz)
29 # datetime.datetime(2021, 6, 24, 21, 15, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
30 d = dt.date()
31
32 event["dtstart"] = vDDDTypes(dt)
33
34 events = recurring_ical_events.of(cal).between(d, d + timedelta(1))
35
36 assert len(events) == 1, "The event was found."
37
38
39 def test_zoneinfo_must_be_installed_if_it_is_possible():
40 """Make sure that zoneinfo and tzdata are installed if possible."""
41 python_version = sys.version_info[:2]
42 if python_version < (3, 7):
43 return # no zoneinfo
44 from importlib.util import find_spec as module_exists
45
46 if python_version <= (3, 8):
47 assert module_exists("backports.zoneinfo"), (
48 "zoneinfo should be installed with pip install backports.zoneinfo"
49 )
50 else:
51 assert module_exists("zoneinfo"), "We assume that zoneinfo exists."
52 assert module_exists("tzdata"), (
53 "tzdata is necessary to test current time zone understanding."
54 )
55
56
57 @pytest.mark.parametrize(
58 "dt1",
59 [
60 datetime(2019, 4, 24, 19),
61 pytz.timezone("Europe/Berlin").localize(datetime(2019, 4, 24, 19)),
62 pytz.timezone("America/New_York").localize(datetime(2019, 4, 24, 19)),
63 ],
64 )
65 def test_zoneinfo_consistent_conversion(calendars, dt1):
66 """Make sure that the conversion function actually works."""
67 dt2 = calendars.consistent_tz(dt1)
68 assert dt1.year == dt2.year
69 assert dt1.month == dt2.month
70 assert dt1.day == dt2.day
71 assert dt1.hour == dt2.hour
72 assert dt1.minute == dt2.minute
73 assert dt1.second == dt2.second
74
75
76 ATTRS = ["year", "month", "day", "hour", "minute", "second"]
77
78
79 @pytest.mark.parametrize(
80 ("dt", "tz", "times"),
81 [
82 (datetime(2019, 2, 22, 4, 30), "Europe/Berlin", (2019, 2, 22, 4, 30)),
83 (datetime(2019, 2, 22, 4, 30), "UTC", (2019, 2, 22, 4, 30)),
84 ],
85 )
86 def test_convert_to_date(dt, tz, times, ZoneInfo): # noqa: N803
87 """Check that a datetime conversion takes place properly."""
88 new = recurring_ical_events.util.convert_to_datetime(dt, ZoneInfo(tz))
89 converted = ()
90 for attr, _ in zip(ATTRS, times):
91 converted += (getattr(new, attr),)
92 assert converted == times