Files
md-to-docx/tests/test_page_layout.py
Mikan 4a48e8de85 [AI]
ГОСТ
2025-12-08 19:49:15 +03:00

145 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from pathlib import Path
from docx import Document
from src.core.style_registry import StyleRegistry
def test_apply_page_layout_all_margins():
"""Тест: все поля заданы."""
yaml_content = """
page:
margins:
top: 2.0
bottom: 2.0
left: 3.0
right: 1.0
styles:
Normal:
font:
name: "Arial"
size: 12
"""
yaml_path = Path("tests/fixtures/test_layout_all.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.apply_page_layout(doc)
section = doc.sections[0]
assert abs(section.top_margin.cm - 2.0) < 0.01
assert abs(section.bottom_margin.cm - 2.0) < 0.01
assert abs(section.left_margin.cm - 3.0) < 0.01
assert abs(section.right_margin.cm - 1.0) < 0.01
yaml_path.unlink(missing_ok=True)
def test_apply_page_layout_partial_margins():
"""Тест: заданы не все поля."""
yaml_content = """
page:
margins:
top: 1.5
left: 2.5
styles:
Normal:
font:
name: "Arial"
size: 12
"""
yaml_path = Path("tests/fixtures/test_layout_partial.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.apply_page_layout(doc)
section = doc.sections[0]
assert abs(section.top_margin.cm - 1.5) < 0.01
assert abs(section.left_margin.cm - 2.5) < 0.01
# Остальные поля не должны измениться (остаются по умолчанию)
# У docx по умолчанию: top=2.54, bottom=2.54, left=3.17, right=3.17
# Но мы их не трогаем, если в YAML нет
yaml_path.unlink(missing_ok=True)
def test_apply_page_layout_no_page_section():
"""Тест: в YAML нет секции page."""
yaml_content = """
styles:
Normal:
font:
name: "Arial"
size: 12
"""
yaml_path = Path("tests/fixtures/test_layout_no_page.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.apply_page_layout(doc)
section = doc.sections[0]
# Поля не должны измениться (остаются по умолчанию)
# top=2.54 cm по умолчанию
assert abs(section.top_margin.cm - 2.54) < 0.01
yaml_path.unlink(missing_ok=True)
def test_apply_page_layout_no_margins():
"""Тест: в YAML есть page, но нет margins."""
yaml_content = """
page:
other_option: value
styles:
Normal:
font:
name: "Arial"
size: 12
"""
yaml_path = Path("tests/fixtures/test_layout_no_margins.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.apply_page_layout(doc)
section = doc.sections[0]
# Поля не должны измениться
assert abs(section.top_margin.cm - 2.54) < 0.01
yaml_path.unlink(missing_ok=True)
def test_apply_page_layout_zero_margins():
"""Тест: нулевые поля."""
yaml_content = """
page:
margins:
top: 0.0
bottom: 0.0
left: 0.0
right: 0.0
styles:
Normal:
font:
name: "Arial"
size: 12
"""
yaml_path = Path("tests/fixtures/test_layout_zero.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.apply_page_layout(doc)
section = doc.sections[0]
assert abs(section.top_margin.cm - 0.0) < 0.01
assert abs(section.bottom_margin.cm - 0.0) < 0.01
assert abs(section.left_margin.cm - 0.0) < 0.01
assert abs(section.right_margin.cm - 0.0) < 0.01
yaml_path.unlink(missing_ok=True)