Files
LEANN/tests/test_basic.py
Andy Lee ab339886dd fix: add --distance-metric support to DiskANN embedding server and remove obsolete macOS ABI test markers
- Add --distance-metric parameter to diskann_embedding_server.py for consistency with other backends
- Remove pytest.skip and pytest.xfail markers for macOS C++ ABI issues as they have been fixed
- Fix test assertions to handle SearchResult objects correctly
- All tests now pass on macOS with the C++ ABI compatibility fixes
2025-07-28 14:49:51 -07:00

86 lines
2.5 KiB
Python

"""
Basic functionality tests for CI pipeline using pytest.
"""
import tempfile
from pathlib import Path
import pytest
def test_imports():
"""Test that all packages can be imported."""
# Test C++ extensions
@pytest.mark.parametrize("backend_name", ["hnsw", "diskann"])
def test_backend_basic(backend_name):
"""Test basic functionality for each backend."""
from leann.api import LeannBuilder, LeannSearcher, SearchResult
# Create temporary directory for index
with tempfile.TemporaryDirectory() as temp_dir:
index_path = str(Path(temp_dir) / f"test.{backend_name}")
# Test with small data
texts = [f"This is document {i} about topic {i % 5}" for i in range(100)]
# Configure builder based on backend
if backend_name == "hnsw":
builder = LeannBuilder(
backend_name="hnsw",
embedding_model="facebook/contriever",
embedding_mode="sentence-transformers",
M=16,
efConstruction=200,
)
else: # diskann
builder = LeannBuilder(
backend_name="diskann",
embedding_model="facebook/contriever",
embedding_mode="sentence-transformers",
num_neighbors=32,
search_list_size=50,
)
# Add texts
for text in texts:
builder.add_text(text)
# Build index
builder.build_index(index_path)
# Test search
searcher = LeannSearcher(index_path)
results = searcher.search("document about topic 2", top_k=5)
# Verify results
assert len(results) > 0
assert isinstance(results[0], SearchResult)
assert "topic 2" in results[0].text or "document" in results[0].text
def test_large_index():
"""Test with larger dataset."""
from leann.api import LeannBuilder, LeannSearcher
with tempfile.TemporaryDirectory() as temp_dir:
index_path = str(Path(temp_dir) / "test_large.hnsw")
texts = [f"Document {i}: {' '.join([f'word{j}' for j in range(50)])}" for i in range(1000)]
builder = LeannBuilder(
backend_name="hnsw",
embedding_model="facebook/contriever",
embedding_mode="sentence-transformers",
)
for text in texts:
builder.add_text(text)
builder.build_index(index_path)
searcher = LeannSearcher(index_path)
results = searcher.search(["word10 word20"], top_k=10)
assert len(results[0]) == 10