@@ -301,15 +301,15 @@ def main():
301301def test_auto_ingest_intelligent_retrieval ():
302302 """
303303 Test _get_auto_ingest_context() for intelligent context retrieval.
304-
304+
305305 This function is the actual implementation that handles:
306306 - Database search with user_input
307307 - Fallback to recent memories
308308 - Recursion guard protection
309309 - Search engine integration
310310 - Error handling
311311 """
312- from unittest .mock import patch , MagicMock
312+ from unittest .mock import MagicMock , patch
313313
314314 print ("\n " + "=" * 60 )
315315 print ("Testing _get_auto_ingest_context() Intelligent Retrieval" )
@@ -344,16 +344,21 @@ def test_auto_ingest_intelligent_retrieval():
344344 result = memori ._get_auto_ingest_context ("What are my preferences?" )
345345
346346 # Verify results returned with metadata
347- if len (result ) == 3 and result [0 ].get ("retrieval_method" ) == "direct_database_search" :
347+ if (
348+ len (result ) == 3
349+ and result [0 ].get ("retrieval_method" ) == "direct_database_search"
350+ ):
348351 print ("[OK] Test 1 passed: Direct search returns 3 results with metadata" )
349352 test_passed += 1
350353 else :
351- print (f"[FAIL] Test 1 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } " )
354+ print (
355+ f"[FAIL] Test 1 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } "
356+ )
352357
353358 # Test 2: Empty input returns empty list
354359 print ("\n [Test 2/8] Empty input returns empty list..." )
355360 result = memori ._get_auto_ingest_context ("" )
356-
361+
357362 if result == []:
358363 print ("[OK] Test 2 passed: Empty input returns []" )
359364 test_passed += 1
@@ -374,16 +379,21 @@ def test_auto_ingest_intelligent_retrieval():
374379 result = memori ._get_auto_ingest_context ("query with no results" )
375380
376381 # Check fallback was used and metadata added
377- if len (result ) == 2 and result [0 ].get ("retrieval_method" ) == "recent_memories_fallback" :
382+ if (
383+ len (result ) == 2
384+ and result [0 ].get ("retrieval_method" ) == "recent_memories_fallback"
385+ ):
378386 print ("[OK] Test 3 passed: Fallback to recent memories works" )
379387 test_passed += 1
380388 else :
381- print (f"[FAIL] Test 3 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } " )
389+ print (
390+ f"[FAIL] Test 3 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } "
391+ )
382392
383393 # Test 4: Recursion guard prevents infinite loops
384394 print ("\n [Test 4/8] Recursion guard prevents infinite loops..." )
385395 memori ._in_context_retrieval = True
386-
396+
387397 mock_results = [{"searchable_content" : "Safe result" , "category_primary" : "fact" }]
388398 with patch .object (
389399 memori .db_manager , "search_memories" , return_value = mock_results
@@ -395,8 +405,8 @@ def test_auto_ingest_intelligent_retrieval():
395405 print ("[OK] Test 4 passed: Recursion guard triggers direct search" )
396406 test_passed += 1
397407 else :
398- print (f "[FAIL] Test 4 failed: Expected direct search results" )
399-
408+ print ("[FAIL] Test 4 failed: Expected direct search results" )
409+
400410 # Reset recursion guard
401411 memori ._in_context_retrieval = False
402412
@@ -410,7 +420,9 @@ def test_auto_ingest_intelligent_retrieval():
410420 memori .search_engine = mock_search_engine
411421
412422 with patch .object (
413- memori .db_manager , "search_memories" , side_effect = [[], []] # Both direct and fallback empty
423+ memori .db_manager ,
424+ "search_memories" ,
425+ side_effect = [[], []], # Both direct and fallback empty
414426 ):
415427 result = memori ._get_auto_ingest_context ("advanced query" )
416428
@@ -419,25 +431,30 @@ def test_auto_ingest_intelligent_retrieval():
419431 print ("[OK] Test 5 passed: Search engine fallback works" )
420432 test_passed += 1
421433 else :
422- print (f"[FAIL] Test 5 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } " )
423-
434+ print (
435+ f"[FAIL] Test 5 failed: got { len (result )} results, metadata: { result [0 ].get ('retrieval_method' ) if result else 'N/A' } "
436+ )
437+
424438 # Reset search engine
425439 memori .search_engine = None
426440
427441 # Test 6: Error handling - graceful degradation
428442 print ("\n [Test 6/8] Error handling with graceful degradation..." )
429-
443+
430444 # First call fails, fallback succeeds
431445 mock_fallback = [{"searchable_content" : "Fallback" , "category_primary" : "fact" }]
432446 with patch .object (
433447 memori .db_manager ,
434448 "search_memories" ,
435- side_effect = [Exception ("DB error" ), mock_fallback ]
449+ side_effect = [Exception ("DB error" ), mock_fallback ],
436450 ):
437451 result = memori ._get_auto_ingest_context ("test error handling" )
438452
439453 # Should fallback to recent memories
440- if len (result ) == 1 and result [0 ].get ("retrieval_method" ) == "recent_memories_fallback" :
454+ if (
455+ len (result ) == 1
456+ and result [0 ].get ("retrieval_method" ) == "recent_memories_fallback"
457+ ):
441458 print ("[OK] Test 6 passed: Error handled, fallback used" )
442459 test_passed += 1
443460 else :
@@ -448,7 +465,7 @@ def test_auto_ingest_intelligent_retrieval():
448465 with patch .object (
449466 memori .db_manager ,
450467 "search_memories" ,
451- return_value = [{"searchable_content" : "Test" , "category_primary" : "fact" }]
468+ return_value = [{"searchable_content" : "Test" , "category_primary" : "fact" }],
452469 ) as mock_search :
453470 user_query = "find my API keys"
454471 result = memori ._get_auto_ingest_context (user_query )
@@ -468,7 +485,9 @@ def test_auto_ingest_intelligent_retrieval():
468485 print ("[OK] Test 7 passed: search_memories called with correct params" )
469486 test_passed += 1
470487 else :
471- print (f"[FAIL] Test 7 failed: query={ query_match } , ns={ namespace_match } , limit={ limit_match } " )
488+ print (
489+ f"[FAIL] Test 7 failed: query={ query_match } , ns={ namespace_match } , limit={ limit_match } "
490+ )
472491 else :
473492 print ("[FAIL] Test 7 failed: search_memories not called" )
474493
@@ -479,22 +498,19 @@ def test_auto_ingest_intelligent_retrieval():
479498 {"searchable_content" : "Item 2" , "category_primary" : "preference" },
480499 ]
481500
482- with patch .object (
483- memori .db_manager , "search_memories" , return_value = mock_results
484- ):
501+ with patch .object (memori .db_manager , "search_memories" , return_value = mock_results ):
485502 result = memori ._get_auto_ingest_context ("metadata test" )
486503
487504 # Check all results have metadata
488505 all_have_metadata = all (
489- r .get ("retrieval_method" ) and r .get ("retrieval_query" )
490- for r in result
506+ r .get ("retrieval_method" ) and r .get ("retrieval_query" ) for r in result
491507 )
492508
493509 if all_have_metadata and result [0 ]["retrieval_query" ] == "metadata test" :
494510 print ("[OK] Test 8 passed: All results have retrieval metadata" )
495511 test_passed += 1
496512 else :
497- print (f "[FAIL] Test 8 failed: metadata missing or incorrect" )
513+ print ("[FAIL] Test 8 failed: metadata missing or incorrect" )
498514
499515 # Summary
500516 print ("\n " + "=" * 60 )
0 commit comments