|
2 | 2 |
|
3 | 3 | from datetime import datetime, timedelta, timezone |
4 | 4 | from pathlib import Path |
5 | | -from typing import Any, Dict, List, Optional |
| 5 | +from typing import Any, Dict, List, Optional, Tuple |
6 | 6 | from unittest.mock import MagicMock, patch |
7 | 7 |
|
8 | 8 | import pytest |
| 9 | +from pydantic import ValidationError |
9 | 10 | from pymilvus import DataType, MilvusClient |
10 | 11 | from pymilvus.client.types import LoadState |
11 | 12 |
|
@@ -496,3 +497,85 @@ def test_autoindex_search_with_level(tmp_path: Path) -> None: |
496 | 497 |
|
497 | 498 | assert len(results) == 1 |
498 | 499 | assert results[0][2] is not None and results[0][2]["city"].string_val == "Paris" |
| 500 | + |
| 501 | + |
| 502 | +def _consistency_calls( |
| 503 | + **online_store: Any, |
| 504 | +) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]: |
| 505 | + """Return the kwargs of create_collection calls and of query/search calls.""" |
| 506 | + with patch(f"{MILVUS_MODULE}.MilvusClient") as mock_client_cls: |
| 507 | + mock_client = _mock_client(mock_client_cls, has_collection=False) |
| 508 | + mock_client.describe_collection.return_value = { |
| 509 | + "collection_name": "test_milvus_driver_embeddings", |
| 510 | + "fields": [ |
| 511 | + {"name": "driver_id_pk", "type": DataType.VARCHAR, "params": {}}, |
| 512 | + {"name": "event_ts", "type": DataType.INT64, "params": {}}, |
| 513 | + {"name": "created_ts", "type": DataType.INT64, "params": {}}, |
| 514 | + { |
| 515 | + "name": "embedding", |
| 516 | + "type": DataType.FLOAT_VECTOR, |
| 517 | + "params": {"dim": 2}, |
| 518 | + }, |
| 519 | + {"name": "city", "type": DataType.VARCHAR, "params": {}}, |
| 520 | + ], |
| 521 | + } |
| 522 | + mock_client.search.return_value = [[]] |
| 523 | + mock_client.query.return_value = [] |
| 524 | + |
| 525 | + store = MilvusOnlineStore() |
| 526 | + config = _mock_config(embedding_dim=2, **online_store) |
| 527 | + fv = _vector_feature_view() |
| 528 | + store.online_read(config, fv, [_entity_key(1)], ["city"]) |
| 529 | + store.retrieve_online_documents_v2( |
| 530 | + config, fv, ["embedding", "city"], embedding=[1.0, 0.0], top_k=1 |
| 531 | + ) |
| 532 | + store.retrieve_online_documents_v2( |
| 533 | + config, fv, ["city"], embedding=None, top_k=1, query_string="Paris" |
| 534 | + ) |
| 535 | + create_calls = [ |
| 536 | + call.kwargs for call in mock_client.create_collection.call_args_list |
| 537 | + ] |
| 538 | + read_calls = [ |
| 539 | + call.kwargs |
| 540 | + for call in mock_client.query.call_args_list |
| 541 | + + mock_client.search.call_args_list |
| 542 | + ] |
| 543 | + return create_calls, read_calls |
| 544 | + |
| 545 | + |
| 546 | +def test_consistency_levels_not_sent_when_unset() -> None: |
| 547 | + create_calls, read_calls = _consistency_calls() |
| 548 | + |
| 549 | + assert len(create_calls) == 1 and len(read_calls) == 3 |
| 550 | + assert all("consistency_level" not in kwargs for kwargs in create_calls) |
| 551 | + assert all("consistency_level" not in kwargs for kwargs in read_calls) |
| 552 | + |
| 553 | + |
| 554 | +def test_consistency_level_applied_to_reads_and_searches_only() -> None: |
| 555 | + create_calls, read_calls = _consistency_calls(consistency_level="Strong") |
| 556 | + |
| 557 | + assert "consistency_level" not in create_calls[0] |
| 558 | + assert len(read_calls) == 3 |
| 559 | + assert all(kwargs["consistency_level"] == "Strong" for kwargs in read_calls) |
| 560 | + |
| 561 | + |
| 562 | +def test_collection_consistency_level_applied_on_create_only() -> None: |
| 563 | + create_calls, read_calls = _consistency_calls(collection_consistency_level="Strong") |
| 564 | + |
| 565 | + assert create_calls[0]["consistency_level"] == "Strong" |
| 566 | + assert all("consistency_level" not in kwargs for kwargs in read_calls) |
| 567 | + |
| 568 | + |
| 569 | +def test_collection_and_read_consistency_levels_independent() -> None: |
| 570 | + create_calls, read_calls = _consistency_calls( |
| 571 | + collection_consistency_level="Session", consistency_level="Strong" |
| 572 | + ) |
| 573 | + |
| 574 | + assert create_calls[0]["consistency_level"] == "Session" |
| 575 | + assert all(kwargs["consistency_level"] == "Strong" for kwargs in read_calls) |
| 576 | + |
| 577 | + |
| 578 | +@pytest.mark.parametrize("field", ["consistency_level", "collection_consistency_level"]) |
| 579 | +def test_invalid_consistency_level_rejected(field: str) -> None: |
| 580 | + with pytest.raises(ValidationError): |
| 581 | + MilvusOnlineStoreConfig(**{field: "Immediate"}) |
0 commit comments