import pytest from pathlib import Path from docx import Document from src.core.converter import process_document @pytest.fixture def create_gost_example(): # Создаём inputs/gost_example/ doc_dir = Path("tests/fixtures/inputs/gost_example") doc_dir.mkdir(parents=True, exist_ok=True) # Создаём document.md md_content = """ # ВВЕДЕНИЕ Текст введения. ## 1.1 Актуальность темы Текст актуальности. | Показатель | 2022 | |------------|------| | Выручка | 1000 | ![Рис. 1 — Диаграмма](images/chart.png) """ (doc_dir / "document.md").write_text(md_content, encoding="utf-8") # Создаём images/ (doc_dir / "images").mkdir(exist_ok=True) # Создаём изображение from PIL import Image img_path = doc_dir / "images/chart.png" img = Image.new("RGB", (100, 100), color="blue") img.save(img_path) yield doc_dir.parent # возвращаем Path("tests/fixtures/inputs") # Удаляем после теста import shutil shutil.rmtree(doc_dir.parent, ignore_errors=True) def test_gost_example_document_is_not_empty(create_gost_example): input_dir = create_gost_example process_document( input_dir=input_dir, output_dir=Path("tests/output"), style_config_path=Path("resources/themes/gost.yaml"), ) output_path = Path("tests/output/gost_example.docx") assert output_path.exists(), "Файл .docx не создан" # Открываем и проверяем, что в документе есть хотя бы 1 непустой параграф doc = Document(output_path) # Проверяем, что есть хотя бы 1 параграф assert len(doc.paragraphs) > 0, f"Документ пустой: {len(doc.paragraphs)} параграфов" # Проверяем, что хотя бы один параграф **не пустой** non_empty_paragraphs = [p for p in doc.paragraphs if p.text.strip() != ""] assert len(non_empty_paragraphs) > 0, f"Все параграфы пустые: {[p.text for p in doc.paragraphs]}" # Проверяем, что есть хотя бы один заголовок "ВВЕДЕНИЕ" found_intro = any("ВВЕДЕНИЕ" in p.text for p in doc.paragraphs) assert found_intro, f"Заголовок 'ВВЕДЕНИЕ' не найден. Параграфы: {[p.text for p in doc.paragraphs]}" # Проверяем, что есть текст "Текст введения" found_intro_text = any("Текст введения" in p.text for p in doc.paragraphs) assert found_intro_text, f"Текст 'Текст введения' не найден. Параграфы: {[p.text for p in doc.paragraphs]}" # Удаляем после теста output_path.unlink(missing_ok=True) def test_gost_example_document_content(create_gost_example): input_dir = create_gost_example process_document( input_dir=input_dir, output_dir=Path("tests/output"), style_config_path=Path("resources/themes/gost.yaml"), ) output_path = Path("tests/output/gost_example.docx") assert output_path.exists(), "Файл .docx не создан" doc = Document(output_path) # Проверим, что есть хотя бы 2 параграфа (заголовок + текст) assert len(doc.paragraphs) >= 2, f"Документ содержит только {len(doc.paragraphs)} параграфов" # Проверим, что в параграфах есть нужный текст texts = [p.text for p in doc.paragraphs] assert "ВВЕДЕНИЕ" in texts, "Заголовок 'ВВЕДЕНИЕ' не найден" assert "Текст введения." in texts, "Текст введения не найден" assert "Текст актуальности." in texts, "Текст актуальности не найден" # Проверим, что таблица добавлена assert len(doc.tables) >= 1, "Таблица не найдена в документе" # Проверим, что в таблице есть нужные данные table = doc.tables[0] assert table.cell(0, 0).text == "Показатель" assert table.cell(0, 1).text == "2022" # заголовок assert table.cell(1, 0).text == "Выручка" assert table.cell(1, 1).text == "1000" # тело таблицы # Удаляем после теста output_path.unlink(missing_ok=True) def test_gost_example_document_is_not_empty_debug(create_gost_example): input_dir = create_gost_example process_document( input_dir=input_dir, output_dir=Path("tests/output"), style_config_path=Path("resources/themes/gost.yaml"), ) output_path = Path("tests/output/gost_example.docx") assert output_path.exists(), "Файл .docx не создан" # Открываем и проверяем, что в документе есть хотя бы 1 непустой параграф doc = Document(output_path) print(f"Количество параграфов: {len(doc.paragraphs)}") for i, p in enumerate(doc.paragraphs): print(f"Параграф {i}: '{p.text}' (len={len(p.text)})") print(f" Количество runs: {len(p.runs)}") for j, run in enumerate(p.runs): print(f" Run {j}: '{run.text}' (len={len(run.text)})") # Проверяем, что есть хотя бы 1 параграф assert len(doc.paragraphs) > 0, f"Документ пустой: {len(doc.paragraphs)} параграфов" # Проверяем, что хотя бы один параграф **не пустой** non_empty_paragraphs = [p for p in doc.paragraphs if p.text.strip() != ""] assert len(non_empty_paragraphs) > 0, f"Все параграфы пустые: {[p.text for p in doc.paragraphs]}" # Проверяем, что есть хотя бы один заголовок "ВВЕДЕНИЕ" found_intro = any("ВВЕДЕНИЕ" in p.text for p in doc.paragraphs) assert found_intro, f"Заголовок 'ВВЕДЕНИЕ' не найден. Параграфы: {[p.text for p in doc.paragraphs]}" # Проверяем, что есть текст "Текст введения" found_intro_text = any("Текст введения" in p.text for p in doc.paragraphs) assert found_intro_text, f"Текст 'Текст введения' не найден. Параграфы: {[p.text for p in doc.paragraphs]}" # Удаляем после теста output_path.unlink(missing_ok=True)