|
| 1 | +from datetime import timedelta, datetime |
1 | 2 | import pytest |
2 | | -from datetime import datetime, timedelta |
| 3 | +from recurrence_util import valid_daily_recurrence, valid_daily_end_date_recurrence, valid_no_end_range, START, END |
3 | 4 | from featuremanagement._time_window_filter._models import Recurrence |
4 | 5 | from featuremanagement._time_window_filter._recurrence_validator import validate_settings |
5 | 6 |
|
6 | | -def valid_daily_pattern(): |
7 | | - return { |
8 | | - "Type": "Daily", |
9 | | - "Interval": 1, |
10 | | - "DaysOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], |
11 | | - "FirstDayOfWeek": "Sunday", |
12 | | - } |
13 | | - |
14 | | -def valid_daily_recurrence(): |
15 | | - return Recurrence( |
16 | | - { |
17 | | - "Pattern": valid_daily_pattern(), |
18 | | - "Range": {"Type": "NoEnd", "EndDate": None, "NumberOfOccurrences": 10}, |
19 | | - } |
20 | | - ) |
21 | | - |
22 | | -def valid_daily_end_date_recurrence(): |
23 | | - return Recurrence( |
24 | | - { |
25 | | - "Pattern": valid_daily_pattern(), |
26 | | - "Range": {"Type": "EndDate", "EndDate": datetime.now() + timedelta(days=10), "NumberOfOccurrences": 10}, |
27 | | - } |
28 | | - ) |
29 | | - |
30 | | -def valid_weekly_recurrence(): |
31 | | - return Recurrence( |
32 | | - { |
33 | | - "Pattern": { |
34 | | - "Type": "Weekly", |
35 | | - "Interval": 1, |
36 | | - "DaysOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], |
37 | | - "FirstDayOfWeek": "Monday", |
38 | | - }, |
39 | | - "Range": {"Type": "NoEnd", "EndDate": None, "NumberOfOccurrences": 10}, |
40 | | - } |
41 | | - ) |
42 | | - |
43 | 7 |
|
44 | 8 | def test_validate_settings_valid_daily(): |
45 | | - start = datetime.now() |
46 | | - end = start + timedelta(days=1) |
47 | | - validate_settings(valid_daily_recurrence(), start, end) |
| 9 | + validate_settings(valid_daily_recurrence(), START, END) |
| 10 | + |
48 | 11 |
|
49 | 12 | def test_validate_settings_valid_daily_end_date(): |
50 | | - start = datetime.now() |
51 | | - end = start + timedelta(days=1) |
52 | | - validate_settings(valid_daily_end_date_recurrence(), start, end) |
| 13 | + validate_settings(valid_daily_end_date_recurrence(), START, END) |
| 14 | + |
| 15 | + |
| 16 | +def test_validate_settings_valid_weekly_one_day(): |
| 17 | + validate_settings( |
| 18 | + Recurrence( |
| 19 | + { |
| 20 | + "Pattern": { |
| 21 | + "Type": "Weekly", |
| 22 | + "Interval": 1, |
| 23 | + "DaysOfWeek": ["Monday"], |
| 24 | + "FirstDayOfWeek": "Monday", |
| 25 | + }, |
| 26 | + "Range": valid_no_end_range(), |
| 27 | + } |
| 28 | + ), |
| 29 | + START, |
| 30 | + END, |
| 31 | + ) |
53 | 32 |
|
54 | 33 |
|
55 | 34 | def test_validate_settings_valid_weekly(): |
56 | | - start = datetime.now() |
57 | | - end = start + timedelta(days=1) |
58 | | - validate_settings(valid_weekly_recurrence(), start, end) |
| 35 | + validate_settings( |
| 36 | + Recurrence( |
| 37 | + { |
| 38 | + "Pattern": { |
| 39 | + "Type": "Weekly", |
| 40 | + "Interval": 1, |
| 41 | + "DaysOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], |
| 42 | + "FirstDayOfWeek": "Monday", |
| 43 | + }, |
| 44 | + "Range": valid_no_end_range(), |
| 45 | + } |
| 46 | + ), |
| 47 | + START, |
| 48 | + END, |
| 49 | + ) |
| 50 | + |
59 | 51 |
|
60 | 52 | def test_validate_settings_duration_exceeds_ten_years(): |
61 | | - start = datetime.now() |
62 | | - end = start + timedelta(days=3651) |
| 53 | + end = START + timedelta(days=3651) |
63 | 54 | with pytest.raises(ValueError, match="Time window duration exceeds ten years: end"): |
64 | | - validate_settings(valid_daily_recurrence(), start, end) |
| 55 | + validate_settings(valid_daily_recurrence(), START, end) |
| 56 | + |
65 | 57 |
|
66 | 58 | def test_validate_settings_invalid_start_end(): |
67 | | - start = datetime.now() + timedelta(days=1) |
68 | | - end = datetime.now() |
| 59 | + start = START + timedelta(days=2) |
69 | 60 | with pytest.raises(ValueError, match="The filter start date Start needs to before the End date."): |
70 | | - validate_settings(valid_daily_recurrence(), start, end) |
| 61 | + validate_settings(valid_daily_recurrence(), start, END) |
| 62 | + |
71 | 63 |
|
72 | 64 | def test_validate_settings_end_date_in_past(): |
73 | | - start = datetime.now() |
74 | | - end = datetime.now() - timedelta(days=1) |
| 65 | + end = START - timedelta(days=1) |
75 | 66 | with pytest.raises(ValueError, match="The filter start date Start needs to before the End date."): |
76 | | - validate_settings(valid_daily_recurrence(), start, end) |
| 67 | + validate_settings(valid_daily_recurrence(), START, end) |
| 68 | + |
77 | 69 |
|
78 | 70 | def test_validate_settings_missing_recurrence(): |
79 | | - start = datetime.now() |
80 | | - end = start + timedelta(days=1) |
81 | 71 | with pytest.raises(ValueError, match="Recurrence is required"): |
82 | | - validate_settings(None, start, end) |
| 72 | + validate_settings(None, START, END) |
| 73 | + |
83 | 74 |
|
84 | 75 | def test_validate_settings_invalid_recurrence_pattern(): |
85 | 76 | with pytest.raises(ValueError, match="Invalid value: InvalidType"): |
86 | | - Recurrence({"Pattern": {"Type": "InvalidType"}, "Range": {"Type": "NoEnd"}}) |
87 | | - |
| 77 | + Recurrence({"Pattern": {"Type": "InvalidType"}, "Range": {"Type": "NoEnd"}}) |
| 78 | + |
| 79 | + |
| 80 | +def test_validate_settings_weekly_recurrence_invalid_start_day(): |
| 81 | + with pytest.raises(ValueError, match="Start day does not match any day of the week: Monday"): |
| 82 | + validate_settings( |
| 83 | + Recurrence( |
| 84 | + { |
| 85 | + "Pattern": { |
| 86 | + "Type": "Weekly", |
| 87 | + "Interval": 1, |
| 88 | + "DaysOfWeek": ["Tuesday", "Wednesday", "Thursday", "Friday"], |
| 89 | + "FirstDayOfWeek": "Monday", |
| 90 | + }, |
| 91 | + "Range": valid_no_end_range(), |
| 92 | + } |
| 93 | + ), |
| 94 | + START, |
| 95 | + END, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def test_validate_settings_period_too_long(): |
| 100 | + end = START + timedelta(days=7) |
| 101 | + with pytest.raises(ValueError, match="Time window duration is out of range:"): |
| 102 | + validate_settings(valid_daily_recurrence(), START, end) |
| 103 | + |
| 104 | + |
| 105 | +def test_validate_settings_no_days_of_week(): |
| 106 | + with pytest.raises(ValueError, match="Required parameter: Recurrence.Pattern.DaysOfWeek"): |
| 107 | + validate_settings( |
| 108 | + Recurrence( |
| 109 | + { |
| 110 | + "Pattern": { |
| 111 | + "Type": "Weekly", |
| 112 | + "Interval": 1, |
| 113 | + "FirstDayOfWeek": "Monday", |
| 114 | + }, |
| 115 | + "Range": valid_no_end_range(), |
| 116 | + } |
| 117 | + ), |
| 118 | + START, |
| 119 | + END, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def test_validate_settings_end_date_before_start(): |
| 124 | + with pytest.raises(ValueError, match="The Recurrence.Range.EndDate should be after the Start"): |
| 125 | + validate_settings(valid_daily_end_date_recurrence(), START + timedelta(days=11), END + timedelta(days=11)) |
| 126 | + |
| 127 | + |
| 128 | +def test_is_duration_compliant_with_days_of_week_false(): |
| 129 | + pattern = { |
| 130 | + "Type": "Weekly", |
| 131 | + "Interval": 1, |
| 132 | + "DaysOfWeek": ["Monday", "Wednesday"], |
| 133 | + "FirstDayOfWeek": "Monday", |
| 134 | + } |
88 | 135 |
|
| 136 | + start = datetime(2025, 4, 7, 9, 0, 0) # Monday |
| 137 | + end = datetime(2025, 4, 10, 9, 0, 0) # Wednesday (48 hours duration) |
| 138 | + with pytest.raises(ValueError, match="Recurrence.Pattern.DaysOfWeek"): |
| 139 | + validate_settings(Recurrence({"Pattern": pattern, "Range": valid_no_end_range()}), start, end) |
0 commit comments