5
5
6
6
7
7
# Add the src directory to the path so we can import the modules
8
- sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' , ' src' ))
8
+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), ".." , " src" ))
9
9
10
10
from config import DashboardConfig
11
11
from owm .owm import WeatherUnits
@@ -17,26 +17,31 @@ class TestDashboardConfig:
17
17
def setup_method (self ):
18
18
"""Clear any existing global config before each test."""
19
19
import config
20
+
20
21
config ._current_config = None
21
22
22
- @patch .dict (os .environ , {
23
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
24
- 'OWM_API_KEY' : 'test_api_key' ,
25
- 'LAT' : '37.7749' ,
26
- 'LNG' : '-122.4194'
27
- }, clear = True )
23
+ @patch .dict (
24
+ os .environ ,
25
+ {
26
+ "ICS_URL" : "https://example.com/calendar.ics" ,
27
+ "OWM_API_KEY" : "test_api_key" ,
28
+ "LAT" : "37.7749" ,
29
+ "LNG" : "-122.4194" ,
30
+ },
31
+ clear = True ,
32
+ )
28
33
def test_init_with_minimal_required_env_vars (self ):
29
34
"""Test DashboardConfig initialization with only required environment variables."""
30
35
config = DashboardConfig ()
31
36
32
37
# Required variables
33
- assert config .ICS_URL == ' https://example.com/calendar.ics'
34
- assert config .OWM_API_KEY == ' test_api_key'
38
+ assert config .ICS_URL == " https://example.com/calendar.ics"
39
+ assert config .OWM_API_KEY == " test_api_key"
35
40
assert config .LAT == 37.7749
36
41
assert config .LNG == - 122.4194
37
42
38
43
# Default values
39
- assert config .DISPLAY_TZ == ' America/Los_Angeles'
44
+ assert config .DISPLAY_TZ == " America/Los_Angeles"
40
45
assert config .NUM_CAL_DAYS_TO_QUERY == 30
41
46
assert config .IMAGE_WIDTH == 1200
42
47
assert config .IMAGE_HEIGHT == 825
@@ -45,29 +50,33 @@ def test_init_with_minimal_required_env_vars(self):
45
50
assert config .SHOW_MOON_PHASE == False
46
51
assert config .SHOW_CALENDAR_NAME == False
47
52
48
- @patch .dict (os .environ , {
49
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
50
- 'OWM_API_KEY' : 'test_api_key' ,
51
- 'LAT' : '40.7128' ,
52
- 'LNG' : '-74.0060' ,
53
- 'DISPLAY_TZ' : 'America/New_York' ,
54
- 'NUM_CAL_DAYS_TO_QUERY' : '45' ,
55
- 'IMAGE_WIDTH' : '1600' ,
56
- 'IMAGE_HEIGHT' : '900' ,
57
- 'WEATHER_UNITS' : 'imperial' ,
58
- 'SHOW_ADDITIONAL_WEATHER' : 'true' ,
59
- 'SHOW_MOON_PHASE' : 'True' ,
60
- 'SHOW_CALENDAR_NAME' : 'TRUE'
61
- }, clear = True )
53
+ @patch .dict (
54
+ os .environ ,
55
+ {
56
+ "ICS_URL" : "https://example.com/calendar.ics" ,
57
+ "OWM_API_KEY" : "test_api_key" ,
58
+ "LAT" : "40.7128" ,
59
+ "LNG" : "-74.0060" ,
60
+ "DISPLAY_TZ" : "America/New_York" ,
61
+ "NUM_CAL_DAYS_TO_QUERY" : "45" ,
62
+ "IMAGE_WIDTH" : "1600" ,
63
+ "IMAGE_HEIGHT" : "900" ,
64
+ "WEATHER_UNITS" : "imperial" ,
65
+ "SHOW_ADDITIONAL_WEATHER" : "true" ,
66
+ "SHOW_MOON_PHASE" : "True" ,
67
+ "SHOW_CALENDAR_NAME" : "TRUE" ,
68
+ },
69
+ clear = True ,
70
+ )
62
71
def test_init_with_all_env_vars_set (self ):
63
72
"""Test DashboardConfig initialization with all environment variables set."""
64
73
config = DashboardConfig ()
65
74
66
- assert config .ICS_URL == ' https://example.com/calendar.ics'
67
- assert config .OWM_API_KEY == ' test_api_key'
75
+ assert config .ICS_URL == " https://example.com/calendar.ics"
76
+ assert config .OWM_API_KEY == " test_api_key"
68
77
assert config .LAT == 40.7128
69
78
assert config .LNG == - 74.0060
70
- assert config .DISPLAY_TZ == ' America/New_York'
79
+ assert config .DISPLAY_TZ == " America/New_York"
71
80
assert config .NUM_CAL_DAYS_TO_QUERY == 45
72
81
assert config .IMAGE_WIDTH == 1600
73
82
assert config .IMAGE_HEIGHT == 900
@@ -77,7 +86,7 @@ def test_init_with_all_env_vars_set(self):
77
86
assert config .SHOW_CALENDAR_NAME == True
78
87
79
88
@patch .dict (os .environ , {}, clear = True )
80
- @patch (' config.logger' )
89
+ @patch (" config.logger" )
81
90
def test_missing_ics_url_exits (self , mock_logger ):
82
91
"""Test that missing ICS_URL causes system exit."""
83
92
with pytest .raises (SystemExit ) as exc_info :
@@ -86,10 +95,8 @@ def test_missing_ics_url_exits(self, mock_logger):
86
95
assert exc_info .value .code == 1
87
96
mock_logger .error .assert_called_with ("ICS_URL needs to be set." )
88
97
89
- @patch .dict (os .environ , {
90
- 'ICS_URL' : 'https://example.com/calendar.ics'
91
- }, clear = True )
92
- @patch ('config.logger' )
98
+ @patch .dict (os .environ , {"ICS_URL" : "https://example.com/calendar.ics" }, clear = True )
99
+ @patch ("config.logger" )
93
100
def test_missing_owm_api_key_exits (self , mock_logger ):
94
101
"""Test that missing OWM_API_KEY causes system exit."""
95
102
with pytest .raises (SystemExit ) as exc_info :
@@ -98,11 +105,12 @@ def test_missing_owm_api_key_exits(self, mock_logger):
98
105
assert exc_info .value .code == 1
99
106
mock_logger .error .assert_called_with ("OWM_API_KEY needs to be set." )
100
107
101
- @patch .dict (os .environ , {
102
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
103
- 'OWM_API_KEY' : 'test_api_key'
104
- }, clear = True )
105
- @patch ('config.logger' )
108
+ @patch .dict (
109
+ os .environ ,
110
+ {"ICS_URL" : "https://example.com/calendar.ics" , "OWM_API_KEY" : "test_api_key" },
111
+ clear = True ,
112
+ )
113
+ @patch ("config.logger" )
106
114
def test_missing_lat_lng_exits (self , mock_logger ):
107
115
"""Test that missing LAT and LNG causes system exit."""
108
116
with pytest .raises (SystemExit ) as exc_info :
@@ -111,12 +119,16 @@ def test_missing_lat_lng_exits(self, mock_logger):
111
119
assert exc_info .value .code == 1
112
120
mock_logger .error .assert_called_with ("LAT and LNG need to be set." )
113
121
114
- @patch .dict (os .environ , {
115
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
116
- 'OWM_API_KEY' : 'test_api_key' ,
117
- 'LAT' : '37.7749'
118
- }, clear = True )
119
- @patch ('config.logger' )
122
+ @patch .dict (
123
+ os .environ ,
124
+ {
125
+ "ICS_URL" : "https://example.com/calendar.ics" ,
126
+ "OWM_API_KEY" : "test_api_key" ,
127
+ "LAT" : "37.7749" ,
128
+ },
129
+ clear = True ,
130
+ )
131
+ @patch ("config.logger" )
120
132
def test_missing_lng_only_exits (self , mock_logger ):
121
133
"""Test that missing only LNG (with LAT present) causes system exit."""
122
134
with pytest .raises (SystemExit ) as exc_info :
@@ -125,12 +137,16 @@ def test_missing_lng_only_exits(self, mock_logger):
125
137
assert exc_info .value .code == 1
126
138
mock_logger .error .assert_called_with ("LAT and LNG need to be set." )
127
139
128
- @patch .dict (os .environ , {
129
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
130
- 'OWM_API_KEY' : 'test_api_key' ,
131
- 'LNG' : '-122.4194'
132
- }, clear = True )
133
- @patch ('config.logger' )
140
+ @patch .dict (
141
+ os .environ ,
142
+ {
143
+ "ICS_URL" : "https://example.com/calendar.ics" ,
144
+ "OWM_API_KEY" : "test_api_key" ,
145
+ "LNG" : "-122.4194" ,
146
+ },
147
+ clear = True ,
148
+ )
149
+ @patch ("config.logger" )
134
150
def test_missing_lat_only_exits (self , mock_logger ):
135
151
"""Test that missing only LAT (with LNG present) causes system exit."""
136
152
with pytest .raises (SystemExit ) as exc_info :
@@ -139,15 +155,19 @@ def test_missing_lat_only_exits(self, mock_logger):
139
155
assert exc_info .value .code == 1
140
156
mock_logger .error .assert_called_with ("LAT and LNG need to be set." )
141
157
142
- @patch .dict (os .environ , {
143
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
144
- 'OWM_API_KEY' : 'test_api_key' ,
145
- 'LAT' : '37.7749' ,
146
- 'LNG' : '-122.4194' ,
147
- 'SHOW_ADDITIONAL_WEATHER' : 'false' ,
148
- 'SHOW_MOON_PHASE' : 'False' ,
149
- 'SHOW_CALENDAR_NAME' : 'NotTrue'
150
- }, clear = True )
158
+ @patch .dict (
159
+ os .environ ,
160
+ {
161
+ "ICS_URL" : "https://example.com/calendar.ics" ,
162
+ "OWM_API_KEY" : "test_api_key" ,
163
+ "LAT" : "37.7749" ,
164
+ "LNG" : "-122.4194" ,
165
+ "SHOW_ADDITIONAL_WEATHER" : "false" ,
166
+ "SHOW_MOON_PHASE" : "False" ,
167
+ "SHOW_CALENDAR_NAME" : "NotTrue" ,
168
+ },
169
+ clear = True ,
170
+ )
151
171
def test_boolean_env_vars_false_values (self ):
152
172
"""Test boolean environment variables with various false values."""
153
173
config = DashboardConfig ()
@@ -156,28 +176,36 @@ def test_boolean_env_vars_false_values(self):
156
176
assert config .SHOW_MOON_PHASE == False
157
177
assert config .SHOW_CALENDAR_NAME == False
158
178
159
- @patch .dict (os .environ , {
160
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
161
- 'OWM_API_KEY' : 'test_api_key' ,
162
- 'LAT' : '-90.0' ,
163
- 'LNG' : '180.0'
164
- }, clear = True )
179
+ @patch .dict (
180
+ os .environ ,
181
+ {
182
+ "ICS_URL" : "https://example.com/calendar.ics" ,
183
+ "OWM_API_KEY" : "test_api_key" ,
184
+ "LAT" : "-90.0" ,
185
+ "LNG" : "180.0" ,
186
+ },
187
+ clear = True ,
188
+ )
165
189
def test_extreme_lat_lng_values (self ):
166
190
"""Test with extreme but valid latitude and longitude values."""
167
191
config = DashboardConfig ()
168
192
169
193
assert config .LAT == - 90.0
170
194
assert config .LNG == 180.0
171
195
172
- @patch .dict (os .environ , {
173
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
174
- 'OWM_API_KEY' : 'test_api_key' ,
175
- 'LAT' : '37.7749' ,
176
- 'LNG' : '-122.4194' ,
177
- 'NUM_CAL_DAYS_TO_QUERY' : '0' ,
178
- 'IMAGE_WIDTH' : '1' ,
179
- 'IMAGE_HEIGHT' : '1'
180
- }, clear = True )
196
+ @patch .dict (
197
+ os .environ ,
198
+ {
199
+ "ICS_URL" : "https://example.com/calendar.ics" ,
200
+ "OWM_API_KEY" : "test_api_key" ,
201
+ "LAT" : "37.7749" ,
202
+ "LNG" : "-122.4194" ,
203
+ "NUM_CAL_DAYS_TO_QUERY" : "0" ,
204
+ "IMAGE_WIDTH" : "1" ,
205
+ "IMAGE_HEIGHT" : "1" ,
206
+ },
207
+ clear = True ,
208
+ )
181
209
def test_integer_edge_cases (self ):
182
210
"""Test integer environment variables with edge case values."""
183
211
config = DashboardConfig ()
@@ -186,7 +214,7 @@ def test_integer_edge_cases(self):
186
214
assert config .IMAGE_WIDTH == 1
187
215
assert config .IMAGE_HEIGHT == 1
188
216
189
- @patch (' config.DashboardConfig.__init__' )
217
+ @patch (" config.DashboardConfig.__init__" )
190
218
def test_get_config_singleton_pattern (self , mock_init ):
191
219
"""Test that get_config() implements singleton pattern correctly."""
192
220
mock_init .return_value = None
@@ -202,52 +230,68 @@ def test_get_config_singleton_pattern(self, mock_init):
202
230
203
231
def test_get_config_returns_same_instance (self ):
204
232
"""Test that get_config returns the same instance on multiple calls."""
205
- with patch .dict (os .environ , {
206
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
207
- 'OWM_API_KEY' : 'test_api_key' ,
208
- 'LAT' : '37.7749' ,
209
- 'LNG' : '-122.4194'
210
- }, clear = True ):
233
+ with patch .dict (
234
+ os .environ ,
235
+ {
236
+ "ICS_URL" : "https://example.com/calendar.ics" ,
237
+ "OWM_API_KEY" : "test_api_key" ,
238
+ "LAT" : "37.7749" ,
239
+ "LNG" : "-122.4194" ,
240
+ },
241
+ clear = True ,
242
+ ):
211
243
config1 = DashboardConfig .get_config ()
212
244
config2 = DashboardConfig .get_config ()
213
245
214
246
assert config1 is config2
215
247
assert id (config1 ) == id (config2 )
216
248
217
- @patch .dict (os .environ , {
218
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
219
- 'OWM_API_KEY' : 'test_api_key' ,
220
- 'LAT' : '37.7749' ,
221
- 'LNG' : '-122.4194' ,
222
- 'WEATHER_UNITS' : 'metric'
223
- }, clear = True )
249
+ @patch .dict (
250
+ os .environ ,
251
+ {
252
+ "ICS_URL" : "https://example.com/calendar.ics" ,
253
+ "OWM_API_KEY" : "test_api_key" ,
254
+ "LAT" : "37.7749" ,
255
+ "LNG" : "-122.4194" ,
256
+ "WEATHER_UNITS" : "metric" ,
257
+ },
258
+ clear = True ,
259
+ )
224
260
def test_weather_units_metric (self ):
225
261
"""Test WEATHER_UNITS with metric value."""
226
262
config = DashboardConfig ()
227
263
assert config .WEATHER_UNITS == WeatherUnits .metric
228
264
229
- @patch .dict (os .environ , {
230
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
231
- 'OWM_API_KEY' : 'test_api_key' ,
232
- 'LAT' : '37.7749' ,
233
- 'LNG' : '-122.4194' ,
234
- 'WEATHER_UNITS' : 'imperial'
235
- }, clear = True )
265
+ @patch .dict (
266
+ os .environ ,
267
+ {
268
+ "ICS_URL" : "https://example.com/calendar.ics" ,
269
+ "OWM_API_KEY" : "test_api_key" ,
270
+ "LAT" : "37.7749" ,
271
+ "LNG" : "-122.4194" ,
272
+ "WEATHER_UNITS" : "imperial" ,
273
+ },
274
+ clear = True ,
275
+ )
236
276
def test_weather_units_imperial (self ):
237
277
"""Test WEATHER_UNITS with imperial value."""
238
278
config = DashboardConfig ()
239
279
assert config .WEATHER_UNITS == WeatherUnits .imperial
240
280
241
- @patch .dict (os .environ , {
242
- 'ICS_URL' : 'https://example.com/calendar.ics' ,
243
- 'OWM_API_KEY' : 'test_api_key' ,
244
- 'LAT' : '37.7749' ,
245
- 'LNG' : '-122.4194' ,
246
- 'NUM_CAL_DAYS_TO_QUERY' : ''
247
- }, clear = True )
281
+ @patch .dict (
282
+ os .environ ,
283
+ {
284
+ "ICS_URL" : "https://example.com/calendar.ics" ,
285
+ "OWM_API_KEY" : "test_api_key" ,
286
+ "LAT" : "37.7749" ,
287
+ "LNG" : "-122.4194" ,
288
+ "NUM_CAL_DAYS_TO_QUERY" : "" ,
289
+ },
290
+ clear = True ,
291
+ )
248
292
def test_empty_string_env_vars_cause_errors (self ):
249
293
"""Test that empty string environment variables cause errors for numeric fields."""
250
- os .environ [' NUM_CAL_DAYS_TO_QUERY' ] = ''
294
+ os .environ [" NUM_CAL_DAYS_TO_QUERY" ] = ""
251
295
252
296
with pytest .raises (ValueError ):
253
297
DashboardConfig ()
0 commit comments