- tests/docgen/:DC-UNIT/API/CONF/E2E/UI/REGRESSION/PERF 分组用例与可复跑脚本,报告逐条留痕(R1-R3 全记录) - 意图 30 条前后端同表双跑(pytest + node) - Playwright headless:docgen-ui/docgen-regression spec + b3 benchpanel 第 5 TAB 断言更新 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
"""test_md2docx.py —— md2docx 转换(打开验证段落/样式/中文字体)。"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
|
|
from docx import Document # noqa: E402
|
|
from docx.oxml.ns import qn # noqa: E402
|
|
|
|
from app.services.docgen.md2docx import docx_from_markdown # noqa: E402
|
|
|
|
|
|
MD_SAMPLE = """# 第一章 总则
|
|
|
|
这是正文段落,介绍基本情况。
|
|
|
|
## 1.1 适用范围
|
|
|
|
- 列表项一
|
|
- 列表项二
|
|
|
|
1. 第一步
|
|
2. 第二步
|
|
|
|
> 引用说明文字。
|
|
|
|
| 字段 | 说明 |
|
|
|------|------|
|
|
| 名称 | 项目名称 |
|
|
"""
|
|
|
|
|
|
def test_convert_and_open(tmp_path):
|
|
out = docx_from_markdown(MD_SAMPLE, tmp_path / "out.docx")
|
|
assert out.exists()
|
|
doc = Document(str(out))
|
|
paras = [p for p in doc.paragraphs]
|
|
styles = [p.style.name for p in paras]
|
|
assert "Heading 1" in styles
|
|
assert "Heading 2" in styles
|
|
assert "List Bullet" in styles
|
|
assert "List Number" in styles
|
|
assert "Quote" in styles
|
|
assert "Normal" in styles
|
|
# 表格:2 行 2 列
|
|
assert len(doc.tables) == 1
|
|
table = doc.tables[0]
|
|
assert len(table.rows) == 2
|
|
assert table.cell(0, 0).text == "字段"
|
|
assert table.cell(1, 1).text == "项目名称"
|
|
|
|
|
|
def test_chinese_fonts_east_asia(tmp_path):
|
|
out = docx_from_markdown("正文中文段落。", tmp_path / "out.docx")
|
|
doc = Document(str(out))
|
|
para = doc.paragraphs[0]
|
|
run = para.runs[0]
|
|
assert run.font.name == "宋体" # 西文名
|
|
rfonts = run._element.find(qn("w:rPr") + "/" + qn("w:rFonts")) if run._element.rPr is not None else None
|
|
# eastAsia 属性必须设置(中文渲染关键)
|
|
rpr = run._element.find(qn("w:rPr"))
|
|
assert rpr is not None
|
|
rfonts = rpr.find(qn("w:rFonts"))
|
|
assert rfonts is not None
|
|
assert rfonts.get(qn("w:eastAsia")) == "宋体"
|
|
|
|
|
|
def test_heading_font_heiti(tmp_path):
|
|
out = docx_from_markdown("# 标题一", tmp_path / "out.docx")
|
|
doc = Document(str(out))
|
|
para = doc.paragraphs[0]
|
|
run = para.runs[0]
|
|
assert run.font.name == "黑体"
|
|
rfonts = run._element.find(qn("w:rPr")).find(qn("w:rFonts"))
|
|
assert rfonts.get(qn("w:eastAsia")) == "黑体"
|
|
assert run.font.bold is True
|
|
|
|
|
|
def test_empty_markdown(tmp_path):
|
|
out = docx_from_markdown("", tmp_path / "empty.docx")
|
|
assert out.exists()
|
|
doc = Document(str(out))
|
|
assert len(doc.paragraphs) == 0
|