Skip to content

Commit c3563f8

Browse files
authored
test(pdf): add cover summary and cleanup tests (#21)
1 parent 80d5586 commit c3563f8

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

tests/test_pdf_tool.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,65 @@ def test_pdf_with_sections_and_charts(tmp_path):
332332
pdf_path = Path(create_pdf(data, out_path=tmp_path / "complex.pdf"))
333333
assert pdf_path.exists()
334334
assert _count_images(pdf_path) >= 4
335+
336+
337+
def test_cover_summary_box_structure(tmp_path):
338+
"""Ensure summary text is placed inside a table on the cover page."""
339+
from tools.pdf_tool import PdfReportBuilder
340+
from reportlab.platypus import Table
341+
342+
with PdfReportBuilder(tmp_path / "summary_box.pdf") as builder:
343+
builder.add_cover("Demo", summary="Important")
344+
assert any(isinstance(item, Table) for item in builder.story)
345+
pdf_path = builder.save()
346+
347+
pdf_file = Path(pdf_path)
348+
assert pdf_file.exists()
349+
from PyPDF2 import PdfReader
350+
351+
reader = PdfReader(str(pdf_file))
352+
assert "Important" in reader.pages[0].extract_text()
353+
354+
355+
def test_builder_multiple_charts(tmp_path):
356+
"""Verify that multiple charts render correctly using PdfReportBuilder."""
357+
from tools.pdf_tool import PdfReportBuilder
358+
359+
specs = [
360+
{"chart_type": "bar", "labels": ["A", "B"], "values": [1, 2]},
361+
{"chart_type": "line", "labels": [1, 2], "values": [3, 4]},
362+
]
363+
with PdfReportBuilder(tmp_path / "builder_multi.pdf") as builder:
364+
builder.add_cover("Charts")
365+
builder.add_section({"title": "Charts", "type": "chart", "chart_spec": specs})
366+
temp_images = list(builder.tmp_pngs)
367+
assert len(temp_images) == 2
368+
for img in temp_images:
369+
assert Path(img).exists()
370+
pdf_path = builder.save()
371+
assert Path(pdf_path).exists()
372+
# temp images should be removed after save
373+
for img in temp_images:
374+
assert not Path(img).exists()
375+
376+
assert _count_images(Path(pdf_path)) >= 2
377+
378+
379+
def test_tmp_images_cleanup_on_exit(tmp_path):
380+
"""Temporary chart images are deleted after closing the builder."""
381+
from tools.pdf_tool import PdfReportBuilder
382+
383+
with PdfReportBuilder(tmp_path / "cleanup.pdf") as builder:
384+
builder.add_cover("Cleanup")
385+
builder.add_section(
386+
{
387+
"title": "Chart",
388+
"type": "chart",
389+
"chart_spec": {"chart_type": "bar", "labels": ["A"], "values": [1]},
390+
}
391+
)
392+
temp_images = list(builder.tmp_pngs)
393+
assert temp_images and all(Path(p).exists() for p in temp_images)
394+
# Context manager exit should remove files
395+
for p in temp_images:
396+
assert not Path(p).exists()

0 commit comments

Comments
 (0)