1616
1717class TestDataFetcher (unittest .TestCase ):
1818 """Tests for the DataFetcher class"""
19-
19+
2020 def setUp (self ):
2121 """Set up test fixtures"""
2222 self .fetcher = DataFetcher (request_timeout = 5 )
23-
23+
2424 @patch ('requests.get' )
2525 def test_fetch_json_success (self , mock_get ):
2626 """Test successful JSON fetch"""
2727 # Mock response
2828 mock_response = Mock ()
2929 mock_response .json .return_value = {"key" : "value" }
3030 mock_get .return_value = mock_response
31-
31+
3232 # Test
3333 result = self .fetcher .fetch_json ("http://example.com/api" )
34-
34+
3535 # Verify
3636 mock_get .assert_called_once_with ("http://example.com/api" , timeout = 5 )
3737 mock_response .raise_for_status .assert_called_once ()
3838 self .assertEqual (result , {"key" : "value" })
39-
39+
4040 @patch ('requests.get' )
4141 def test_fetch_json_error (self , mock_get ):
4242 """Test JSON fetch with request error"""
4343 # Mock response
4444 mock_get .side_effect = requests .RequestException ("Connection error" )
45-
45+
4646 # Test
4747 with self .assertRaises (requests .RequestException ):
4848 self .fetcher .fetch_json ("http://example.com/api" )
49-
49+
5050 @patch ('requests.get' )
5151 @patch ('PIL.Image.open' )
5252 def test_fetch_image_success (self , mock_open , mock_get ):
5353 """Test successful image fetch"""
5454 # Mock response
5555 mock_response = Mock ()
56+ mock_response .content = b'fake_image_content'
5657 mock_get .return_value = mock_response
57-
58+
5859 # Mock image
5960 mock_image = MagicMock (spec = Image .Image )
6061 mock_image .convert .return_value = mock_image
6162 mock_open .return_value = mock_image
62-
63+
6364 # Test
6465 result = self .fetcher .fetch_image ("http://example.com/image.jpg" )
65-
66+
6667 # Verify
6768 mock_get .assert_called_once_with ("http://example.com/image.jpg" , timeout = 5 )
6869 mock_response .raise_for_status .assert_called_once ()
6970 mock_image .convert .assert_called_once_with ("RGBA" )
7071 self .assertEqual (result , mock_image )
71-
72+
7273 @patch ('requests.get' )
7374 def test_fetch_image_request_error (self , mock_get ):
7475 """Test image fetch with request error"""
7576 # Mock response
7677 mock_get .side_effect = requests .RequestException ("Connection error" )
77-
78+
7879 # Test
7980 with self .assertRaises (requests .RequestException ):
8081 self .fetcher .fetch_image ("http://example.com/image.jpg" )
81-
82+
8283 @patch ('requests.get' )
8384 @patch ('PIL.Image.open' )
8485 def test_fetch_image_processing_error (self , mock_open , mock_get ):
8586 """Test image fetch with image processing error"""
8687 # Mock response
8788 mock_response = Mock ()
89+ mock_response .content = b'invalid_image_content'
8890 mock_get .return_value = mock_response
89-
91+
9092 # Mock image open error
9193 mock_open .side_effect = UnidentifiedImageError ("invalid image" )
92-
94+
9395 # Test
9496 with self .assertRaises (IOError ):
9597 self .fetcher .fetch_image ("http://example.com/image.jpg" )
96-
98+
9799 @patch ('PIL.Image.open' )
98100 def test_load_local_image_success (self , mock_open ):
99101 """Test successful local image load"""
100102 # Mock image
101103 mock_image = MagicMock (spec = Image .Image )
102104 mock_image .convert .return_value = mock_image
103105 mock_open .return_value = mock_image
104-
106+
105107 # Test
106108 result = self .fetcher .load_local_image ("path/to/image.png" )
107-
109+
108110 # Verify
109111 mock_open .assert_called_once_with ("path/to/image.png" )
110112 mock_image .convert .assert_called_once_with ("RGBA" )
111113 self .assertEqual (result , mock_image )
112-
114+
113115 @patch ('PIL.Image.open' )
114116 def test_load_local_image_file_not_found (self , mock_open ):
115117 """Test local image load with file not found"""
116118 # Mock error
117119 mock_open .side_effect = FileNotFoundError ("File not found" )
118-
120+
119121 # Test
120122 with self .assertRaises (FileNotFoundError ):
121123 self .fetcher .load_local_image ("path/to/nonexistent.png" )
122-
124+
123125 @patch ('PIL.Image.open' )
124126 def test_load_local_image_processing_error (self , mock_open ):
125127 """Test local image load with processing error"""
126128 # Mock error
127129 mock_open .side_effect = IOError ("Invalid image" )
128-
130+
129131 # Test
130132 with self .assertRaises (IOError ):
131133 self .fetcher .load_local_image ("path/to/invalid.png" )
132-
134+
133135 @patch .object (DataFetcher , 'fetch_json' )
134136 def test_fetch_speakers (self , mock_fetch_json ):
135137 """Test fetching speakers"""
@@ -138,17 +140,17 @@ def test_fetch_speakers(self, mock_fetch_json):
138140 {"id" : "1" , "name" : "Speaker 1" },
139141 {"id" : "2" , "name" : "Speaker 2" }
140142 ]
141-
143+
142144 # Test
143145 result = self .fetcher .fetch_speakers ("http://example.com/speakers" )
144-
146+
145147 # Verify
146148 mock_fetch_json .assert_called_once_with ("http://example.com/speakers" )
147149 self .assertEqual (result , {
148150 "1" : {"id" : "1" , "name" : "Speaker 1" },
149151 "2" : {"id" : "2" , "name" : "Speaker 2" }
150152 })
151-
153+
152154 @patch .object (DataFetcher , 'fetch_json' )
153155 def test_fetch_sessions_by_track (self , mock_fetch_json ):
154156 """Test fetching sessions by track"""
@@ -168,10 +170,10 @@ def test_fetch_sessions_by_track(self, mock_fetch_json):
168170 ]
169171 }
170172 ]
171-
173+
172174 # Test
173175 result = self .fetcher .fetch_sessions_by_track ("http://example.com/sessions" )
174-
176+
175177 # Verify
176178 mock_fetch_json .assert_called_once_with ("http://example.com/sessions" )
177179 self .assertEqual (result , {
@@ -183,16 +185,16 @@ def test_fetch_sessions_by_track(self, mock_fetch_json):
183185 {"id" : "3" , "title" : "Session 3" }
184186 ]
185187 })
186-
188+
187189 @patch .object (DataFetcher , 'fetch_json' )
188190 def test_fetch_sessions_by_track_empty (self , mock_fetch_json ):
189191 """Test fetching sessions by track with empty data"""
190192 # Mock data
191193 mock_fetch_json .return_value = []
192-
194+
193195 # Test
194196 result = self .fetcher .fetch_sessions_by_track ("http://example.com/sessions" )
195-
197+
196198 # Verify
197199 mock_fetch_json .assert_called_once_with ("http://example.com/sessions" )
198200 self .assertEqual (result , {})
0 commit comments