75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
"""Tests for the .docx export module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from docx import Document
|
|
|
|
from cheddahbot.docx_export import text_to_docx
|
|
|
|
SAMPLE_PR = (
|
|
"Acme Corp Expands Digital Marketing Services Nationwide\n"
|
|
"\n"
|
|
"NEW YORK, Feb 16, 2026 -- Acme Corp, a leader in digital marketing\n"
|
|
"solutions, today announced the expansion of its services to all 50 states.\n"
|
|
"\n"
|
|
"The company has been operating in the Northeast for the past decade and is\n"
|
|
"now bringing its full suite of SEO, content marketing, and paid advertising\n"
|
|
"services to clients across the country.\n"
|
|
"\n"
|
|
'"We are thrilled to bring our proven approach to businesses nationwide,"\n'
|
|
"said Jane Smith, CEO of Acme Corp.\n"
|
|
"\n"
|
|
"For more information, visit www.acmecorp.com.\n"
|
|
)
|
|
|
|
|
|
def test_text_to_docx_creates_file(tmp_path: Path):
|
|
"""Verify .docx file is created at the expected path."""
|
|
out = tmp_path / "test.docx"
|
|
result = text_to_docx(SAMPLE_PR, out)
|
|
|
|
assert result == out
|
|
assert out.exists()
|
|
assert out.stat().st_size > 0
|
|
|
|
|
|
def test_text_to_docx_headline_is_bold(tmp_path: Path):
|
|
"""First paragraph should be the headline with bold formatting."""
|
|
out = tmp_path / "test.docx"
|
|
text_to_docx(SAMPLE_PR, out)
|
|
|
|
doc = Document(str(out))
|
|
headline_para = doc.paragraphs[0]
|
|
assert headline_para.runs[0].bold is True
|
|
assert "Acme Corp Expands" in headline_para.text
|
|
|
|
|
|
def test_text_to_docx_body_paragraphs(tmp_path: Path):
|
|
"""Body text should be split into multiple paragraphs."""
|
|
out = tmp_path / "test.docx"
|
|
text_to_docx(SAMPLE_PR, out)
|
|
|
|
doc = Document(str(out))
|
|
# Headline + 4 body paragraphs
|
|
assert len(doc.paragraphs) >= 4
|
|
|
|
|
|
def test_text_to_docx_empty_text(tmp_path: Path):
|
|
"""Empty input should still produce a valid .docx."""
|
|
out = tmp_path / "empty.docx"
|
|
text_to_docx("", out)
|
|
|
|
assert out.exists()
|
|
doc = Document(str(out))
|
|
assert len(doc.paragraphs) == 0
|
|
|
|
|
|
def test_text_to_docx_creates_parent_dirs(tmp_path: Path):
|
|
"""Should create parent directories if they don't exist."""
|
|
out = tmp_path / "sub" / "dir" / "test.docx"
|
|
text_to_docx(SAMPLE_PR, out)
|
|
|
|
assert out.exists()
|