1+ from datetime import datetime , timedelta
2+ import pytest
3+
4+ from custom_components .target_timeframes .entities import is_target_timeframe_complete_in_period
5+
6+ @pytest .mark .asyncio
7+ async def test_when_both_lists_are_none_then_false_is_returned ():
8+ # Arrange
9+ current_date = datetime .now ()
10+ applicable_time_periods = None
11+ target_timeframes = None
12+
13+ # Act
14+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
15+
16+ # Assert
17+ assert result is False
18+
19+ @pytest .mark .asyncio
20+ async def test_when_applicable_time_periods_is_empty_then_false_is_returned ():
21+ # Arrange
22+ current_date = datetime .now ()
23+ applicable_time_periods = []
24+ target_timeframes = [{"start" : datetime .now (), "end" : datetime .now () + timedelta (hours = 1 )}]
25+
26+ # Act
27+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
28+
29+ # Assert
30+ assert result is False
31+
32+ @pytest .mark .asyncio
33+ async def test_when_target_timeframes_is_empty_then_false_is_returned ():
34+ # Arrange
35+ current_date = datetime .now ()
36+ applicable_time_periods = [{"start" : datetime .now (), "end" : datetime .now () + timedelta (hours = 1 )}]
37+ target_timeframes = []
38+
39+ # Act
40+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
41+
42+ # Assert
43+ assert result is False
44+
45+ @pytest .mark .asyncio
46+ async def test_when_target_timeframe_is_within_applicable_periods_and_complete_then_true_is_returned ():
47+ # Arrange
48+ current_date = datetime .now ()
49+ start_time = current_date - timedelta (hours = 2 )
50+ end_time = current_date - timedelta (minutes = 30 )
51+
52+ applicable_time_periods = [
53+ {"start" : start_time - timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 30 )},
54+ {"start" : start_time + timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 60 )},
55+ {"start" : start_time + timedelta (minutes = 60 ), "end" : end_time + timedelta (minutes = 30 )}
56+ ]
57+
58+ target_timeframes = [
59+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
60+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time }
61+ ]
62+
63+ # Act
64+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
65+
66+ # Assert
67+ assert result is True
68+
69+ @pytest .mark .asyncio
70+ async def test_when_target_timeframe_start_before_applicable_periods_then_false_is_returned ():
71+ # Arrange
72+ current_date = datetime .now ()
73+ start_time = current_date - timedelta (hours = 2 )
74+ end_time = current_date - timedelta (minutes = 30 )
75+
76+ applicable_time_periods = [
77+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
78+ {"start" : start_time + timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 60 )},
79+ {"start" : start_time + timedelta (minutes = 60 ), "end" : end_time }
80+ ]
81+
82+ target_timeframes = [
83+ {"start" : start_time - timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 30 )},
84+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time }
85+ ]
86+
87+ # Act
88+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
89+
90+ # Assert
91+ assert result is False
92+
93+ @pytest .mark .asyncio
94+ async def test_when_target_timeframe_end_after_applicable_periods_then_false_is_returned ():
95+ # Arrange
96+ current_date = datetime .now ()
97+ start_time = current_date - timedelta (hours = 2 )
98+ end_time = current_date - timedelta (minutes = 30 )
99+
100+ applicable_time_periods = [
101+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
102+ {"start" : start_time + timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 60 )},
103+ {"start" : start_time + timedelta (minutes = 60 ), "end" : end_time }
104+ ]
105+
106+ target_timeframes = [
107+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
108+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time + timedelta (minutes = 30 )}
109+ ]
110+
111+ # Act
112+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
113+
114+ # Assert
115+ assert result is False
116+
117+ @pytest .mark .asyncio
118+ async def test_when_target_timeframe_end_not_in_past_then_false_is_returned ():
119+ # Arrange
120+ current_date = datetime .now ()
121+ start_time = current_date - timedelta (hours = 2 )
122+ end_time = current_date + timedelta (minutes = 30 )
123+
124+ applicable_time_periods = [
125+ {"start" : start_time - timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 30 )},
126+ {"start" : start_time + timedelta (minutes = 30 ), "end" : start_time + timedelta (minutes = 60 )},
127+ {"start" : start_time + timedelta (minutes = 60 ), "end" : end_time + timedelta (minutes = 30 )}
128+ ]
129+
130+ target_timeframes = [
131+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
132+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time }
133+ ]
134+
135+ # Act
136+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
137+
138+ # Assert
139+ assert result is False
140+
141+ @pytest .mark .asyncio
142+ async def test_when_target_timeframe_is_exactly_equal_to_applicable_periods_and_complete_then_true_is_returned ():
143+ # Arrange
144+ current_date = datetime .now ()
145+ start_time = current_date - timedelta (hours = 2 )
146+ end_time = current_date - timedelta (minutes = 30 )
147+
148+ applicable_time_periods = [
149+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
150+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time }
151+ ]
152+
153+ target_timeframes = [
154+ {"start" : start_time , "end" : start_time + timedelta (minutes = 30 )},
155+ {"start" : start_time + timedelta (minutes = 30 ), "end" : end_time }
156+ ]
157+
158+ # Act
159+ result = is_target_timeframe_complete_in_period (current_date , applicable_time_periods , target_timeframes )
160+
161+ # Assert
162+ assert result is True
0 commit comments