table + list
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
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.handlers.builtin import handle_heading, handle_paragraph, handle_image, handle_list, handle_table
|
||||
from src.core.style_registry import StyleRegistry
|
||||
|
||||
# Тесты для обработчиков
|
||||
@@ -40,29 +40,6 @@ def test_handle_image():
|
||||
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"))
|
||||
@@ -87,4 +64,88 @@ def test_handle_paragraph_with_inline():
|
||||
assert runs[2].text == " и "
|
||||
assert runs[3].text == "курсив"
|
||||
assert runs[3].italic is True
|
||||
assert runs[4].text == "."
|
||||
assert runs[4].text == "."
|
||||
|
||||
def test_handle_list():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {
|
||||
"type": "list",
|
||||
"ordered": False,
|
||||
"children": [
|
||||
{"type": "list_item", "children": [{"type": "text", "raw": "Элемент 1"}]},
|
||||
{"type": "list_item", "children": [{"type": "text", "raw": "Элемент 2"}]},
|
||||
]
|
||||
}
|
||||
handle_list(node, doc, Path("."), style_reg)
|
||||
|
||||
assert len(doc.paragraphs) == 2
|
||||
assert doc.paragraphs[0].style.name == "List Bullet"
|
||||
assert doc.paragraphs[0].text == "Элемент 1"
|
||||
assert doc.paragraphs[1].text == "Элемент 2"
|
||||
|
||||
|
||||
def test_handle_table():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {
|
||||
"type": "table",
|
||||
"children": [
|
||||
{ # header
|
||||
"type": "table_row",
|
||||
"children": [
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Заголовок 1"}]},
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Заголовок 2"}]},
|
||||
]
|
||||
},
|
||||
{ # row 1
|
||||
"type": "table_row",
|
||||
"children": [
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Ячейка 1"}]},
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Ячейка 2"}]},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
handle_table(node, doc, Path("."), style_reg)
|
||||
|
||||
assert len(doc.tables) == 1
|
||||
table = doc.tables[0]
|
||||
assert len(table.rows) == 2
|
||||
assert table.cell(0, 0).text == "Заголовок 1"
|
||||
assert table.cell(1, 1).text == "Ячейка 2"
|
||||
|
||||
|
||||
def test_handle_table_with_br():
|
||||
doc = Document()
|
||||
style_reg = StyleRegistry(Path("resources/style_config.yaml"))
|
||||
node = {
|
||||
"type": "table",
|
||||
"children": [
|
||||
{ # header
|
||||
"type": "table_row",
|
||||
"children": [
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Заголовок 1"}]},
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Заголовок 2"}]},
|
||||
]
|
||||
},
|
||||
{ # row 1
|
||||
"type": "table_row",
|
||||
"children": [
|
||||
{"type": "table_cell", "children": [
|
||||
{"type": "text", "raw": "Первая строка"},
|
||||
{"type": "softbreak"}, # или linebreak/html
|
||||
{"type": "text", "raw": "Вторая строка"}
|
||||
]},
|
||||
{"type": "table_cell", "children": [{"type": "text", "raw": "Ячейка 2"}]},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
handle_table(node, doc, Path("."), style_reg)
|
||||
|
||||
assert len(doc.tables) == 1
|
||||
table = doc.tables[0]
|
||||
cell_text = table.cell(1, 0).text # "Первая строка\nВторая строка"
|
||||
assert "Первая строка" in cell_text
|
||||
assert "Вторая строка" in cell_text
|
||||
|
||||
Reference in New Issue
Block a user