Skip to content

Commit c76178a

Browse files
committed
added tests fixed max int
1 parent 706588a commit c76178a

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

featuremanagement/_time_window_filter/_models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# -------------------------------------------------------------------------
6-
import math
76
from enum import Enum
87
from typing import Dict, Any, Optional, List
98
from datetime import datetime
@@ -80,10 +79,12 @@ def __init__(self, pattern_data: Dict[str, Any]):
8079
days_of_week_str = pattern_data.get("DaysOfWeek", [])
8180

8281
# Days of the week are represented as a list of integers from 0 to 6.
83-
self.days_of_week = []
82+
self.days_of_week: List[int] = []
8483
for day in days_of_week_str:
8584
if day not in self.days:
8685
raise ValueError(f"Invalid value for DaysOfWeek: {day}")
86+
if self.days.index(day) in self.days_of_week:
87+
raise ValueError(f"Duplicate day of the week found: {day}")
8788
self.days_of_week.append(self.days.index(day))
8889
if pattern_data.get("FirstDayOfWeek") and pattern_data.get("FirstDayOfWeek") not in self.days:
8990
raise ValueError(f"Invalid value for FirstDayOfWeek: {pattern_data.get('FirstDayOfWeek')}")
@@ -109,7 +110,7 @@ def __init__(self, range_data: Dict[str, Any]):
109110
except TypeError as e:
110111
# Python 3.9 and earlier throw TypeError if the string is not in RFC 2822 format.
111112
raise ValueError(f"Invalid value for EndDate: {end_date_str}") from e
112-
self.num_of_occurrences = range_data.get("NumberOfOccurrences", 2 ** 63 - 1)
113+
self.num_of_occurrences = range_data.get("NumberOfOccurrences", 2**63 - 1)
113114
if self.num_of_occurrences <= 0:
114115
raise ValueError("The number of occurrences must be greater than 0.")
115116

tests/time_window_filter/test_recurrence_validator.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,109 @@ def test_sort_days_of_week():
213213
days_of_week = [1] # Tuesday
214214
sorted_days = _sort_days_of_week(days_of_week, 0)
215215
assert sorted_days == [1]
216+
217+
218+
def test_validate_settings_invalid_days_of_week():
219+
with pytest.raises(ValueError, match="Invalid value for DaysOfWeek: Thor's Day"):
220+
validate_settings(
221+
Recurrence(
222+
{
223+
"Pattern": {
224+
"Type": "Weekly",
225+
"Interval": 1,
226+
"DaysOfWeek": ["Thor's Day"],
227+
"FirstDayOfWeek": "Monday",
228+
},
229+
"Range": valid_no_end_range(),
230+
}
231+
),
232+
START,
233+
END,
234+
)
235+
with pytest.raises(ValueError, match="Required parameter: Recurrence.Pattern.DaysOfWeek"):
236+
validate_settings(
237+
Recurrence(
238+
{
239+
"Pattern": {
240+
"Type": "Weekly",
241+
"Interval": 1,
242+
"DaysOfWeek": [],
243+
"FirstDayOfWeek": "Monday",
244+
},
245+
"Range": valid_no_end_range(),
246+
}
247+
),
248+
START,
249+
END,
250+
)
251+
252+
253+
def test_validate_settings_invalid_days_of_week_duplicate():
254+
with pytest.raises(ValueError, match="Duplicate day of the week found: Monday"):
255+
validate_settings(
256+
Recurrence(
257+
{
258+
"Pattern": {
259+
"Type": "Weekly",
260+
"Interval": 1,
261+
"DaysOfWeek": ["Monday", "Monday"],
262+
"FirstDayOfWeek": "Monday",
263+
},
264+
"Range": valid_no_end_range(),
265+
}
266+
),
267+
START,
268+
END,
269+
)
270+
271+
272+
def test_validate_settings_invalid_end_date_format():
273+
with pytest.raises(ValueError, match="Invalid value for EndDate: invalid-date-format"):
274+
validate_settings(
275+
Recurrence(
276+
{
277+
"Pattern": valid_daily_pattern(),
278+
"Range": {
279+
"Type": "EndDate",
280+
"EndDate": "invalid-date-format",
281+
"NumberOfOccurrences": 10,
282+
},
283+
}
284+
),
285+
START,
286+
END,
287+
)
288+
289+
290+
def test_validate_settings_boundary_condition_ten_years():
291+
end = START + timedelta(days=3650) # Exactly 10 years
292+
recurrence = Recurrence(
293+
{
294+
"Pattern": {
295+
"Type": "Daily",
296+
"Interval": 3651, # Interval greater than the time window length
297+
"DaysOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
298+
"FirstDayOfWeek": "Sunday",
299+
},
300+
"Range": valid_no_end_range(),
301+
}
302+
)
303+
validate_settings(recurrence, START, end)
304+
recurrence = Recurrence(
305+
{
306+
"Pattern": {
307+
"Type": "Daily",
308+
"Interval": 3652, # Interval greater than the time window length
309+
"DaysOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
310+
"FirstDayOfWeek": "Sunday",
311+
},
312+
"Range": valid_no_end_range(),
313+
}
314+
)
315+
with pytest.raises(ValueError, match="Time window duration exceeds ten years: end"):
316+
validate_settings(recurrence, START, START + timedelta(days=3652))
317+
318+
319+
def test_validate_settings_boundary_condition_interval():
320+
end = START + timedelta(days=1) # Exactly matches the interval duration for daily recurrence
321+
validate_settings(valid_daily_recurrence(), START, end)

tests/time_window_filter/test_time_window_filter_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_recurrence_pattern():
7373

7474

7575
def test_recurrence_range():
76-
max_occurrences = math.pow(2, 63) - 1
76+
max_occurrences = 2**63 - 1
7777

7878
range = RecurrenceRange({"Type": "NoEnd"})
7979
assert range.type == RecurrenceRangeType.NO_END

0 commit comments

Comments
 (0)