131 lines
4.6 KiB
Python
131 lines
4.6 KiB
Python
"""Unit tests for neutral-record translation into Mochow SDK operations."""
|
|
|
|
from types import SimpleNamespace
|
|
from uuid import UUID, uuid4
|
|
|
|
import pytest
|
|
|
|
from ekp_vector_store import DenseSearchRequest, MochowVectorStore, VectorRecord
|
|
from ekp_vector_store.mochow import MochowConfigurationError, MochowVectorStoreError
|
|
|
|
|
|
class FakeMochowTable:
|
|
def __init__(self) -> None:
|
|
self.upsert_batches: list[list[object]] = []
|
|
self.delete_filters: list[str] = []
|
|
self.search_calls: list[tuple[object, list[str]]] = []
|
|
self.search_rows: list[dict[str, object]] = []
|
|
|
|
def upsert(self, rows: list[object]) -> object:
|
|
self.upsert_batches.append(rows)
|
|
return None
|
|
|
|
def delete(self, *, filter: str) -> object:
|
|
self.delete_filters.append(filter)
|
|
return None
|
|
|
|
def vector_search(self, *, request: object, projections: list[str]) -> object:
|
|
self.search_calls.append((request, projections))
|
|
return SimpleNamespace(rows=self.search_rows)
|
|
|
|
|
|
def _record(document_id: UUID, chunk_number: int) -> VectorRecord:
|
|
return VectorRecord(
|
|
index_record_id=uuid4(),
|
|
chunk_id=uuid4(),
|
|
document_id=document_id,
|
|
filename="policy.md",
|
|
page=1,
|
|
page_end=2,
|
|
section="产品 / 退款",
|
|
chunk_index=chunk_number,
|
|
content="退款周期为七天。",
|
|
embedding=(0.1, 0.2),
|
|
embedding_model="fake",
|
|
index_profile_id="profile-v1",
|
|
)
|
|
|
|
|
|
def test_mochow_adapter_maps_rows_and_batches_upserts() -> None:
|
|
table = FakeMochowTable()
|
|
store = MochowVectorStore(table, lambda **fields: fields, batch_size=2)
|
|
document_id = uuid4()
|
|
records = tuple(_record(document_id, index) for index in range(3))
|
|
|
|
store.upsert(records)
|
|
|
|
assert [len(batch) for batch in table.upsert_batches] == [2, 1]
|
|
first_row = table.upsert_batches[0][0]
|
|
assert isinstance(first_row, dict)
|
|
assert first_row["index_record_id"] == str(records[0].index_record_id)
|
|
assert first_row["document_id"] == str(document_id)
|
|
assert first_row["embedding"] == [0.1, 0.2]
|
|
assert first_row["section"] == "产品 / 退款"
|
|
|
|
|
|
def test_mochow_adapter_deletes_only_document_profile_rows() -> None:
|
|
table = FakeMochowTable()
|
|
store = MochowVectorStore(table, lambda **fields: fields)
|
|
document_id = uuid4()
|
|
|
|
store.delete_document(document_id, "profile-v1")
|
|
|
|
assert table.delete_filters == [
|
|
f"document_id == '{document_id}' AND index_profile_id == 'profile-v1'"
|
|
]
|
|
|
|
|
|
def test_mochow_adapter_rejects_unsafe_profile_filter() -> None:
|
|
store = MochowVectorStore(FakeMochowTable(), lambda **fields: fields)
|
|
|
|
with pytest.raises(MochowConfigurationError):
|
|
store.delete_document(uuid4(), "bad' OR true")
|
|
|
|
|
|
def test_mochow_adapter_builds_profile_scoped_dense_topk_request() -> None:
|
|
table = FakeMochowTable()
|
|
source = _record(uuid4(), 3)
|
|
table.search_rows = [
|
|
{
|
|
"row": {
|
|
"index_record_id": str(source.index_record_id),
|
|
"chunk_id": str(source.chunk_id),
|
|
"document_id": str(source.document_id),
|
|
"filename": source.filename,
|
|
"page": source.page,
|
|
"page_end": source.page_end,
|
|
"section": source.section,
|
|
"chunk_index": source.chunk_index,
|
|
"content": source.content,
|
|
"index_profile_id": source.index_profile_id,
|
|
},
|
|
"score": 0.87,
|
|
"distance": 0.13,
|
|
}
|
|
]
|
|
store = MochowVectorStore(table, lambda **fields: fields, search_ef=10)
|
|
|
|
candidates = store.dense_search(
|
|
DenseSearchRequest((0.1, 0.2), index_profile_id="profile-v1", top_n=20)
|
|
)
|
|
|
|
sdk_request, projections = table.search_calls[0]
|
|
request_body = sdk_request.to_dict() # type: ignore[attr-defined]
|
|
assert request_body["anns"]["vectorField"] == "embedding"
|
|
assert request_body["anns"]["vectorFloats"] == [0.1, 0.2]
|
|
assert request_body["anns"]["filter"] == "index_profile_id = 'profile-v1'"
|
|
assert request_body["anns"]["params"]["limit"] == 20
|
|
assert request_body["anns"]["params"]["ef"] == 20
|
|
assert "content" in projections
|
|
assert candidates[0].chunk_id == source.chunk_id
|
|
assert candidates[0].score == pytest.approx(0.87)
|
|
|
|
|
|
def test_mochow_adapter_sanitizes_malformed_search_response() -> None:
|
|
table = FakeMochowTable()
|
|
table.search_rows = [{"score": "not-a-number"}]
|
|
store = MochowVectorStore(table, lambda **fields: fields)
|
|
|
|
with pytest.raises(MochowVectorStoreError, match="dense search failed"):
|
|
store.dense_search(DenseSearchRequest((0.1, 0.2), "profile-v1"))
|