|
9 | 9 | import pytest |
10 | 10 | from chromadb.api import AsyncClientAPI |
11 | 11 | from chromadb.api.models.AsyncCollection import AsyncCollection |
| 12 | +from chromadb.utils import embedding_functions |
12 | 13 |
|
13 | 14 | from vectorcode.cli_utils import Config |
14 | 15 | from vectorcode.common import ( |
@@ -68,6 +69,34 @@ def test_get_embedding_function(): |
68 | 69 | assert "SentenceTransformerEmbeddingFunction" in str(type(embedding_function)) |
69 | 70 |
|
70 | 71 |
|
| 72 | +def test_get_embedding_function_init_exception(): |
| 73 | + # Test when the embedding function exists but raises an error during initialization |
| 74 | + config = Config( |
| 75 | + embedding_function="SentenceTransformerEmbeddingFunction", |
| 76 | + embedding_params={"model_name": "non_existent_model_should_cause_error"}, |
| 77 | + ) |
| 78 | + |
| 79 | + # Mock SentenceTransformerEmbeddingFunction.__init__ to raise a generic exception |
| 80 | + with patch.object( |
| 81 | + embedding_functions, "SentenceTransformerEmbeddingFunction", autospec=True |
| 82 | + ) as mock_stef: |
| 83 | + # Simulate an error during the embedding function's __init__ |
| 84 | + mock_stef.side_effect = Exception("Simulated initialization error") |
| 85 | + |
| 86 | + with pytest.raises(Exception) as excinfo: |
| 87 | + get_embedding_function(config) |
| 88 | + |
| 89 | + # Check if the raised exception is the one we simulated |
| 90 | + assert "Simulated initialization error" in str(excinfo.value) |
| 91 | + # Check if the additional note was added |
| 92 | + assert "For errors caused by missing dependency" in excinfo.value.__notes__[0] |
| 93 | + |
| 94 | + # Verify that the constructor was called with the correct parameters |
| 95 | + mock_stef.assert_called_once_with( |
| 96 | + model_name="non_existent_model_should_cause_error" |
| 97 | + ) |
| 98 | + |
| 99 | + |
71 | 100 | @pytest.mark.asyncio |
72 | 101 | async def test_try_server(): |
73 | 102 | # This test requires a server to be running, so it's difficult to make it truly isolated. |
|
0 commit comments