Add testing

This commit is contained in:
2025-09-26 01:29:19 +00:00
parent 3ec2687226
commit de9826a4b6
13 changed files with 445 additions and 100 deletions

67
tests/conftest.py Normal file
View File

@@ -0,0 +1,67 @@
import subprocess
import time
import pytest
from fastembed import TextEmbedding
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, PointStruct, VectorParams
@pytest.fixture(scope="session")
def embedding_model():
return TextEmbedding()
@pytest.fixture(scope="session")
def qdrant_client(embedding_model: TextEmbedding):
client = QdrantClient(":memory:")
documents: list[str] = [
"Rick es el mas guapo",
"Los pulpos tienen tres corazones y sangre azul",
"Las cucarachas pueden vivir hasta una semana sin cabeza",
"Los koalas tienen huellas dactilares casi idénticas a las humanas",
"La miel nunca se echa a perder, incluso después de miles de años",
]
embeddings = list(embedding_model.embed(documents))
size = len(embeddings[0])
_ = client.recreate_collection(
collection_name="dummy_collection",
vectors_config=VectorParams(distance=Distance.COSINE, size=size),
)
for idx, (emb, document) in enumerate(zip(embeddings, documents)):
_ = client.upsert(
collection_name="dummy_collection",
points=[
PointStruct(id=idx, vector=emb.tolist(), payload={"text": document})
],
)
yield client
@pytest.fixture(scope="session", autouse=True)
def run_mcp():
# Start the MCP server in the background
process = subprocess.Popen(
["uv", "run", "vector-search-mcp"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# Give the server a moment to start up
time.sleep(2)
try:
yield "http://localhost:8000/sse"
finally:
# Clean up the process when tests are done
process.terminate()
try:
_ = process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
_ = process.wait()

31
tests/test_mcp.py Normal file
View File

@@ -0,0 +1,31 @@
import json
from fastembed import TextEmbedding
from fastmcp import Client
from mcp.types import TextContent
async def test_call_tool(embedding_model: TextEmbedding, run_mcp: str):
input = "Quien es el mas guapo?"
collection = "dummy_collection"
embedding: list[float] = list(embedding_model.embed(input))[0].tolist()
client = Client(run_mcp)
async with client:
name = "semantic_search"
body = {"embedding": embedding, "collection": collection}
result = await client.call_tool(name, body)
content_block = result.content[0]
assert isinstance(content_block, TextContent)
deserialized_result = json.loads(content_block.text)
top_result = deserialized_result[0]
assert top_result["chunk_id"] == "0"
assert top_result["score"] > 0.7
assert top_result["payload"] == {"text": "Rick es el mas guapo"}