Files
md-to-docx/tests/test_converter.py
Mikan 8da965234c [AI]
Поддержка тем
2025-12-08 17:21:41 +03:00

85 lines
2.6 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.converter import process_document
@pytest.fixture
def create_example_doc():
# Создаём inputs/example_doc/
doc_dir = Path("tests/fixtures/inputs/example_doc")
doc_dir.mkdir(parents=True, exist_ok=True)
# Создаём document.md
md_content = "# Заголовок\n\nПараграф."
(doc_dir / "document.md").write_text(md_content, encoding="utf-8")
# Создаём images/
(doc_dir / "images").mkdir(exist_ok=True)
yield doc_dir.parent # возвращаем Path("tests/fixtures/inputs")
# Удаляем после теста
import shutil
shutil.rmtree(doc_dir.parent, ignore_errors=True)
def test_process_document_with_template(create_example_doc):
# create_example_doc = Path("tests/fixtures/inputs")
input_dir = create_example_doc
# Создать пустой .dotx файл для теста
tpl_path = Path("tests/fixtures/template.dotx")
tpl_path.parent.mkdir(exist_ok=True)
doc = Document()
doc.save(tpl_path)
# Запустить конвертацию с шаблоном
process_document(
input_dir=input_dir,
output_dir=Path("tests/output"),
style_config_path=Path("resources/style_config.yaml"),
template_path=tpl_path
)
output_path = Path("tests/output/example_doc.docx")
assert output_path.exists()
# Удаляем файл после теста
output_path.unlink(missing_ok=True)
tpl_path.unlink(missing_ok=True)
def test_process_document_with_theme():
# Создаём тему
theme_dir = Path("resources/themes")
theme_dir.mkdir(exist_ok=True)
theme_path = theme_dir / "test_theme.yaml"
theme_path.write_text("""
styles:
Heading 1:
font:
name: "Arial"
size: 14
bold: true
paragraph:
space_before: 10
space_after: 5
""", encoding="utf-8")
# Создаём фикстуру
doc_dir = Path("tests/fixtures/inputs/theme_test")
doc_dir.mkdir(parents=True, exist_ok=True)
(doc_dir / "document.md").write_text("# Заголовок", encoding="utf-8")
(doc_dir / "images").mkdir(exist_ok=True)
process_document(
input_dir=Path("tests/fixtures/inputs"),
output_dir=Path("tests/output"),
style_config_path=Path("resources/style_config.yaml"),
theme_name="test_theme"
)
output_path = Path("tests/output/theme_test.docx")
assert output_path.exists()
output_path.unlink(missing_ok=True)
theme_path.unlink(missing_ok=True)