@@ -224,6 +224,16 @@ async def test_get_collection():
224
224
with patch ("chromadb.AsyncHttpClient" ) as MockAsyncHttpClient :
225
225
mock_client = MagicMock (spec = AsyncClientAPI )
226
226
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
+ }
227
237
mock_client .get_collection .return_value = mock_collection
228
238
MockAsyncHttpClient .return_value = mock_client
229
239
@@ -252,7 +262,7 @@ async def test_get_collection():
252
262
"created-by" : "VectorCode" ,
253
263
}
254
264
255
- async def mock_get_or_create_collection (
265
+ async def mock_create_collection (
256
266
self ,
257
267
name = None ,
258
268
configuration = None ,
@@ -263,7 +273,7 @@ async def mock_get_or_create_collection(
263
273
mock_collection .metadata .update (metadata or {})
264
274
return mock_collection
265
275
266
- mock_client .get_or_create_collection .side_effect = mock_get_or_create_collection
276
+ mock_client .create_collection .side_effect = mock_create_collection
267
277
MockAsyncHttpClient .return_value = mock_client
268
278
269
279
collection = await get_collection (mock_client , config , make_if_missing = True )
@@ -273,16 +283,18 @@ async def mock_get_or_create_collection(
273
283
)
274
284
assert collection .metadata ["created-by" ] == "VectorCode"
275
285
assert collection .metadata ["hnsw:M" ] == 64
276
- mock_client .get_or_create_collection .assert_called_once ()
286
+ mock_client .create_collection .assert_called_once ()
277
287
mock_client .get_collection .side_effect = None
278
288
279
289
# 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
+ ):
281
294
mock_client = MagicMock (spec = AsyncClientAPI )
282
- mock_client .get_or_create_collection .side_effect = IndexError (
283
- "Hash collision occurred"
284
- )
295
+
285
296
MockAsyncHttpClient .return_value = mock_client
297
+ mock_client .get_collection = AsyncMock (return_value = mock_collection )
286
298
from vectorcode .common import __COLLECTION_CACHE
287
299
288
300
__COLLECTION_CACHE .clear ()
@@ -315,7 +327,8 @@ async def test_get_collection_hnsw():
315
327
"embedding_function" : "SentenceTransformerEmbeddingFunction" ,
316
328
"path" : "/test_project" ,
317
329
}
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
319
332
MockAsyncHttpClient .return_value = mock_client
320
333
321
334
# Clear the collection cache to force creation
@@ -332,9 +345,9 @@ async def test_get_collection_hnsw():
332
345
assert collection .metadata ["created-by" ] == "VectorCode"
333
346
assert collection .metadata ["hnsw:ef_construction" ] == 200
334
347
assert collection .metadata ["hnsw:M" ] == 32
335
- mock_client .get_or_create_collection .assert_called_once ()
348
+ mock_client .create_collection .assert_called_once ()
336
349
assert (
337
- mock_client .get_or_create_collection .call_args .kwargs ["metadata" ]
350
+ mock_client .create_collection .call_args .kwargs ["metadata" ]
338
351
== mock_collection .metadata
339
352
)
340
353
0 commit comments