From 925499b05b923b9c24ffe1f9960ba83782ea2bfc 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:50 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=85=20ingest=5Ffile=20?= =?UTF-8?q?=E5=92=8C=20ingest=5Fdirectory=20=E7=9A=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude --- tests/test_ingest.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_ingest.py b/tests/test_ingest.py index b6651af..a371caf 100644 --- a/tests/test_ingest.py +++ b/tests/test_ingest.py @@ -146,3 +146,78 @@ class TestIngestorIntegration: c1 = ingestor.ingest_content("# A", "dup.md") c2 = ingestor.ingest_content("# B", "dup.md") assert ingestor.collection.count() == c2 + + +class TestIngestFile: + """ingest_file 方法测试.""" + + def test_ingest_file_markdown(self, tmp_path): + """通过文件路径入库 .md 文件.""" + from src.core.config import EmbedConfig + from src.core.db import VectorDB + from src.core.embedder import create_embedder + from src.core.ingest import DocumentIngestor + + md_file = tmp_path / "hello.md" + md_file.write_text("# 测试\n这是测试内容。", encoding="utf-8") + + db = VectorDB(persist_dir=str(tmp_path / "db")) + embedder = create_embedder(EmbedConfig(mode="local")) + ingestor = DocumentIngestor(db, embedder, "test_file") + + count = ingestor.ingest_file(str(md_file)) + assert count > 0 + assert ingestor.collection.count() == count + + def test_ingest_file_text(self, tmp_path): + """通过文件路径入库 .txt 文件.""" + from src.core.config import EmbedConfig + from src.core.db import VectorDB + from src.core.embedder import create_embedder + from src.core.ingest import DocumentIngestor + + txt_file = tmp_path / "notes.txt" + txt_file.write_text("这是一段纯文本内容。\n\n第二段内容在这里。", encoding="utf-8") + + db = VectorDB(persist_dir=str(tmp_path / "db")) + embedder = create_embedder(EmbedConfig(mode="local")) + ingestor = DocumentIngestor(db, embedder, "test_txt") + + count = ingestor.ingest_file(str(txt_file)) + assert count > 0 + + +class TestIngestDirectory: + """ingest_directory 方法测试.""" + + def test_ingest_directory_mixed_formats(self, tmp_path): + """入库包含多种格式的目录.""" + from src.core.config import EmbedConfig + from src.core.db import VectorDB + from src.core.embedder import create_embedder + from src.core.ingest import DocumentIngestor + + (tmp_path / "a.md").write_text("# A\n内容 A", encoding="utf-8") + (tmp_path / "b.txt").write_text("内容 B", encoding="utf-8") + (tmp_path / "not_supported.xyz").write_text("不应被处理", encoding="utf-8") + + db = VectorDB(persist_dir=str(tmp_path / "db")) + embedder = create_embedder(EmbedConfig(mode="local")) + ingestor = DocumentIngestor(db, embedder, "test_dir") + + results = ingestor.ingest_directory(str(tmp_path)) + assert len(results) >= 2 # a.md + b.txt, .xyz 被忽略 + + def test_ingest_directory_empty(self, tmp_path): + """空目录返回空结果.""" + from src.core.config import EmbedConfig + from src.core.db import VectorDB + from src.core.embedder import create_embedder + from src.core.ingest import DocumentIngestor + + db = VectorDB(persist_dir=str(tmp_path / "db")) + embedder = create_embedder(EmbedConfig(mode="local")) + ingestor = DocumentIngestor(db, embedder, "test_empty_dir") + + results = ingestor.ingest_directory(str(tmp_path)) + assert results == {}