55import pytest
66from chromadb .api import ClientAPI
77from chromadb .api .models .Collection import Collection
8+ from chromadb .errors import NotFoundError
89
910from semantic_kernel .connectors .chroma import ChromaCollection , ChromaStore
1011from semantic_kernel .exceptions .vector_store_exceptions import VectorStoreOperationException
1112
12- try : # mirrors the connector, resolved here so these tests do not depend on it
13- from chromadb .errors import NotFoundError as _ChromaNotFoundError
14-
15- COLLECTION_NOT_FOUND_IS_VALUE_ERROR = False
16- except ImportError : # chromadb < 1.0
17- _ChromaNotFoundError = ValueError
18- COLLECTION_NOT_FOUND_IS_VALUE_ERROR = True
19-
2013
2114def _not_found () -> Exception :
22- """The error Chroma raises for a missing collection, on whichever version is installed.
23-
24- Resolved here rather than imported from the connector, so these tests run unchanged
25- against the code before and after the fix.
26- """
27- return _ChromaNotFoundError ("Collection test_collection does not exist." )
15+ """The error Chroma raises for a missing collection."""
16+ return NotFoundError ("Collection test_collection does not exist." )
2817
2918
3019@pytest .fixture
@@ -135,9 +124,9 @@ async def test_chroma_collection_search(chroma_collection, mock_client, include_
135124
136125# -- what a Chroma failure means, and what "not there" means --
137126#
138- # chromadb changed the error for a missing collection inside the supported range
139- # (chromadb >= 0.5, < 1.6): 0.5 raised ValueError, 1.0 raises errors.NotFoundError,
140- # which is not a ValueError . collection_exists caught bare Exception, so every failure
127+ # chromadb 1.x raises errors.NotFoundError for a missing collection, which is not a
128+ # ValueError (this connector only imports on chromadb 1.x: it needs
129+ # chromadb.api.collection_configuration) . collection_exists caught bare Exception, so every failure
141130# read as "absent"; ensure_collection_deleted caught only ValueError, so on a modern
142131# chromadb a genuinely absent collection raised instead of being tolerated.
143132
@@ -152,8 +141,9 @@ async def test_collection_exists_raises_when_the_server_cannot_be_reached(chroma
152141 """False means the collection is not there. A refused connection must not say that."""
153142 mock_client .get_collection .side_effect = ConnectionError ("connection refused" )
154143
155- with pytest .raises (ConnectionError ) :
144+ with pytest .raises (VectorStoreOperationException ) as raised :
156145 await chroma_collection .collection_exists ()
146+ assert isinstance (raised .value .__cause__ , ConnectionError )
157147
158148
159149async def test_ensure_collection_deleted_tolerates_an_absent_collection (chroma_collection , mock_client ):
@@ -170,22 +160,14 @@ async def test_ensure_collection_deleted_still_raises_on_a_real_failure(chroma_c
170160 await chroma_collection .ensure_collection_deleted ()
171161
172162
173- @pytest .mark .skipif (
174- COLLECTION_NOT_FOUND_IS_VALUE_ERROR ,
175- reason = "on chromadb < 1.0 a ValueError IS the missing-collection signal" ,
176- )
177163async def test_collection_exists_raises_on_an_unrelated_value_error (chroma_collection , mock_client ):
178164 """Where Chroma has a dedicated NotFoundError, a ValueError is not 'absent'."""
179165 mock_client .get_collection .side_effect = ValueError ("some other problem" )
180166
181- with pytest .raises (ValueError ):
167+ with pytest .raises (VectorStoreOperationException ):
182168 await chroma_collection .collection_exists ()
183169
184170
185- @pytest .mark .skipif (
186- COLLECTION_NOT_FOUND_IS_VALUE_ERROR ,
187- reason = "on chromadb < 1.0 a ValueError IS the missing-collection signal" ,
188- )
189171async def test_ensure_collection_deleted_does_not_swallow_an_unrelated_value_error (chroma_collection , mock_client ):
190172 """The more dangerous half: a swallowed ValueError here would silently skip a delete."""
191173 mock_client .delete_collection .side_effect = ValueError ("some other problem" )
0 commit comments