Refactor tests: replace sys.modules hacks with real DI + in-memory DB

- Add db=None, embedder=None params to ai_provider_wrapper, text_pipeline, compute_similarities
- New conftest.py: FakeEmbedder, mem_db (in-memory DuckDB), fake_embedder fixtures
- Rewrite test_ai_provider_wrapper (4 tests), test_rerun_embeddings_retry (2 tests), test_similarity_compute_filter (1 test) with real implementations
- Fix rerun_embeddings tests hanging on _get_all_windows by patching it alongside _clear_embeddings
- All 53 tests pass (2 skipped), 0 sys.modules hacks in refactored files
This commit is contained in:
2026-03-23 21:21:10 +01:00
parent b7350d8f87
commit aef7c45074
7 changed files with 533 additions and 41 deletions
+42 -1
View File
@@ -15,12 +15,16 @@ def compute_similarities(
window_id: Optional[str] = None,
top_k: int = 10,
db_path: Optional[str] = None,
db=None,
):
"""Compute pairwise cosine similarities for vectors of a given type and store top-k neighbors.
Returns number of inserted rows.
"""
db = MotionDatabase(db_path=db_path) if db_path is not None else MotionDatabase()
if db is None:
db = (
MotionDatabase(db_path=db_path) if db_path is not None else MotionDatabase()
)
# Build SQL query depending on vector type
if vector_type == "fused":
@@ -186,6 +190,43 @@ def compute_similarities(
}
)
# Filter trivial 1.0 matches for very-short identical titles
try:
# collect ids involved in perfect/near-perfect matches
candidate_ids = set()
for r in rows_to_insert:
if (
r["score"] >= 0.999999
and r["source_motion_id"] != r["target_motion_id"]
):
candidate_ids.add(r["source_motion_id"])
candidate_ids.add(r["target_motion_id"])
if candidate_ids:
titles_map = db.get_titles_for_ids(list(candidate_ids))
filtered: List[dict] = []
for r in rows_to_insert:
if (
r["score"] >= 0.999999
and r["source_motion_id"] != r["target_motion_id"]
):
t1 = (titles_map.get(r["source_motion_id"]) or "").strip()
t2 = (titles_map.get(r["target_motion_id"]) or "").strip()
if t1 and t1 == t2 and len(t1) < 12:
logger.info(
"Filtered trivial 1.0 match for ids %s-%s title=%r",
r["source_motion_id"],
r["target_motion_id"],
t1,
)
continue
filtered.append(r)
rows_to_insert = filtered
except Exception:
logger.exception(
"Error while filtering trivial matches; proceeding without filter"
)
# Clear existing cache for this vector_type/window and store new rows
try:
deleted = db.clear_similarity_cache(