| 1 | """These are tests to make sure that the mixture of datetime and date works. |
| 2 | |
| 3 | See https://github.com/niccokunzmann/python-recurring-ical-events/issues/201 |
| 4 | """ |
| 5 | |
| 6 | from datetime import date, datetime, timedelta |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from recurring_ical_events.types import Time |
| 11 | from recurring_ical_events.util import has_timezone, is_date, is_datetime |
| 12 | |
| 13 | |
| 14 | def test_can_calculate_times_of_issue_example(calendars, utc): |
| 15 | """The example from the issue should work.""" |
| 16 | events = list(calendars.issue_201_mixed_datetime_and_date.all()) |
| 17 | assert len(events) == 1 |
| 18 | event = events[0] |
| 19 | assert event.start == datetime(2023, 7, 24, 0, 0, tzinfo=utc) |
| 20 | assert event.end == datetime(2023, 8, 17, 0, 0, tzinfo=utc) |
| 21 | |
| 22 | |
| 23 | def test_is_date(utc): |
| 24 | """Identify dates.""" |
| 25 | assert is_date(date(2023, 7, 24)) |
| 26 | assert not is_date(datetime(2023, 7, 24, 0, 0, tzinfo=utc)) |
| 27 | assert not is_date(datetime(2023, 7, 24, 0, 0)) |
| 28 | |
| 29 | |
| 30 | def test_is_datetime(utc): |
| 31 | """Identify datetimes.""" |
| 32 | assert is_datetime(datetime(2023, 7, 24, 0, 0, tzinfo=utc)) |
| 33 | assert not is_datetime(date(2023, 7, 24)) |
| 34 | assert is_datetime(datetime(2023, 7, 24, 0, 0)) |
| 35 | |
| 36 | |
| 37 | def test_has_timezone(utc): |
| 38 | """Check if we have a timezone.""" |
| 39 | assert has_timezone(datetime(2023, 7, 24, 0, 0, tzinfo=utc)) |
| 40 | assert not has_timezone(datetime(2023, 7, 24, 0, 0)) |
| 41 | assert not has_timezone(date(2023, 7, 24)) |
| 42 | |
| 43 | |
| 44 | @pytest.mark.parametrize("component", ["VEVENT", "VTODO"]) |
| 45 | @pytest.mark.parametrize("start", ["DATE", "DATETIME", "UTC"]) |
| 46 | @pytest.mark.parametrize("end", ["DATE", "DATETIME", "UTC", "DAYS", "HOURS"]) |
| 47 | def test_all_possiblilities_are_considered(calendars, component, start, end): |
| 48 | """All possibilities are present in the result.""" |
| 49 | calendars.components = [component] |
| 50 | uids = {str(c["UID"]) for c in calendars.issue_201_test_matrix.all()} |
| 51 | assert f"{component}-{start}-{end}" in uids |
| 52 | |
| 53 | |
| 54 | @pytest.mark.parametrize( |
| 55 | ("end", "duration"), |
| 56 | [ |
| 57 | ("DATE", timedelta(days=1)), |
| 58 | ("DATETIME", timedelta(days=1, hours=4)), |
| 59 | ("UTC", timedelta(days=2, hours=2)), |
| 60 | ("DAYS", timedelta(days=3)), |
| 61 | ("HOURS", timedelta(hours=10)), |
| 62 | ], |
| 63 | ) |
| 64 | def test_duration(calendars, end, duration): |
| 65 | """Check the duration.""" |
| 66 | calendars.components = ["VEVENT", "VTODO"] |
| 67 | components = [ |
| 68 | c for c in calendars.issue_201_test_matrix.all() if c["UID"].endswith(end) |
| 69 | ] |
| 70 | for component in components: |
| 71 | uid = str(component["UID"]) |
| 72 | assert component.duration == duration, uid |
| 73 | assert component.end - component.start == duration, uid |
| 74 | |
| 75 | |
| 76 | def is_datetime_without_timezone(dt: Time) -> bool: |
| 77 | """Wether this has no timrzone.""" |
| 78 | return is_datetime(dt) and not has_timezone(dt) |
| 79 | |
| 80 | |
| 81 | def is_datetime_with_timezone(dt: Time) -> bool: |
| 82 | """Wether this has no timrzone.""" |
| 83 | return is_datetime(dt) and has_timezone(dt) |
| 84 | |
| 85 | |
| 86 | def is_days(td: timedelta): |
| 87 | """Wether this is only days, not hours.""" |
| 88 | return td.days != 0 and td.seconds == 0 |
| 89 | |
| 90 | |
| 91 | def is_hours(td: timedelta): |
| 92 | """Wether this is only days, not hours.""" |
| 93 | return td.seconds != 0 |
| 94 | |
| 95 | |
| 96 | @pytest.mark.parametrize( |
| 97 | ("uid", "check"), |
| 98 | [ |
| 99 | ("DATE", is_date), |
| 100 | ("DATETIME", is_datetime_without_timezone), |
| 101 | ("UTC", is_datetime_with_timezone), |
| 102 | ], |
| 103 | ) |
| 104 | def test_start_is_correct(calendars, uid, check): |
| 105 | """Check the start of the components.""" |
| 106 | components = calendars.raw.issue_201_test_matrix.walk( |
| 107 | select=lambda c: c.get("UID", "--").split("-")[1] == uid |
| 108 | ) |
| 109 | for component in components: |
| 110 | assert check(component.DTSTART), f"{component['UID']}.DTSTART" |
| 111 | |
| 112 | |
| 113 | @pytest.mark.parametrize( |
| 114 | ("uid", "check"), |
| 115 | [ |
| 116 | ("DATE", is_date), |
| 117 | ("DATETIME", is_datetime_without_timezone), |
| 118 | ("UTC", is_datetime_with_timezone), |
| 119 | ("DAYS", is_days), |
| 120 | ("HOURS", is_hours), |
| 121 | ], |
| 122 | ) |
| 123 | def test_end_is_correct(calendars, uid, check): |
| 124 | """Check the start of the components.""" |
| 125 | components = calendars.raw.issue_201_test_matrix.walk( |
| 126 | select=lambda c: c.get("UID", "--").split("-")[2] == uid |
| 127 | ) |
| 128 | for component in components: |
| 129 | if uid in ("DAYS", "HOURS"): |
| 130 | assert check(component.DURATION) |
| 131 | elif component.name == "VEVENT": |
| 132 | assert check(component.DTEND), f"{component['UID']}.DTEND" |
| 133 | else: |
| 134 | assert component.name == "VTODO" |
| 135 | assert check(component.DUE), f"{component['UID']}.DUE" |