This commit is contained in:
66
tests/test_search.py
Normal file
66
tests/test_search.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
"""Tests for DevTrace search module."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import Mock, patch, MagicMock
|
||||||
|
|
||||||
|
from src.search.embeddings import EmbeddingManager
|
||||||
|
from src.search.semantic import SemanticSearch
|
||||||
|
|
||||||
|
|
||||||
|
class TestEmbeddingManager:
|
||||||
|
"""Tests for EmbeddingManager."""
|
||||||
|
|
||||||
|
def test_manager_creation(self):
|
||||||
|
"""Test manager creation."""
|
||||||
|
manager = EmbeddingManager()
|
||||||
|
|
||||||
|
assert manager.model_name == "all-MiniLM-L6-v2"
|
||||||
|
|
||||||
|
def test_content_to_string(self):
|
||||||
|
"""Test content to string conversion."""
|
||||||
|
manager = EmbeddingManager()
|
||||||
|
|
||||||
|
assert manager._content_to_string("test") == "test"
|
||||||
|
|
||||||
|
result = manager._content_to_string({"key": "value"})
|
||||||
|
assert "key" in result
|
||||||
|
assert "value" in result
|
||||||
|
|
||||||
|
def test_compute_similarity(self):
|
||||||
|
"""Test cosine similarity computation."""
|
||||||
|
manager = EmbeddingManager()
|
||||||
|
|
||||||
|
embedding1 = [1.0, 0.0, 0.0]
|
||||||
|
embedding2 = [1.0, 0.0, 0.0]
|
||||||
|
embedding3 = [-1.0, 0.0, 0.0]
|
||||||
|
|
||||||
|
sim_same = manager.compute_similarity(embedding1, embedding2)
|
||||||
|
sim_opposite = manager.compute_similarity(embedding1, embedding3)
|
||||||
|
|
||||||
|
assert sim_same == pytest.approx(1.0, abs=0.01)
|
||||||
|
assert sim_opposite == pytest.approx(-1.0, abs=0.01)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSemanticSearch:
|
||||||
|
"""Tests for SemanticSearch."""
|
||||||
|
|
||||||
|
def test_search_creation(self):
|
||||||
|
"""Test search creation."""
|
||||||
|
search = SemanticSearch()
|
||||||
|
|
||||||
|
assert search.embedding_manager is not None
|
||||||
|
|
||||||
|
def test_keyword_search(self):
|
||||||
|
"""Test keyword search fallback."""
|
||||||
|
search = SemanticSearch()
|
||||||
|
|
||||||
|
documents = {
|
||||||
|
1: {"content": "python test file"},
|
||||||
|
2: {"content": "javascript web app"},
|
||||||
|
3: {"content": "python api server"}
|
||||||
|
}
|
||||||
|
|
||||||
|
results = search._keyword_search("python", documents)
|
||||||
|
|
||||||
|
assert len(results) == 2
|
||||||
|
assert all(r["score"] > 0 for r in results)
|
||||||
Reference in New Issue
Block a user