| import os |
| import tempfile |
| import matplotlib.pyplot as plt |
| import base64 |
| from weasyprint import HTML |
|
|
| from config import SheamiConfig |
| from pdf_helper import generate_pdf |
|
|
| def test_generate_pdf(): |
| from pathlib import Path |
|
|
| |
| tmp_dir = tempfile.mkdtemp() |
| |
| |
| interpretation_html = """ |
| <h1>Test Patient: John Doe</h1> |
| <p>Age: 45, Sex: Male</p> |
| <p>Clinical Summary:</p> |
| <ul> |
| <li>All vitals normal ✅</li> |
| <li>Minor deviation in cholesterol ▲</li> |
| <li>Vitamin D slightly low ▼</li> |
| </ul> |
| """ |
|
|
| |
| plot_files = [] |
| for i in range(4): |
| plt.figure(figsize=(4,3)) |
| plt.plot([1,2,3,4], [i*2+1, i*2+2, i*2+1, i*2+3], marker='o') |
| plt.title(f"Test Plot {i+1}") |
| plt.xlabel("X") |
| plt.ylabel("Y") |
| plot_path = os.path.join(tmp_dir, f"plot_{i+1}.png") |
| plt.savefig(plot_path) |
| plt.close() |
| plot_files.append((f"Test {i+1}", plot_path)) |
|
|
| |
| logo_path = SheamiConfig.logo_path if hasattr(SheamiConfig, 'logo_path') else plot_files[0][1] |
|
|
| |
| pdf_path = os.path.join(tmp_dir, "test_report.pdf") |
| generate_pdf(pdf_path=pdf_path, interpretation_html=interpretation_html, plot_files=plot_files) |
|
|
| print(f"Test PDF generated at: {pdf_path}") |
|
|
|
|
| test_generate_pdf() |