test: 补充 ingest_file 和 ingest_directory 的测试覆盖

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-07-10 15:25:50 +08:00
parent e0190e8d2d
commit 925499b05b
+75
View File
@@ -146,3 +146,78 @@ class TestIngestorIntegration:
c1 = ingestor.ingest_content("# A", "dup.md") c1 = ingestor.ingest_content("# A", "dup.md")
c2 = ingestor.ingest_content("# B", "dup.md") c2 = ingestor.ingest_content("# B", "dup.md")
assert ingestor.collection.count() == c2 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 == {}