@@ -224,6 +224,16 @@ async def test_get_collection():
224224 with patch ("chromadb.AsyncHttpClient" ) as MockAsyncHttpClient :
225225 mock_client = MagicMock (spec = AsyncClientAPI )
226226 mock_collection = MagicMock ()
227+ mock_collection .metadata = {
228+ "path" : config .project_root ,
229+ "hostname" : socket .gethostname (),
230+ "created-by" : "VectorCode" ,
231+ "username" : os .environ .get (
232+ "USER" , os .environ .get ("USERNAME" , "DEFAULT_USER" )
233+ ),
234+ "embedding_function" : config .embedding_function ,
235+ "hnsw:M" : 64 ,
236+ }
227237 mock_client .get_collection .return_value = mock_collection
228238 MockAsyncHttpClient .return_value = mock_client
229239
@@ -252,7 +262,7 @@ async def test_get_collection():
252262 "created-by" : "VectorCode" ,
253263 }
254264
255- async def mock_get_or_create_collection (
265+ async def mock_create_collection (
256266 self ,
257267 name = None ,
258268 configuration = None ,
@@ -263,7 +273,7 @@ async def mock_get_or_create_collection(
263273 mock_collection .metadata .update (metadata or {})
264274 return mock_collection
265275
266- mock_client .get_or_create_collection .side_effect = mock_get_or_create_collection
276+ mock_client .create_collection .side_effect = mock_create_collection
267277 MockAsyncHttpClient .return_value = mock_client
268278
269279 collection = await get_collection (mock_client , config , make_if_missing = True )
@@ -273,16 +283,18 @@ async def mock_get_or_create_collection(
273283 )
274284 assert collection .metadata ["created-by" ] == "VectorCode"
275285 assert collection .metadata ["hnsw:M" ] == 64
276- mock_client .get_or_create_collection .assert_called_once ()
286+ mock_client .create_collection .assert_called_once ()
277287 mock_client .get_collection .side_effect = None
278288
279289 # Test raising IndexError on hash collision.
280- with patch ("chromadb.AsyncHttpClient" ) as MockAsyncHttpClient :
290+ with (
291+ patch ("chromadb.AsyncHttpClient" ) as MockAsyncHttpClient ,
292+ patch ("socket.gethostname" , side_effect = (lambda : "dummy" )),
293+ ):
281294 mock_client = MagicMock (spec = AsyncClientAPI )
282- mock_client .get_or_create_collection .side_effect = IndexError (
283- "Hash collision occurred"
284- )
295+
285296 MockAsyncHttpClient .return_value = mock_client
297+ mock_client .get_collection = AsyncMock (return_value = mock_collection )
286298 from vectorcode .common import __COLLECTION_CACHE
287299
288300 __COLLECTION_CACHE .clear ()
@@ -315,7 +327,8 @@ async def test_get_collection_hnsw():
315327 "embedding_function" : "SentenceTransformerEmbeddingFunction" ,
316328 "path" : "/test_project" ,
317329 }
318- mock_client .get_or_create_collection .return_value = mock_collection
330+ mock_client .create_collection .return_value = mock_collection
331+ mock_client .get_collection .side_effect = ValueError
319332 MockAsyncHttpClient .return_value = mock_client
320333
321334 # Clear the collection cache to force creation
@@ -332,9 +345,9 @@ async def test_get_collection_hnsw():
332345 assert collection .metadata ["created-by" ] == "VectorCode"
333346 assert collection .metadata ["hnsw:ef_construction" ] == 200
334347 assert collection .metadata ["hnsw:M" ] == 32
335- mock_client .get_or_create_collection .assert_called_once ()
348+ mock_client .create_collection .assert_called_once ()
336349 assert (
337- mock_client .get_or_create_collection .call_args .kwargs ["metadata" ]
350+ mock_client .create_collection .call_args .kwargs ["metadata" ]
338351 == mock_collection .metadata
339352 )
340353
0 commit comments