@@ -160,6 +160,54 @@ def test_invalid_start_date(self):
160160 with self .assertRaises (ValueError ):
161161 api .search (start_date = 123 )
162162
163+ @patch ("currentsapi.client.requests.get" )
164+ def test_api_error_uses_msg_key (self , mock_get ):
165+ mock_get .return_value = Mock (
166+ status_code = 401 ,
167+ json = Mock (return_value = {"status" : "401" , "msg" : "Invalid token" }),
168+ )
169+ api = CurrentsAPI ("key" )
170+ with self .assertRaises (CurrentsAPIError ) as ctx :
171+ api .latest_news ()
172+ self .assertEqual (ctx .exception .status , 401 )
173+ self .assertIsNone (ctx .exception .code )
174+ self .assertEqual (ctx .exception .message , "Invalid token" )
175+ self .assertEqual (str (ctx .exception ), "Invalid token" )
176+
177+ @patch ("currentsapi.client.requests.get" )
178+ def test_api_error_prefers_message_over_msg (self , mock_get ):
179+ mock_get .return_value = Mock (
180+ status_code = 400 ,
181+ json = Mock (
182+ return_value = {
183+ "status" : "400" ,
184+ "msg" : "Bad request" ,
185+ "code" : "INVALID_QUERY" ,
186+ "message" : "Invalid parameters" ,
187+ }
188+ ),
189+ )
190+ api = CurrentsAPI ("key" )
191+ with self .assertRaises (CurrentsAPIError ) as ctx :
192+ api .search (keywords = "x" , category = "nope" )
193+ self .assertEqual (ctx .exception .status , 400 )
194+ self .assertEqual (ctx .exception .code , "INVALID_QUERY" )
195+ self .assertEqual (ctx .exception .message , "Invalid parameters" )
196+ self .assertEqual (str (ctx .exception ), "Invalid parameters" )
197+
198+ @patch ("currentsapi.client.requests.get" )
199+ def test_tz_aware_datetime_converted_to_utc (self , mock_get ):
200+ mock_get .return_value = Mock (status_code = 200 , json = Mock (return_value = {"status" : "ok" }))
201+ api = CurrentsAPI ("key" )
202+ tz = datetime .timezone (datetime .timedelta (hours = 8 ))
203+ api .search (start_date = datetime .datetime (2024 , 6 , 1 , 12 , 0 , tzinfo = tz ))
204+ kwargs = mock_get .call_args .kwargs
205+ self .assertEqual (kwargs ["params" ]["start_date" ], "2024-06-01T04:00:00Z" )
206+
207+ def test_impossible_date_string_raises_valueerror (self ):
208+ api = CurrentsAPI ("key" )
209+ with self .assertRaises (ValueError ):
210+ api .search (start_date = "2026-13-45" )
163211
164212if __name__ == "__main__" :
165213 unittest .main ()
0 commit comments