Skip to content

Commit 00a78d2

Browse files
author
WangGLJoseph
committed
cover some missing branches in search_backend
1 parent d419e68 commit 00a78d2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/app/search/test_search_backend.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ def test_search_func_in_class(self, monkeypatch):
228228
assert res.class_name == "TestClass"
229229
assert res.func_name == "func"
230230
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 == []
231239

232240
def test_search_func_in_all_classes(self, monkeypatch):
233241
monkeypatch.setattr(
@@ -315,6 +323,10 @@ def test_search_top_level_func(self, tmp_path, monkeypatch):
315323
assert res.func_name == "top_func"
316324
assert res.code == expected_code
317325

326+
# case where function_name is not in self.function_index
327+
results = sb._search_top_level_func("NonExistingFunc")
328+
assert results == []
329+
318330
def test_search_func_in_code_base(self, tmp_path, monkeypatch):
319331
from app.search.search_backend import SearchBackend, SearchResult
320332

@@ -436,6 +448,57 @@ def test_get_class_full_snippet_not_found(self):
436448
assert result == expected_message
437449
assert search_res == []
438450
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+
439502

440503
def test_get_class_full_snippet_found(self, monkeypatch):
441504

0 commit comments

Comments
 (0)