|
| 1 | +"""Tests for error handling in the query module's reranker integration.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from chromadb.api.models.AsyncCollection import AsyncCollection |
| 7 | + |
| 8 | +from vectorcode.cli_utils import Config, QueryInclude |
| 9 | +from vectorcode.subcommands.query import get_query_result_files |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_collection(): |
| 14 | + collection = AsyncMock(spec=AsyncCollection) |
| 15 | + collection.count.return_value = 10 |
| 16 | + collection.query.return_value = { |
| 17 | + "ids": [["id1", "id2", "id3"]], |
| 18 | + "distances": [[0.1, 0.2, 0.3]], |
| 19 | + "metadatas": [ |
| 20 | + [ |
| 21 | + {"path": "file1.py", "start": 1, "end": 1}, |
| 22 | + {"path": "file2.py", "start": 1, "end": 1}, |
| 23 | + {"path": "file3.py", "start": 1, "end": 1}, |
| 24 | + ], |
| 25 | + ], |
| 26 | + "documents": [ |
| 27 | + ["content1", "content2", "content3"], |
| 28 | + ], |
| 29 | + } |
| 30 | + return collection |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def mock_config(): |
| 35 | + return Config( |
| 36 | + query=["test query"], |
| 37 | + n_result=3, |
| 38 | + query_multiplier=2, |
| 39 | + chunk_size=100, |
| 40 | + overlap_ratio=0.2, |
| 41 | + project_root="/test/project", |
| 42 | + pipe=False, |
| 43 | + include=[QueryInclude.path, QueryInclude.document], |
| 44 | + query_exclude=[], |
| 45 | + reranker=None, |
| 46 | + reranker_params={}, |
| 47 | + use_absolute_path=False, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_get_query_result_files_registry_error(mock_collection, mock_config): |
| 53 | + """Test graceful handling of a reranker not found in registry.""" |
| 54 | + # Set a custom reranker to trigger the error path |
| 55 | + mock_config.reranker = "custom-reranker" |
| 56 | + |
| 57 | + # Mock stderr to capture error messages |
| 58 | + with patch("sys.stderr") as mock_stderr: |
| 59 | + # Mock the NaiveReranker for fallback |
| 60 | + with patch("vectorcode.subcommands.query.reranker.NaiveReranker") as mock_naive: |
| 61 | + mock_reranker_instance = MagicMock() |
| 62 | + mock_reranker_instance.rerank.return_value = ["file1.py", "file2.py"] |
| 63 | + mock_naive.return_value = mock_reranker_instance |
| 64 | + |
| 65 | + # This should fall back to NaiveReranker |
| 66 | + result = await get_query_result_files(mock_collection, mock_config) |
| 67 | + |
| 68 | + # Verify the error was logged |
| 69 | + assert mock_stderr.write.called |
| 70 | + assert "not found in registry" in "".join( |
| 71 | + [c[0][0] for c in mock_stderr.write.call_args_list] |
| 72 | + ) |
| 73 | + |
| 74 | + # Verify fallback to NaiveReranker happened |
| 75 | + assert mock_naive.called |
| 76 | + |
| 77 | + # Check the result contains the expected files |
| 78 | + assert result == ["file1.py", "file2.py"] |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_get_query_result_files_general_exception(mock_collection, mock_config): |
| 83 | + """Test handling of unexpected exceptions during reranker loading.""" |
| 84 | + # Set a custom reranker to trigger the import path |
| 85 | + mock_config.reranker = "buggy-reranker" |
| 86 | + |
| 87 | + # Create a patching context that raises an unexpected exception |
| 88 | + with patch("vectorcode.rerankers", new=MagicMock()) as mock_rerankers: |
| 89 | + # Configure the mock to raise RuntimeError when create_reranker is called |
| 90 | + mock_rerankers.create_reranker.side_effect = RuntimeError("Unexpected error") |
| 91 | + |
| 92 | + # Mock stderr to capture error messages |
| 93 | + with patch("sys.stderr") as mock_stderr: |
| 94 | + # Mock the NaiveReranker for fallback |
| 95 | + with patch( |
| 96 | + "vectorcode.subcommands.query.reranker.NaiveReranker" |
| 97 | + ) as mock_naive: |
| 98 | + mock_reranker_instance = MagicMock() |
| 99 | + mock_reranker_instance.rerank.return_value = ["file1.py", "file2.py"] |
| 100 | + mock_naive.return_value = mock_reranker_instance |
| 101 | + |
| 102 | + # This should catch the exception and fall back to NaiveReranker |
| 103 | + result = await get_query_result_files(mock_collection, mock_config) |
| 104 | + |
| 105 | + # Verify the error was logged |
| 106 | + assert mock_stderr.write.called |
| 107 | + |
| 108 | + # Verify fallback to NaiveReranker happened |
| 109 | + assert mock_naive.called |
| 110 | + |
| 111 | + # Check the result contains the expected files |
| 112 | + assert result == ["file1.py", "file2.py"] |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.asyncio |
| 116 | +async def test_get_query_result_files_cross_encoder_error(mock_collection, mock_config): |
| 117 | + """Test the CrossEncoder special case with error handling.""" |
| 118 | + # Set a cross encoder model to trigger that code path |
| 119 | + mock_config.reranker = "cross-encoder/model-name" |
| 120 | + |
| 121 | + # Mock the CrossEncoderReranker to raise an exception |
| 122 | + with patch( |
| 123 | + "vectorcode.subcommands.query.reranker.CrossEncoderReranker" |
| 124 | + ) as mock_cross_encoder: |
| 125 | + mock_cross_encoder.side_effect = ValueError("Model not found") |
| 126 | + |
| 127 | + # Mock stderr to capture error messages |
| 128 | + with patch("sys.stderr") as mock_stderr: |
| 129 | + # Mock the NaiveReranker for fallback |
| 130 | + with patch( |
| 131 | + "vectorcode.subcommands.query.reranker.NaiveReranker" |
| 132 | + ) as mock_naive: |
| 133 | + mock_reranker_instance = MagicMock() |
| 134 | + mock_reranker_instance.rerank.return_value = ["file1.py", "file2.py"] |
| 135 | + mock_naive.return_value = mock_reranker_instance |
| 136 | + |
| 137 | + # This should catch the exception and fall back |
| 138 | + result = await get_query_result_files(mock_collection, mock_config) |
| 139 | + |
| 140 | + # Verify the error was logged |
| 141 | + assert mock_stderr.write.called |
| 142 | + |
| 143 | + # Verify fallback to NaiveReranker happened |
| 144 | + assert mock_naive.called |
| 145 | + |
| 146 | + # Check the result contains the expected files |
| 147 | + assert result == ["file1.py", "file2.py"] |
0 commit comments