Files
law-kb/tests/test_parse_clause.py
freedak 641e33b834 feat: QYLAW 法律法规知识库
- 语义检索(FAISS + embedding)+ 精确查找(法规名+条号)
- RAG 问答(SSE 流式,支持 thinking 折叠显示)
- 法规浏览(原文阅读)
- 历史记录(检索+对话持久化到 SQLite)
- 设置页(系统提示词/模板/LLM 参数可配置)
- 检索质量评估脚本

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-08-07 14:55:25 +08:00

99 lines
3.2 KiB
Python

"""切片解析单测"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from scripts.parse_clause import (
parse_law_file,
extract_name_and_date,
load_region_mapping,
scan_category_dir,
)
# 测试数据路径(相对于项目根)
PACK_DIR = Path(__file__).parent.parent.parent / "law-pack-2026-07-01-markdown"
def test_extract_name_and_date():
"""测试文件名解析"""
name, date = extract_name_and_date("中华人民共和国民法典_20200528.md")
assert name == "中华人民共和国民法典"
assert date == "2020-05-28"
name, date = extract_name_and_date("不动产登记暂行条例_20240310.md")
assert name == "不动产登记暂行条例"
assert date == "2024-03-10"
# 无日期后缀
name, date = extract_name_and_date("某法规.md")
assert name == "某法规"
assert date is None
def test_parse_civil_code():
"""测试民法典切片"""
f = PACK_DIR / "法律" / "中华人民共和国民法典_20200528.md"
if not f.exists():
return # 数据不存在时跳过
clauses = parse_law_file(f, "法律")
assert len(clauses) > 1000 # 民法典 1260 条
assert clauses[0].clause_no == "第一条"
assert clauses[0].law_name == "中华人民共和国民法典"
assert clauses[0].category == "法律"
assert clauses[0].publish_date == "2020-05-28"
assert "保护民事主体" in clauses[0].content
def test_parse_admin_regulation():
"""测试行政法规切片"""
f = PACK_DIR / "行政法规" / "不动产登记暂行条例_20240310.md"
if not f.exists():
return
clauses = parse_law_file(f, "行政法规")
assert len(clauses) > 0
assert clauses[0].category == "行政法规"
def test_parse_local_regulation_with_mapping():
"""测试地方性法规 + 区域映射"""
mapping = load_region_mapping(PACK_DIR / "地方性法规区域映射.json")
f = PACK_DIR / "地方性法规" / "七台河市倭肯河流域水环境保护条例_20211224.md"
if not f.exists():
return
clauses = parse_law_file(f, "地方性法规", mapping)
assert len(clauses) > 0
assert clauses[0].province == "黑龙江省"
assert clauses[0].city == "七台河市"
assert clauses[0].region_level == "市级"
def test_null_mapping_skipped():
"""测试 null 映射文件跳过"""
mapping = load_region_mapping(PACK_DIR / "地方性法规区域映射.json")
f = PACK_DIR / "地方性法规" / "~$壮族自治区巴马盘阳河流域生态环境保护条例_20150527.md"
if not f.exists():
return
clauses = parse_law_file(f, "地方性法规", mapping)
assert len(clauses) == 0 # null 映射应跳过
if __name__ == "__main__":
# 手动运行(无 pytest 时)
tests = [
test_extract_name_and_date,
test_parse_civil_code,
test_parse_admin_regulation,
test_parse_local_regulation_with_mapping,
test_null_mapping_skipped,
]
for t in tests:
try:
t()
print(f"{t.__name__} 通过")
except AssertionError as e:
print(f"{t.__name__} 失败: {e}")
except Exception as e:
print(f"⚠️ {t.__name__} 跳过: {e}")