@@ -228,6 +228,14 @@ def test_search_func_in_class(self, monkeypatch):
228
228
assert res .class_name == "TestClass"
229
229
assert res .func_name == "func"
230
230
assert res .code == expected_code
231
+
232
+ # case where class_name is not in self.class_func_index
233
+ results = sb ._search_func_in_class ("func" , "NonExistingClass" )
234
+ assert results == []
235
+
236
+ # case where class_name is in, but function_name is not in self.class_func_index[class_name]
237
+ results = sb ._search_func_in_class ("NonExistingFunc" , "TestClass" )
238
+ assert results == []
231
239
232
240
def test_search_func_in_all_classes (self , monkeypatch ):
233
241
monkeypatch .setattr (
@@ -315,6 +323,10 @@ def test_search_top_level_func(self, tmp_path, monkeypatch):
315
323
assert res .func_name == "top_func"
316
324
assert res .code == expected_code
317
325
326
+ # case where function_name is not in self.function_index
327
+ results = sb ._search_top_level_func ("NonExistingFunc" )
328
+ assert results == []
329
+
318
330
def test_search_func_in_code_base (self , tmp_path , monkeypatch ):
319
331
from app .search .search_backend import SearchBackend , SearchResult
320
332
@@ -436,6 +448,57 @@ def test_get_class_full_snippet_not_found(self):
436
448
assert result == expected_message
437
449
assert search_res == []
438
450
assert flag is False
451
+
452
+ def test_get_class_full_snippet_empty_class_index (self ):
453
+ # Create a SearchBackend instance with an empty list for class "A".
454
+ sb = SearchBackend (project_path = "dummy_project" )
455
+ sb .class_index = {"A" : []} # key exists, but no occurrences
456
+
457
+ # Call get_class_full_snippet for class "A".
458
+ result , search_res , flag = sb .get_class_full_snippet ("A" )
459
+
460
+ # Expect a message indicating that the class was not found, no search results, and flag False.
461
+ expected_message = "Could not find class A in the codebase."
462
+ assert result == expected_message
463
+ assert search_res == []
464
+ assert flag is False
465
+
466
+ def test_get_class_full_snippet_too_many_results (self , monkeypatch ):
467
+ sb = SearchBackend (project_path = "dummy_project" )
468
+
469
+ # Set up class_index with three occurrences of class "A"
470
+ sb .class_index = {
471
+ "A" : [
472
+ ("/absolute/path/fileA.py" , (1 , 10 )),
473
+ ("/absolute/path/fileB.py" , (11 , 20 )),
474
+ ("/absolute/path/fileC.py" , (21 , 30 )),
475
+ ]
476
+ }
477
+
478
+ # Monkey-patch get_code_snippets to return a dummy snippet.
479
+ monkeypatch .setattr (
480
+ "app.search.search_backend.search_utils.get_code_snippets" ,
481
+ lambda file_path , start , end , with_lineno = True : f"dummy code snippet from { file_path } lines { start } -{ end } "
482
+ )
483
+
484
+ # Monkey-patch SearchResult.to_tagged_str to return a predictable string.
485
+ monkeypatch .setattr (
486
+ SearchResult , "to_tagged_str" ,
487
+ lambda self , project_path : f"tagged snippet from { self .file_path } { self .start } -{ self .end } "
488
+ )
489
+
490
+ # Call get_class_full_snippet for class "A".
491
+ result , search_res , flag = sb .get_class_full_snippet ("A" )
492
+
493
+ # Verify that flag is True
494
+ assert flag is True
495
+
496
+ # Verify that the search results are truncated to 2 even though there are 3 entries.
497
+ assert len (search_res ) == 2
498
+
499
+ # Verify that the message for too many results is included.
500
+ assert "Too many results, showing full code for 2 of them:" in result
501
+
439
502
440
503
def test_get_class_full_snippet_found (self , monkeypatch ):
441
504
0 commit comments