Skip to content

Commit 8233ee5

Browse files
committed
Adding more tests
1 parent ab57af1 commit 8233ee5

File tree

2 files changed

+274
-2
lines changed

2 files changed

+274
-2
lines changed

featuremanagement/_time_window_filter/_recurrence_evaluator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ def _get_previous_occurrence(recurrence: Recurrence, start: datetime, now: datet
4949
occurrence_info = _get_daily_previous_occurrence(recurrence, start, now)
5050
elif pattern_type == RecurrencePatternType.WEEKLY:
5151
occurrence_info = _get_weekly_previous_occurrence(recurrence, start, now)
52-
else:
53-
raise ValueError(f"Invalid recurrence pattern type: {pattern_type}")
5452

5553
recurrence_range = recurrence.range
5654
range_type = recurrence_range.type
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
import pytest
2+
from datetime import datetime, timedelta
3+
from featuremanagement._time_window_filter._recurrence_evaluator import is_match
4+
from featuremanagement._time_window_filter._models import TimeWindowFilterSettings, Recurrence
5+
6+
7+
def test_is_match_within_time_window():
8+
start = datetime(2025, 4, 7, 9, 0, 0)
9+
end = datetime(2025, 4, 7, 17, 0, 0)
10+
now = datetime(2025, 4, 7, 10, 0, 0)
11+
12+
recurrence = Recurrence(
13+
{
14+
"Pattern": {"Type": "Daily", "Interval": 1},
15+
"Range": {"Type": "NoEnd"},
16+
}
17+
)
18+
19+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
20+
21+
assert is_match(settings, now) is True
22+
23+
24+
def test_is_match_outside_time_window():
25+
start = datetime(2025, 4, 7, 9, 0, 0)
26+
end = datetime(2025, 4, 7, 17, 0, 0)
27+
now = datetime(2025, 4, 7, 18, 0, 0)
28+
29+
recurrence = Recurrence(
30+
{
31+
"Pattern": {"Type": "Daily", "Interval": 1},
32+
"Range": {"Type": "NoEnd"},
33+
}
34+
)
35+
36+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
37+
38+
assert is_match(settings, now) is False
39+
40+
41+
def test_is_match_no_previous_occurrence():
42+
start = datetime(2025, 4, 7, 9, 0, 0)
43+
end = datetime(2025, 4, 7, 17, 0, 0)
44+
now = datetime(2025, 4, 6, 10, 0, 0) # Before the start time
45+
46+
recurrence = Recurrence(
47+
{
48+
"Pattern": {"Type": "Daily", "Interval": 1},
49+
"Range": {"Type": "NoEnd"},
50+
}
51+
)
52+
53+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
54+
55+
assert is_match(settings, now) is False
56+
57+
58+
def test_is_match_no_recurrence():
59+
start = datetime(2025, 4, 7, 9, 0, 0)
60+
end = datetime(2025, 4, 7, 17, 0, 0)
61+
now = datetime(2025, 4, 7, 10, 0, 0)
62+
63+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=None)
64+
65+
with pytest.raises(ValueError, match="Required parameter: Recurrence"):
66+
is_match(settings, now)
67+
68+
69+
def test_is_match_missing_start():
70+
end = datetime(2025, 4, 7, 17, 0, 0)
71+
now = datetime(2025, 4, 7, 10, 0, 0)
72+
73+
recurrence = Recurrence(
74+
{
75+
"Pattern": {"Type": "Daily", "Interval": 1},
76+
"Range": {"Type": "NoEnd"},
77+
}
78+
)
79+
80+
settings = TimeWindowFilterSettings(start=None, end=end, recurrence=recurrence)
81+
82+
with pytest.raises(ValueError, match="Required parameter: Start or End"):
83+
is_match(settings, now)
84+
85+
86+
def test_is_match_missing_end():
87+
start = datetime(2025, 4, 7, 9, 0, 0)
88+
now = datetime(2025, 4, 7, 10, 0, 0)
89+
90+
recurrence = Recurrence(
91+
{
92+
"Pattern": {"Type": "Daily", "Interval": 1},
93+
"Range": {"Type": "NoEnd"},
94+
}
95+
)
96+
97+
settings = TimeWindowFilterSettings(start=start, end=None, recurrence=recurrence)
98+
99+
with pytest.raises(ValueError, match="Required parameter: Start or End"):
100+
is_match(settings, now)
101+
102+
103+
def test_is_match_weekly_recurrence():
104+
start = datetime(2025, 4, 7, 9, 0, 0) # Monday
105+
end = datetime(2025, 4, 7, 17, 0, 0) # Monday
106+
now = datetime(2025, 4, 14, 10, 0, 0) # Next Monday
107+
108+
recurrence = Recurrence(
109+
{
110+
"Pattern": {"Type": "Weekly", "Interval": 1, "DaysOfWeek": ["Monday"], "FirstDayOfWeek": "Monday"},
111+
"Range": {"Type": "NoEnd"},
112+
}
113+
)
114+
115+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
116+
117+
assert is_match(settings, now) is True
118+
119+
120+
def test_is_match_end_date_has_passed():
121+
start = datetime(2025, 4, 7, 9, 0, 0)
122+
end = datetime(2025, 4, 7, 17, 0, 0)
123+
now = datetime(2025, 4, 9, 10, 0, 0) # After the end date
124+
125+
recurrence = Recurrence(
126+
{
127+
"Pattern": {"Type": "Daily", "Interval": 1},
128+
"Range": {"Type": "EndDate", "EndDate": "Tue, 8 Apr 2025 10:00:00"},
129+
}
130+
)
131+
132+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
133+
134+
assert is_match(settings, now) is False
135+
136+
137+
def test_is_match_numbered_recurrence():
138+
start = datetime(2025, 4, 7, 9, 0, 0)
139+
end = datetime(2025, 4, 7, 17, 0, 0)
140+
now = datetime(2025, 4, 8, 10, 0, 0)
141+
142+
recurrence = Recurrence(
143+
{
144+
"Pattern": {"Type": "Daily", "Interval": 1},
145+
"Range": {"Type": "Numbered", "NumberOfOccurrences": 2},
146+
}
147+
)
148+
149+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
150+
151+
assert is_match(settings, now) is True
152+
now = datetime(2025, 4, 15, 10, 0, 0)
153+
assert is_match(settings, now) is False
154+
155+
156+
def test_is_match_weekly_recurrence_with_occurrences_single_day():
157+
start = datetime(2025, 4, 7, 9, 0, 0) # Monday
158+
end = datetime(2025, 4, 7, 17, 0, 0) # Monday
159+
160+
recurrence = Recurrence(
161+
{
162+
"Pattern": {
163+
"Type": "Weekly",
164+
"Interval": 2,
165+
"DaysOfWeek": ["Monday"],
166+
"FirstDayOfWeek": "Monday",
167+
},
168+
"Range": {"Type": "Numbered", "NumberOfOccurrences": 2},
169+
}
170+
)
171+
172+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
173+
174+
# First occurrence should match
175+
assert is_match(settings, datetime(2025, 4, 7, 10, 0, 0)) is True
176+
177+
# Second week occurrence shouldn't match
178+
assert is_match(settings, datetime(2025, 4, 14, 10, 0, 0)) is False
179+
180+
# Third week occurrence should match
181+
assert is_match(settings, datetime(2025, 4, 21, 10, 0, 0)) is True
182+
183+
# Fourth week occurrence shouldn't match
184+
assert is_match(settings, datetime(2025, 4, 28, 10, 0, 0)) is False
185+
186+
# Fifth week occurrence shouldn't match, passed the range
187+
assert is_match(settings, datetime(2025, 5, 5, 10, 0, 0)) is False
188+
189+
def test_is_match_weekly_recurrence_with_occurrences_multi_day():
190+
start = datetime(2025, 4, 7, 9, 0, 0) # Monday
191+
end = datetime(2025, 4, 7, 17, 0, 0) # Monday
192+
193+
recurrence = Recurrence(
194+
{
195+
"Pattern": {
196+
"Type": "Weekly",
197+
"Interval": 2,
198+
"DaysOfWeek": ["Monday", "Tuesday"],
199+
"FirstDayOfWeek": "Monday",
200+
},
201+
"Range": {"Type": "Numbered", "NumberOfOccurrences": 4},
202+
}
203+
)
204+
205+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
206+
207+
# First occurrence should match
208+
assert is_match(settings, datetime(2025, 4, 7, 10, 0, 0)) is True
209+
assert is_match(settings, datetime(2025, 4, 8, 10, 0, 0)) is True
210+
211+
# Second week occurrence shouldn't match
212+
assert is_match(settings, datetime(2025, 4, 14, 10, 0, 0)) is False
213+
assert is_match(settings, datetime(2025, 4, 15, 10, 0, 0)) is False
214+
215+
assert is_match(settings, datetime(2025, 4, 7, 8, 0, 0)) is False
216+
# Third week occurrence should match
217+
assert is_match(settings, datetime(2025, 4, 21, 10, 0, 0)) is True
218+
assert is_match(settings, datetime(2025, 4, 22, 10, 0, 0)) is True
219+
220+
# Fourth week occurrence shouldn't match
221+
assert is_match(settings, datetime(2025, 4, 28, 10, 0, 0)) is False
222+
assert is_match(settings, datetime(2025, 4, 29, 10, 0, 0)) is False
223+
224+
# Fifth week occurrence shouldn't match
225+
assert is_match(settings, datetime(2025, 5, 5, 10, 0, 0)) is False
226+
assert is_match(settings, datetime(2025, 5, 6, 10, 0, 0)) is False
227+
228+
229+
def test_weekly_recurrence_start_after_min_offset():
230+
start = datetime(2025, 4, 9, 9, 0, 0) # Monday
231+
end = datetime(2025, 4, 9, 17, 0, 0) # Monday
232+
now = datetime(2025, 4, 12, 10, 0, 0) # Saturday
233+
234+
recurrence = Recurrence(
235+
{
236+
"Pattern": {
237+
"Type": "Weekly",
238+
"Interval": 1,
239+
"DaysOfWeek": ["Monday", "Wednesday"],
240+
"FirstDayOfWeek": "Monday",
241+
},
242+
"Range": {"Type": "NoEnd"},
243+
}
244+
)
245+
246+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
247+
248+
# Verify that the main method is_match correctly handles the scenario
249+
assert is_match(settings, now) is False
250+
assert is_match(settings, start) is True
251+
252+
253+
def test_weekly_recurrence_now_before_min_offset():
254+
start = datetime(2025, 4, 9, 9, 0, 0) # Monday
255+
end = datetime(2025, 4, 9, 17, 0, 0) # Monday
256+
now = datetime(2025, 4, 16, 8, 0, 0)
257+
258+
recurrence = Recurrence(
259+
{
260+
"Pattern": {
261+
"Type": "Weekly",
262+
"Interval": 1,
263+
"DaysOfWeek": ["Wednesday", "Friday"],
264+
"FirstDayOfWeek": "Monday",
265+
},
266+
"Range": {"Type": "NoEnd"},
267+
}
268+
)
269+
270+
settings = TimeWindowFilterSettings(start=start, end=end, recurrence=recurrence)
271+
272+
# Verify that the main method is_match correctly handles the scenario
273+
assert is_match(settings, now) is False
274+

0 commit comments

Comments
 (0)