@@ -56,10 +56,12 @@ def mock_faiss_repository() -> Generator[
5656 # Create two distinct mock instances for papers and models
5757 mock_instance_papers = MagicMock (spec = FaissRepository )
5858 mock_instance_papers .index = MagicMock ()
59+ mock_instance_papers .id_map = {} # Add id_map attribute
5960 mock_instance_papers .is_ready .return_value = True # Default to ready
6061
6162 mock_instance_models = MagicMock (spec = FaissRepository )
6263 mock_instance_models .index = MagicMock ()
64+ mock_instance_models .id_map = {} # Add id_map attribute
6365 mock_instance_models .is_ready .return_value = True # Default to ready
6466
6567 # Configure the class mock to return the correct instance based on call args
@@ -252,44 +254,25 @@ async def test_lifespan_faiss_not_ready(
252254 mock_faiss_repository
253255 )
254256
255- # Simulate Faiss Papers repo being not ready after init
257+ # Simulate Papers Faiss repo being not ready
256258 mock_repo_instance_papers .is_ready .return_value = False
257- mock_repo_instance_models .is_ready .return_value = True # Models repo is fine
259+ mock_repo_instance_models .is_ready .return_value = True # Models is ready
258260
259261 ctx = lifespan (mock_app , mock_settings )
260- # Lifespan should NOT raise an exception here, just log warning and set state to None
261- await ctx .__aenter__ ()
262-
263- # Assertions after startup
264- mock_pool_class .assert_called_once ()
265- mock_driver_func .assert_called_once ()
266- mock_repo_class .assert_any_call (
267- index_path = mock_settings .faiss_index_path ,
268- id_map_path = mock_settings .faiss_mapping_path ,
269- id_type = "int" ,
270- )
271- mock_repo_class .assert_any_call (
272- index_path = mock_settings .models_faiss_index_path ,
273- id_map_path = mock_settings .models_faiss_mapping_path ,
274- id_type = "str" ,
275- )
276- assert mock_repo_class .call_count == 2
277- mock_repo_instance_papers .is_ready .assert_called_once () # is_ready was checked
278- mock_repo_instance_models .is_ready .assert_called_once () # is_ready was checked
262+ with pytest .raises (RuntimeError , match = "Papers Faiss Repository is not ready after initialization." ):
263+ await ctx .__aenter__ ()
279264
280- assert mock_app .state .pg_pool == mock_pool_instance
281- assert mock_app .state .neo4j_driver == mock_driver_instance
282- assert (
283- mock_app .state .faiss_repo_papers is None
284- ) # State should be None as it wasn't ready
265+ # Check state after expected failure
266+ assert mock_app .state .pg_pool == mock_pool_instance # Should still be set
267+ assert mock_app .state .neo4j_driver == mock_driver_instance # Should still be set
268+ assert mock_app .state .faiss_repo_papers is None # Should be None or not set
285269 assert (
286- mock_app .state .faiss_repo_models == mock_repo_instance_models
287- ) # Models repo is fine
270+ mock_app .state .faiss_repo_models is None
271+ ) # Should also be None as init likely stopped
288272
289- # Check shutdown calls
290- await ctx .__aexit__ (None , None , None )
291- mock_pool_instance .close .assert_awaited_once ()
292- mock_driver_instance .close .assert_awaited_once ()
273+ # Check close calls are not made if startup failed mid-way
274+ mock_pool_instance .close .assert_not_awaited ()
275+ mock_driver_instance .close .assert_not_awaited ()
293276
294277
295278# Test initialization failure (e.g., Faiss Papers Repo __init__ fails)
@@ -320,27 +303,18 @@ def side_effect(*args: Any, **kwargs: Any) -> Any:
320303 mock_repo_class .side_effect = side_effect
321304
322305 ctx = lifespan (mock_app , mock_settings )
323- # Lifespan should NOT raise error, just log and set state to None
324- await ctx .__aenter__ ()
325-
326- # Assertions after startup
327- mock_pool_class .assert_called_once ()
328- mock_driver_func .assert_called_once ()
329- assert mock_repo_class .call_count == 2 # Both init attempts were made
330- mock_repo_instance_papers .is_ready .assert_not_called () # is_ready not called if init fails
331- mock_repo_instance_models .is_ready .assert_called_once () # is_ready called for models
306+ with pytest .raises (RuntimeError , match = "Papers Faiss Repository initialization failed" ):
307+ await ctx .__aenter__ ()
332308
309+ # Check state after expected failure
333310 assert mock_app .state .pg_pool == mock_pool_instance
334311 assert mock_app .state .neo4j_driver == mock_driver_instance
335- assert mock_app .state .faiss_repo_papers is None # State is None due to init failure
336- assert (
337- mock_app .state .faiss_repo_models == mock_repo_instance_models
338- ) # Models repo ok
312+ assert mock_app .state .faiss_repo_papers is None
313+ assert mock_app .state .faiss_repo_models is None
339314
340- # Check shutdown calls
341- await ctx .__aexit__ (None , None , None )
342- mock_pool_instance .close .assert_awaited_once ()
343- mock_driver_instance .close .assert_awaited_once ()
315+ # Check close calls
316+ mock_pool_instance .close .assert_not_awaited ()
317+ mock_driver_instance .close .assert_not_awaited ()
344318
345319
346320# Test initialization failure (e.g., Faiss Models Repo __init__ fails)
@@ -371,27 +345,19 @@ def side_effect(*args: Any, **kwargs: Any) -> Any:
371345 mock_repo_class .side_effect = side_effect
372346
373347 ctx = lifespan (mock_app , mock_settings )
374- # Lifespan should NOT raise error
375- await ctx .__aenter__ ()
376-
377- # Assertions after startup
378- mock_pool_class .assert_called_once ()
379- mock_driver_func .assert_called_once ()
380- assert mock_repo_class .call_count == 2
381- mock_repo_instance_papers .is_ready .assert_called_once () # Papers is_ready called
382- mock_repo_instance_models .is_ready .assert_not_called () # Models is_ready not called
348+ with pytest .raises (RuntimeError , match = "Models Faiss Repository initialization failed" ):
349+ await ctx .__aenter__ ()
383350
351+ # Check state after expected failure
384352 assert mock_app .state .pg_pool == mock_pool_instance
385353 assert mock_app .state .neo4j_driver == mock_driver_instance
386- assert (
387- mock_app .state .faiss_repo_papers == mock_repo_instance_papers
388- ) # Papers repo ok
389- assert mock_app .state .faiss_repo_models is None # State is None due to init failure
354+ # Paper repo might be initialized before model repo fails
355+ assert mock_app .state .faiss_repo_papers == mock_repo_instance_papers
356+ assert mock_app .state .faiss_repo_models is None
390357
391- # Check shutdown calls
392- await ctx .__aexit__ (None , None , None )
393- mock_pool_instance .close .assert_awaited_once ()
394- mock_driver_instance .close .assert_awaited_once ()
358+ # Check close calls
359+ mock_pool_instance .close .assert_not_awaited ()
360+ mock_driver_instance .close .assert_not_awaited ()
395361
396362
397363# Test Neo4j not configured scenario
0 commit comments