initial MVP
This commit is contained in:
90
tests/test_handlers.py
Normal file
90
tests/test_handlers.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from docx import Document
|
||||
from src.handlers.builtin import handle_heading, handle_paragraph, handle_image, handle_list
|
||||
from src.core.style_registry import StyleRegistry
|
||||
|
||||
# Тесты для обработчиков
|
||||
def test_handle_heading():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {"type": "heading", "level": 1, "children": [{"type": "text", "raw": "Заголовок 1"}]}
|
||||
handle_heading(node, doc, Path("."), style_reg)
|
||||
assert len(doc.paragraphs) == 1
|
||||
assert doc.paragraphs[0].style.name == "Heading 1"
|
||||
|
||||
def test_handle_paragraph():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {"type": "paragraph", "children": [{"type": "text", "raw": "Текст параграфа"}]}
|
||||
handle_paragraph(node, doc, Path("."), style_reg)
|
||||
assert len(doc.paragraphs) == 1
|
||||
assert doc.paragraphs[0].text == "Текст параграфа"
|
||||
|
||||
def test_handle_image():
|
||||
from PIL import Image
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
style_reg.ensure_styles_in_doc(doc)
|
||||
node = {"type": "image", "src": "images/logo.png", "alt": "Логотип"} # ✅ src = images/logo.png
|
||||
|
||||
# Создаем реальное изображение
|
||||
img_path = Path("tests/fixtures/images/logo.png")
|
||||
img_path.parent.mkdir(exist_ok=True)
|
||||
img = Image.new("RGB", (100, 100), color="red")
|
||||
img.save(img_path)
|
||||
|
||||
handle_image(node, doc, Path("tests/fixtures"), style_reg) # images_dir = tests/fixtures
|
||||
|
||||
# После вызова должно быть 2 параграфа: картинка и подпись
|
||||
assert len(doc.paragraphs) == 2
|
||||
assert doc.paragraphs[1].text == "Логотип" # подпись
|
||||
|
||||
|
||||
def test_debug_handle_image():
|
||||
from PIL import Image
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
style_reg.ensure_styles_in_doc(doc)
|
||||
|
||||
# Проверим, есть ли стиль "Caption" в документе
|
||||
print("Доступные стили:", [s.name for s in doc.styles if s.type.name == 'PARAGRAPH'])
|
||||
assert "Caption" in [s.name for s in doc.styles if s.type.name == 'PARAGRAPH']
|
||||
|
||||
node = {"type": "image", "src": "logo.png", "alt": "Логотип"}
|
||||
img_path = Path("tests/fixtures/images/logo.png")
|
||||
img_path.parent.mkdir(exist_ok=True)
|
||||
img = Image.new("RGB", (100, 100), color="red")
|
||||
img.save(img_path)
|
||||
|
||||
handle_image(node, doc, Path("tests/fixtures"), style_reg)
|
||||
|
||||
print("Количество параграфов:", len(doc.paragraphs))
|
||||
for i, p in enumerate(doc.paragraphs):
|
||||
print(f"Параграф {i}: '{p.text}', стиль: {p.style.name if p.style else 'None'}")
|
||||
|
||||
def test_handle_paragraph_with_inline():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {
|
||||
"type": "paragraph",
|
||||
"children": [
|
||||
{"type": "text", "raw": "Текст "},
|
||||
{"type": "strong", "children": [{"type": "text", "raw": "жирный"}]},
|
||||
{"type": "text", "raw": " и "},
|
||||
{"type": "emphasis", "children": [{"type": "text", "raw": "курсив"}]},
|
||||
{"type": "text", "raw": "."},
|
||||
]
|
||||
}
|
||||
handle_paragraph(node, doc, Path("."), style_reg)
|
||||
|
||||
p = doc.paragraphs[0]
|
||||
runs = p.runs
|
||||
assert len(runs) == 5
|
||||
assert runs[0].text == "Текст "
|
||||
assert runs[1].text == "жирный"
|
||||
assert runs[1].bold is True
|
||||
assert runs[2].text == " и "
|
||||
assert runs[3].text == "курсив"
|
||||
assert runs[3].italic is True
|
||||
assert runs[4].text == "."
|
||||
Reference in New Issue
Block a user