@@ -287,9 +287,19 @@ def mock_get(*args, **kwargs):
287
287
288
288
def test_api_error (monkeypatch ):
289
289
"""Test handling of API errors with CSV fallback"""
290
+ # Create a mock CSV content
291
+ csv_content = '''mdb_source_id,data_type,entity_type,location.country_code,location.subdivision_name,location.municipality,provider,name,note,feed_contact_email,static_reference,urls.direct_download,urls.authentication_type,urls.authentication_info,urls.api_key_parameter_name,urls.latest,urls.license,location.bounding_box.minimum_latitude,location.bounding_box.maximum_latitude,location.bounding_box.minimum_longitude,location.bounding_box.maximum_longitude,location.bounding_box.extracted_on,status,features,redirect.id,redirect.comment
292
+ test-1,gtfs,,HU,Budapest,Budapest,Test Provider 1,,,,,http://test1.com/direct,,,,http://test1.com/latest,http://test1.com/license,,,,,,,,,
293
+ test-2,gtfs,,HU,Debrecen,Debrecen,Test Provider 2,,,,,http://test2.com/direct,,,,http://test2.com/latest,http://test2.com/license,,,,,,,,,'''
294
+
290
295
def mock_get (* args , ** kwargs ):
291
296
response = requests .Response ()
292
- response .status_code = 500
297
+
298
+ if "mobilitydatabase.org" in args [0 ]: # API calls
299
+ response .status_code = 500
300
+ elif "share.mobilitydata.org" in args [0 ]: # CSV download
301
+ response .status_code = 200
302
+ response ._content = csv_content .encode ()
293
303
return response
294
304
295
305
def mock_post (* args , ** kwargs ):
@@ -300,20 +310,31 @@ def mock_post(*args, **kwargs):
300
310
301
311
monkeypatch .setattr (requests , "get" , mock_get )
302
312
monkeypatch .setattr (requests , "post" , mock_post )
303
- api = MobilityAPI ()
313
+
314
+ # Use a fresh directory to ensure no cached CSV
315
+ test_dir = "test_api_error"
316
+ if Path (test_dir ).exists ():
317
+ shutil .rmtree (test_dir )
318
+
319
+ try :
320
+ api = MobilityAPI (data_dir = test_dir )
304
321
305
- # Test provider search with API error - should fall back to CSV
306
- providers = api .get_providers_by_country ("HU" )
307
- assert len (providers ) > 0 # Should get providers from CSV
308
- assert api ._use_csv is True # Should switch to CSV mode after API error
322
+ # Test provider search with API error - should fall back to CSV
323
+ providers = api .get_providers_by_country ("HU" )
324
+ assert len (providers ) > 0 # Should get providers from CSV
325
+ assert api ._use_csv is True # Should switch to CSV mode after API error
309
326
310
- # Create a new API instance to test with forced CSV mode
311
- api_csv = MobilityAPI (force_csv_mode = True )
312
- providers_csv = api_csv .get_providers_by_country ("HU" )
327
+ # Create a new API instance to test with forced CSV mode
328
+ api_csv = MobilityAPI (data_dir = test_dir , force_csv_mode = True )
329
+ providers_csv = api_csv .get_providers_by_country ("HU" )
330
+
331
+ # Results should be the same as the fallback
332
+ assert len (providers ) == len (providers_csv )
333
+ assert [p ['id' ] for p in providers ] == [p ['id' ] for p in providers_csv ]
313
334
314
- # Results should be the same as the fallback
315
- assert len ( providers ) == len ( providers_csv )
316
- assert [ p [ 'id' ] for p in providers ] == [ p [ 'id' ] for p in providers_csv ]
335
+ finally :
336
+ if Path ( test_dir ). exists ():
337
+ shutil . rmtree ( test_dir )
317
338
318
339
def test_missing_feed_info ():
319
340
"""Test handling of missing feed_info.txt"""
0 commit comments