-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
279 lines (230 loc) · 10.8 KB
/
test_integration.py
File metadata and controls
279 lines (230 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import unittest
import tempfile
import os
from unittest.mock import patch, Mock
from io import StringIO
# Import the main function
from weather import main
class TestWeatherIntegration(unittest.TestCase):
"""Integration tests for the weather application."""
def setUp(self):
"""Set up test fixtures."""
self.temp_dir = tempfile.mkdtemp()
self.rss_file = os.path.join(self.temp_dir, 'test.rss')
self.image_dir = os.path.join(self.temp_dir, 'images')
os.makedirs(self.image_dir, exist_ok=True)
def tearDown(self):
"""Clean up test fixtures."""
import shutil
shutil.rmtree(self.temp_dir)
@patch('weather.upload_files_to_discord')
@patch('weather.upload_files_to_slack')
@patch('weather.generate_rss_feed')
@patch('weather.process_single_image')
@patch('weather.delete_storm_images')
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_no_storms_with_updated_static_image(self, mock_argv, mock_fetch_xml,
mock_delete_storm_images, mock_process_image,
mock_generate_rss, mock_upload_slack,
mock_upload_discord):
"""Test main function when no storms are expected and static image is updated."""
from weather import WeatherImage
# Mock command line arguments
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url'
][i]
mock_argv.__len__ = lambda s: 7
# Mock no storms scenario
mock_fetch_xml.return_value = (0, Mock()) # no_storms=0 (count)
# Mock updated static image
static_image = WeatherImage('two_atl_7d0', 'path.png', 'path.gif', 'url', True, 'static')
mock_process_image.return_value = static_image
main()
mock_fetch_xml.assert_called_once()
mock_delete_storm_images.assert_called_once_with(self.image_dir)
mock_process_image.assert_called_once()
mock_generate_rss.assert_called_once_with(static_image, self.rss_file)
mock_upload_slack.assert_called_once_with([static_image], 'slack_token', 'upload_channel')
mock_upload_discord.assert_called_once_with([static_image], 'discord_webhook_url')
@patch('weather.upload_files_to_discord')
@patch('weather.upload_files_to_slack')
@patch('weather.generate_rss_feed')
@patch('weather.process_single_image')
@patch('weather.delete_storm_images')
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_no_storms_with_unchanged_static_image(self, mock_argv, mock_fetch_xml,
mock_delete_storm_images, mock_process_image,
mock_generate_rss, mock_upload_slack,
mock_upload_discord):
"""Test main function when no storms are expected and static image is unchanged."""
from weather import WeatherImage
# Mock command line arguments
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url'
][i]
mock_argv.__len__ = lambda s: 7
# Mock no storms scenario
mock_fetch_xml.return_value = (0, Mock()) # no_storms=0 (count)
# Mock unchanged static image
static_image = WeatherImage('two_atl_7d0', 'path.png', 'path.gif', 'url', False, 'static')
mock_process_image.return_value = static_image
main()
mock_fetch_xml.assert_called_once()
mock_delete_storm_images.assert_called_once_with(self.image_dir)
mock_process_image.assert_called_once()
mock_generate_rss.assert_called_once_with(static_image, self.rss_file)
# Should not upload since image is unchanged
mock_upload_slack.assert_not_called()
mock_upload_discord.assert_not_called()
@patch('weather.upload_files_to_discord')
@patch('weather.upload_files_to_slack')
@patch('weather.generate_rss_feed')
@patch('weather.fetch_all_weather_images')
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_with_storms_and_new_images(self, mock_argv, mock_fetch_xml,
mock_fetch_images, mock_generate_rss,
mock_upload_slack, mock_upload_discord):
"""Test main function with storms and new images."""
from weather import WeatherImage
# Mock command line arguments
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url'
][i]
mock_argv.__len__ = lambda s: 7
# Mock storms scenario
mock_soup = Mock()
mock_fetch_xml.return_value = (1, mock_soup) # storms=1 (count)
# Mock images
static_image = WeatherImage('static', 'path.png', 'path.gif', 'url', True, 'static')
new_image = WeatherImage('new', 'new.png', 'new.gif', 'url', True, 'cone')
cached_image = WeatherImage('cached', 'cached.png', 'cached.gif', 'url', False, 'cached')
mock_fetch_images.return_value = [static_image, new_image, cached_image]
main()
mock_fetch_xml.assert_called_once()
mock_fetch_images.assert_called_once()
mock_generate_rss.assert_called_once_with(static_image, self.rss_file)
mock_upload_slack.assert_called_once()
mock_upload_discord.assert_called_once()
# Verify only new images are uploaded
uploaded_images = mock_upload_slack.call_args[0][0]
self.assertEqual(len(uploaded_images), 2) # static_image and new_image
self.assertTrue(all(img.is_new for img in uploaded_images))
@patch('weather.fetch_all_weather_images')
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_with_storms_no_new_images(self, mock_argv, mock_fetch_xml, mock_fetch_images):
"""Test main function with storms but no new images."""
from weather import WeatherImage
# Mock command line arguments
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url'
][i]
mock_argv.__len__ = lambda s: 7
# Mock storms scenario
mock_soup = Mock()
mock_fetch_xml.return_value = (1, mock_soup) # storms=1 (count)
# Mock only cached/unchanged images
cached_image = WeatherImage('cached', 'cached.png', 'cached.gif', 'url', False, 'static')
mock_fetch_images.return_value = [cached_image]
# Capture stdout
captured_output = StringIO()
with patch('sys.stdout', captured_output):
main()
mock_fetch_xml.assert_called_once()
mock_fetch_images.assert_called_once()
@patch('weather.upload_files_to_discord')
@patch('weather.upload_files_to_slack')
@patch('weather.generate_rss_feed')
@patch('weather.process_single_image')
@patch('weather.delete_storm_images')
@patch('weather.setup_logging')
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_with_log_file_argument(self, mock_argv, mock_fetch_xml, mock_setup_logging,
mock_delete_storm_images, mock_process_image,
mock_generate_rss, mock_upload_slack, mock_upload_discord):
"""Test main function with log file argument."""
from weather import WeatherImage
log_file = os.path.join(self.temp_dir, 'test.log')
# Mock command line arguments with log file
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url',
'--log-file',
log_file
][i]
mock_argv.__len__ = lambda s: 9
mock_fetch_xml.return_value = (0, Mock()) # no_storms=0 (count)
# Mock unchanged static image to avoid upload calls
static_image = WeatherImage('two_atl_7d0', 'path.png', 'path.gif', 'url', False, 'static')
mock_process_image.return_value = static_image
main()
mock_setup_logging.assert_called_once_with(log_file)
mock_delete_storm_images.assert_called_once_with(self.image_dir)
mock_process_image.assert_called_once()
mock_generate_rss.assert_called_once_with(static_image, self.rss_file)
# Should not upload since image is unchanged
mock_upload_slack.assert_not_called()
mock_upload_discord.assert_not_called()
@patch('weather.fetch_xml_feed')
@patch('sys.argv')
def test_main_with_custom_threshold(self, mock_argv, mock_fetch_xml):
"""Test main function with custom threshold argument."""
# Mock command line arguments with custom threshold
mock_argv.__getitem__ = lambda s, i: [
'weather.py',
self.rss_file,
self.image_dir,
'slack_webhook_url',
'slack_token',
'upload_channel',
'discord_webhook_url',
'--threshold',
'0.005'
][i]
mock_argv.__len__ = lambda s: 9
mock_fetch_xml.return_value = (0, Mock()) # no_storms=0 (count)
with patch('weather.delete_images'), \
patch('weather.fetch_all_weather_images') as mock_fetch_images:
mock_fetch_xml.return_value = (1, Mock()) # Change to have storms (count=1)
mock_fetch_images.return_value = []
main()
# Verify custom threshold was passed to fetch_all_weather_images
mock_fetch_images.assert_called_once()
args, kwargs = mock_fetch_images.call_args
if len(args) >= 3:
self.assertEqual(args[2], 0.005)
if __name__ == '__main__':
unittest.main()