test: 补充 Searcher list_sources/delete_by_source/get_collection_info 测试

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-07-10 15:25:54 +08:00
parent 925499b05b
commit b0319f366a
+48
View File
@@ -74,3 +74,51 @@ class TestSearcher:
"""无语义匹配时不崩溃."""
results = searcher.search("xyzxyz不存在的内容abcabc", top_k=3)
assert isinstance(results, list)
def test_list_sources(self, searcher):
"""list_sources 返回已入库的源文件列表."""
sources = searcher.list_sources()
assert isinstance(sources, list)
def test_get_collection_info(self, searcher):
"""get_collection_info 返回 collection 信息."""
info = searcher.get_collection_info()
assert info["name"] == "test_search"
assert info["count"] > 0
def test_delete_by_source(self, searcher):
"""delete_by_source 删除源文件的所有 chunks."""
sources_before = searcher.list_sources()
if sources_before:
target = sources_before[0]
result = searcher.delete_by_source(target)
assert result is True
sources_after = searcher.list_sources()
assert target not in sources_after
def test_search_with_source_filter(self, searcher):
"""带 source_file 过滤的搜索."""
sources = searcher.list_sources()
if sources:
results = searcher.search("测试", top_k=3, source_file=sources[0])
assert isinstance(results, list)
for r in results:
assert r["source_file"] == sources[0]
def test_delete_by_source_nonexistent(self, searcher):
"""删除不存在的源文件返回 False."""
result = searcher.delete_by_source("nonexistent_file_xyz.md")
assert result is False
def test_list_sources_empty_collection(self, tmp_path):
"""空 collection 的 list_sources 返回空列表."""
from src.core.config import EmbedConfig
from src.core.db import VectorDB
from src.core.embedder import create_embedder
from src.core.search import Searcher
db = VectorDB(persist_dir=str(tmp_path))
embedder = create_embedder(EmbedConfig(mode="local"))
searcher = Searcher(db, embedder, "empty_coll")
sources = searcher.list_sources()
assert sources == []