From b0319f366a4c3398d90bd8c7d1ce868ea0852e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Fri, 10 Jul 2026 15:25:54 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=85=20Searcher=20list=5F?= =?UTF-8?q?sources/delete=5Fby=5Fsource/get=5Fcollection=5Finfo=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude --- tests/test_search.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_search.py b/tests/test_search.py index 42611f4..4371376 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -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 == []