|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 |
|
| 4 | +import pytest |
| 5 | + |
4 | 6 | from kotaemon.base import DocumentWithEmbedding |
5 | 7 | from kotaemon.storages import ( |
6 | 8 | ChromaVectorStore, |
7 | 9 | InMemoryVectorStore, |
8 | 10 | MilvusVectorStore, |
| 11 | + QdrantVectorStore, |
9 | 12 | SimpleFileVectorStore, |
10 | 13 | ) |
11 | 14 |
|
@@ -248,3 +251,118 @@ def test_save_load_delete(self, tmp_path): |
248 | 251 | # reinit the milvus with the same collection name |
249 | 252 | db2 = MilvusVectorStore(path=str(tmp_path), overwrite=False) |
250 | 253 | assert db2.count() == 0, "delete collection function does not work correctly" |
| 254 | + |
| 255 | + |
| 256 | +class TestQdrantVectorStore: |
| 257 | + def test_add(self): |
| 258 | + from qdrant_client import QdrantClient |
| 259 | + |
| 260 | + db = QdrantVectorStore(collection_name="test", client=QdrantClient(":memory:")) |
| 261 | + |
| 262 | + embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] |
| 263 | + metadatas = [{"a": 1, "b": 2}, {"a": 3, "b": 4}] |
| 264 | + ids = [ |
| 265 | + "0f0611b3-2d9c-4818-ab69-1f1c4cf66693", |
| 266 | + "90aba5d3-f4f8-47c6-bad9-5ea457442e07", |
| 267 | + ] |
| 268 | + |
| 269 | + output = db.add(embeddings=embeddings, metadatas=metadatas, ids=ids) |
| 270 | + assert output == ids, "Expected output to be the same as ids" |
| 271 | + assert db.count() == 2, "Expected 2 added entries" |
| 272 | + |
| 273 | + def test_add_from_docs(self, tmp_path): |
| 274 | + from qdrant_client import QdrantClient |
| 275 | + |
| 276 | + db = QdrantVectorStore(collection_name="test", client=QdrantClient(":memory:")) |
| 277 | + |
| 278 | + embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] |
| 279 | + metadatas = [{"a": 1, "b": 2}, {"a": 3, "b": 4}] |
| 280 | + documents = [ |
| 281 | + DocumentWithEmbedding(embedding=embedding, metadata=metadata) |
| 282 | + for embedding, metadata in zip(embeddings, metadatas) |
| 283 | + ] |
| 284 | + |
| 285 | + output = db.add(documents) |
| 286 | + assert len(output) == 2, "Expected outputting 2 ids" |
| 287 | + assert db.count() == 2, "Expected 2 added entries" |
| 288 | + |
| 289 | + def test_delete(self, tmp_path): |
| 290 | + from qdrant_client import QdrantClient |
| 291 | + |
| 292 | + db = QdrantVectorStore(collection_name="test", client=QdrantClient(":memory:")) |
| 293 | + |
| 294 | + embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]] |
| 295 | + metadatas = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}] |
| 296 | + ids = [ |
| 297 | + "0f0611b3-2d9c-4818-ab69-1f1c4cf66693", |
| 298 | + "90aba5d3-f4f8-47c6-bad9-5ea457442e07", |
| 299 | + "6bed07c3-d284-47a3-a711-c3f9186755b8", |
| 300 | + ] |
| 301 | + |
| 302 | + db.add(embeddings=embeddings, metadatas=metadatas, ids=ids) |
| 303 | + assert db.count() == 3, "Expected 3 added entries" |
| 304 | + db.delete( |
| 305 | + ids=[ |
| 306 | + "0f0611b3-2d9c-4818-ab69-1f1c4cf66693", |
| 307 | + "90aba5d3-f4f8-47c6-bad9-5ea457442e07", |
| 308 | + ] |
| 309 | + ) |
| 310 | + assert db.count() == 1, "Expected 1 remaining entry" |
| 311 | + db.delete(ids=["6bed07c3-d284-47a3-a711-c3f9186755b8"]) |
| 312 | + assert db.count() == 0, "Expected 0 remaining entry" |
| 313 | + |
| 314 | + def test_query(self, tmp_path): |
| 315 | + from qdrant_client import QdrantClient |
| 316 | + |
| 317 | + db = QdrantVectorStore(collection_name="test", client=QdrantClient(":memory:")) |
| 318 | + |
| 319 | + embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]] |
| 320 | + metadatas = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}] |
| 321 | + ids = [ |
| 322 | + "0f0611b3-2d9c-4818-ab69-1f1c4cf66693", |
| 323 | + "90aba5d3-f4f8-47c6-bad9-5ea457442e07", |
| 324 | + "6bed07c3-d284-47a3-a711-c3f9186755b8", |
| 325 | + ] |
| 326 | + |
| 327 | + db.add(embeddings=embeddings, metadatas=metadatas, ids=ids) |
| 328 | + |
| 329 | + _, sim, out_ids = db.query(embedding=[0.1, 0.2, 0.3], top_k=1) |
| 330 | + assert sim[0] - 1.0 < 1e-6 |
| 331 | + assert out_ids == ["0f0611b3-2d9c-4818-ab69-1f1c4cf66693"] |
| 332 | + |
| 333 | + _, _, out_ids = db.query(embedding=[0.4, 0.5, 0.6], top_k=1) |
| 334 | + assert out_ids == ["90aba5d3-f4f8-47c6-bad9-5ea457442e07"] |
| 335 | + |
| 336 | + def test_save_load_delete(self, tmp_path): |
| 337 | + """Test that save/load func behave correctly.""" |
| 338 | + embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]] |
| 339 | + metadatas = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}] |
| 340 | + ids = [ |
| 341 | + "0f0611b3-2d9c-4818-ab69-1f1c4cf66693", |
| 342 | + "90aba5d3-f4f8-47c6-bad9-5ea457442e07", |
| 343 | + "6bed07c3-d284-47a3-a711-c3f9186755b8", |
| 344 | + ] |
| 345 | + from qdrant_client import QdrantClient |
| 346 | + |
| 347 | + db = QdrantVectorStore( |
| 348 | + collection_name="test", client=QdrantClient(path=tmp_path) |
| 349 | + ) |
| 350 | + db.add(embeddings=embeddings, metadatas=metadatas, ids=ids) |
| 351 | + del db |
| 352 | + |
| 353 | + db2 = QdrantVectorStore( |
| 354 | + collection_name="test", client=QdrantClient(path=tmp_path) |
| 355 | + ) |
| 356 | + assert db2.count() == 3 |
| 357 | + |
| 358 | + db2.drop() |
| 359 | + del db2 |
| 360 | + |
| 361 | + db2 = QdrantVectorStore( |
| 362 | + collection_name="test", client=QdrantClient(path=tmp_path) |
| 363 | + ) |
| 364 | + |
| 365 | + with pytest.raises(Exception): |
| 366 | + # Since no docs were added, the collection should not exist yet |
| 367 | + # and thus the count function should raise an exception |
| 368 | + db2.count() |
0 commit comments