1+ # ------------------------------------------------------------------------
2+ # Copyright (c) Microsoft Corporation. All rights reserved.
3+ # Licensed under the MIT License. See License.txt in the project root for
4+ # license information.
5+ # -------------------------------------------------------------------------
6+ from featuremanagement ._time_window_filter ._models import RecurrencePatternType , RecurrenceRangeType , RecurrencePattern , RecurrenceRange , Recurrence
7+ from datetime import datetime
8+ import math
9+
10+ def test_recurrence_pattern_type ():
11+ assert RecurrencePatternType .from_str ("Daily" ) == RecurrencePatternType .DAILY
12+ assert RecurrencePatternType .from_str ("Weekly" ) == RecurrencePatternType .WEEKLY
13+ try :
14+ RecurrencePatternType .from_str ("Invalid" )
15+ except ValueError as e :
16+ assert str (e ) == "Invalid value: Invalid"
17+
18+ def test_recurrence_range_type ():
19+ assert RecurrenceRangeType .from_str ("NoEnd" ) == RecurrenceRangeType .NO_END
20+ assert RecurrenceRangeType .from_str ("EndDate" ) == RecurrenceRangeType .END_DATE
21+ assert RecurrenceRangeType .from_str ("Numbered" ) == RecurrenceRangeType .NUMBERED
22+ try :
23+ RecurrenceRangeType .from_str ("Invalid" )
24+ except ValueError as e :
25+ assert str (e ) == "Invalid value: Invalid"
26+
27+ def test_recurrence_pattern ():
28+ pattern = RecurrencePattern ({"Type" :"Daily" , "Interval" :1 , "DaysOfWeek" :["Monday" , "Tuesday" ]})
29+ assert pattern .type == RecurrencePatternType .DAILY
30+ assert pattern .interval == 1
31+ assert pattern .days_of_week == [0 , 1 ]
32+ assert pattern .first_day_of_week == 6
33+
34+ pattern = RecurrencePattern ({"Type" :"Daily" , "Interval" :1 , "DaysOfWeek" :["Monday" , "Tuesday" ], "FirstDayOfWeek" :"Monday" })
35+ assert pattern .type == RecurrencePatternType .DAILY
36+ assert pattern .interval == 1
37+ assert pattern .days_of_week == [0 , 1 ]
38+ assert pattern .first_day_of_week == 0
39+
40+ try :
41+ pattern = RecurrencePattern ({"Type" :"Daily" , "Interval" :1 , "DaysOfWeek" :["Monday" , "Tuesday" ], "FirstDayOfWeek" :"Thor's day" })
42+ except ValueError as e :
43+ assert str (e ) == "Invalid value for FirstDayOfWeek: Thor's day"
44+
45+ pattern = RecurrencePattern ({"Type" :"Weekly" , "Interval" :2 , "DaysOfWeek" :["Wednesday" ]})
46+ assert pattern .type == RecurrencePatternType .WEEKLY
47+ assert pattern .interval == 2
48+ assert pattern .days_of_week == [2 ]
49+ assert pattern .first_day_of_week == 6
50+
51+ try :
52+ pattern = RecurrencePattern ({"Type" :"Daily" , "Interval" :0 , "DaysOfWeek" :["Monday" , "Tuesday" ]})
53+ except ValueError as e :
54+ assert str (e ) == "The interval must be greater than 0."
55+
56+ try :
57+ pattern = RecurrencePattern ({"Type" :"Daily" , "Interval" :1 , "DaysOfWeek" :["Monday" , "Thor's day" ]})
58+ except ValueError as e :
59+ assert str (e ) == "Invalid value for DaysOfWeek: Thor's day"
60+
61+ def test_recurrence_range ():
62+ max_occurrences = math .pow (2 , 63 ) - 1
63+
64+ range = RecurrenceRange ({"Type" :"NoEnd" })
65+ assert range .type == RecurrenceRangeType .NO_END
66+ assert range .end_date is None
67+ assert range .num_of_occurrences == max_occurrences
68+
69+ range = RecurrenceRange ({"Type" :"EndDate" , "EndDate" :"Mon, 31 Mar 2025 00:00:00" })
70+ assert range .type == RecurrenceRangeType .END_DATE
71+ assert range .end_date == datetime (2025 , 3 , 31 , 0 , 0 , 0 )
72+ assert range .num_of_occurrences == max_occurrences
73+
74+ range = RecurrenceRange ({"Type" :"Numbered" , "NumberOfOccurrences" :10 })
75+ assert range .type == RecurrenceRangeType .NUMBERED
76+ assert range .end_date is None
77+ assert range .num_of_occurrences == 10
78+
79+ try :
80+ range = RecurrenceRange ({"Type" :"NoEnd" , "NumberOfOccurrences" :- 1 })
81+ except ValueError as e :
82+ assert str (e ) == "The number of occurrences must be greater than or equal to 0."
83+
84+ try :
85+ range = RecurrenceRange ({"Type" :"EndDate" , "EndDate" :"InvalidDate" })
86+ except ValueError as e :
87+ assert str (e ) == "Invalid value for EndDate: InvalidDate"
0 commit comments