File: fix-calendar.patch

package info (click to toggle)
pyicloud 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 828 kB
  • sloc: python: 9,741; sh: 17; makefile: 3
file content (172 lines) | stat: -rw-r--r-- 7,782 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
commit fd3d09b1ee451ccbcbe84c8a7acd39655752d288
Author: Jacob Arnould <jacob@jacob-arnould.com>
Date:   Thu Sep 4 22:48:11 2025 +0200

    calendar: fix monthrange misuse; use day=1 and days_in_month (fixes #116) (#117)
    
    * calendar: fix monthrange misuse; use day=1 and days_in_month (fixes #116)
    
    * calendar: anchor missing bounds to provided month; use replace(); tests: Feb bounds + anchoring; confirm endDate inclusive
    
    * calendar: anchor missing bounds to provided month; use replace(); tests: Feb bounds + anchoring; confirm endDate inclusive
    
    * calendar: document refresh_client range semantics and inclusive endDate
    
    ---------
    
    Co-authored-by: Tim Laing <11019084+timlaing@users.noreply.github.com>

Index: pyicloud/pyicloud/services/calendar.py
===================================================================
--- pyicloud.orig/pyicloud/services/calendar.py	2025-09-08 00:04:30.400669499 +0200
+++ pyicloud/pyicloud/services/calendar.py	2025-09-08 00:04:30.396669497 +0200
@@ -220,9 +220,13 @@
     def default_params(self) -> dict[str, Any]:
         """Returns the default parameters for the calendar service."""
         today: datetime = datetime.today()
-        first_day, last_day = monthrange(today.year, today.month)
-        from_dt = datetime(today.year, today.month, first_day)
-        to_dt = datetime(today.year, today.month, last_day)
+        _, days_in_month = monthrange(
+            today.year, today.month
+        )  # monthrange returns: weekday of the first day of the month (0 -> Mon, 6 -> Sun) and number of days in the month (Jan -> 31, Feb -> 28/29, etc.)
+        from_dt = datetime(
+            today.year, today.month, 1
+        )  # Hardcoded to 1 so that startDate is always the first (1st) day of the month
+        to_dt = datetime(today.year, today.month, days_in_month)
         params = dict(self.params)
         params.update(
             {
@@ -257,16 +261,32 @@
 
     def refresh_client(self, from_dt=None, to_dt=None) -> dict[str, Any]:
         """
-        Refreshes the CalendarService endpoint, ensuring that the
-        event data is up-to-date. If no 'from_dt' or 'to_dt' datetimes
-        have been given, the range becomes this month.
+        Refresh the Calendar service and return a fresh event payload.
+
+        Date range semantics:
+        - If both 'from_dt' and 'to_dt' are provided, they are respected as-is.
+        - If exactly one bound is provided, the missing bound is anchored to the
+          same month as the provided bound and expanded to the full month
+          (1st..last day of that month).
+        - If neither is provided, defaults to the current month.
+
+        Notes:
+        - Apple's Calendar API treats 'endDate' as inclusive; events occurring on
+          the last day of the month (including all-day and 23:00->00:00 boundary
+          events) are returned. See tests in tests/test_calendar.py.
         """
         today: datetime = datetime.today()
-        first_day, last_day = monthrange(today.year, today.month)
-        if not from_dt:
-            from_dt = datetime(today.year, today.month, first_day)
-        if not to_dt:
-            to_dt = datetime(today.year, today.month, last_day)
+        # Anchor missing bound(s) to whichever bound is provided, else to 'today'
+        anchor: datetime = from_dt or to_dt or today
+        year, month = anchor.year, anchor.month
+        _, days_in_month = monthrange(
+            year, month
+        )  # (weekday_of_first_day, days_in_month)
+        # If either bound is missing, normalize to the full month of the anchor.
+        # When both bounds are provided (e.g., day/week queries), respect them as-is.
+        if from_dt is None or to_dt is None:
+            from_dt = anchor.replace(day=1)
+            to_dt = anchor.replace(day=days_in_month)
         params = dict(self.params)
         params.update(
             {
Index: pyicloud/tests/test_calendar.py
===================================================================
--- pyicloud.orig/tests/test_calendar.py	2025-09-08 00:04:30.400669499 +0200
+++ pyicloud/tests/test_calendar.py	2025-09-08 00:07:22.040748496 +0200
@@ -160,3 +160,87 @@
         event = EventObject(pguid="calendar123", title="New Event")
         response = service.remove_event(event)
         assert response["status"] == "success"
+
+def _service_with_mocks(mock_session: PyiCloudSession) -> CalendarService:
+    with patch("pyicloud.services.calendar.get_localzone_name", return_value="UTC"):
+        return CalendarService("https://example.com", mock_session, {"dsid": "12345"})
+
+
+class _FixedDateTime(datetime):
+    """Subclass datetime to control today() for tests."""
+
+    fixed: datetime = datetime(2025, 2, 10)
+
+    @classmethod
+    def today(cls) -> "_FixedDateTime":  # type: ignore[override]
+        return cls.fromtimestamp(cls.fixed.timestamp())
+
+
+def test_default_params_feb_non_leap() -> None:
+    """default_params should compute Feb (non-leap) as 1..28."""
+    mock_session = MagicMock(spec=PyiCloudSession)
+    service = _service_with_mocks(mock_session)
+
+    # Freeze 'today' to 2025-02-10 (non-leap year)
+    _FixedDateTime.fixed = datetime(2025, 2, 10)
+    with (
+        patch("pyicloud.services.calendar.datetime", _FixedDateTime),
+        patch("pyicloud.services.calendar.get_localzone_name", return_value="UTC"),
+    ):
+        params = service.default_params
+        assert params["startDate"] == "2025-02-01"
+        assert params["endDate"] == "2025-02-28"
+
+
+def test_default_params_feb_leap() -> None:
+    """default_params should compute Feb (leap year) as 1..29."""
+    mock_session = MagicMock(spec=PyiCloudSession)
+    service = _service_with_mocks(mock_session)
+
+    # Freeze 'today' to 2028-02-10 (leap year)
+    _FixedDateTime.fixed = datetime(2028, 2, 10)
+    with (
+        patch("pyicloud.services.calendar.datetime", _FixedDateTime),
+        patch("pyicloud.services.calendar.get_localzone_name", return_value="UTC"),
+    ):
+        params = service.default_params
+        assert params["startDate"] == "2028-02-01"
+        assert params["endDate"] == "2028-02-29"
+
+
+def test_refresh_client_anchors_from_dt_month() -> None:
+    """When only from_dt is provided, anchor to its month for the end bound."""
+    mock_session = MagicMock(spec=PyiCloudSession)
+    mock_response = MagicMock(spec=Response)
+    mock_response.json.return_value = {"Event": []}
+    mock_session.get.return_value = mock_response
+    service = _service_with_mocks(mock_session)
+
+    from_dt = datetime(2025, 3, 15)
+    with patch("pyicloud.services.calendar.get_localzone_name", return_value="UTC"):
+        service.refresh_client(from_dt=from_dt, to_dt=None)
+
+    # Inspect params passed to GET
+    _, kwargs = mock_session.get.call_args
+    params = kwargs["params"]
+    assert params["startDate"] == "2025-03-01"
+    assert params["endDate"] == "2025-03-31"
+
+
+def test_refresh_client_anchors_to_dt_month() -> None:
+    """When only to_dt is provided, anchor to its month for the start bound."""
+    mock_session = MagicMock(spec=PyiCloudSession)
+    mock_response = MagicMock(spec=Response)
+    mock_response.json.return_value = {"Event": []}
+    mock_session.get.return_value = mock_response
+    service = _service_with_mocks(mock_session)
+
+    to_dt = datetime(2025, 4, 20)
+    with patch("pyicloud.services.calendar.get_localzone_name", return_value="UTC"):
+        service.refresh_client(from_dt=None, to_dt=to_dt)
+
+    # Inspect params passed to GET
+    _, kwargs = mock_session.get.call_args
+    params = kwargs["params"]
+    assert params["startDate"] == "2025-04-01"
+    assert params["endDate"] == "2025-04-30"