Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
106 lines
3.8 KiB
Python
106 lines
3.8 KiB
Python
"""Tests for download summary Excel columns."""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
|
|
from planner_download import (
|
|
_build_workable_files,
|
|
_format_status_changed,
|
|
_success_status,
|
|
_write_download_summary,
|
|
)
|
|
|
|
|
|
def test_success_status_labels():
|
|
assert _success_status(0, 0) == "No tasks"
|
|
assert _success_status(0, 3, tasks_skipped_no_attachment=3) == "No attachments"
|
|
assert _success_status(1, 3, tasks_with_downloads=1, tasks_with_attachments=1) == "Success"
|
|
assert _success_status(2, 4, tasks_with_downloads=2, tasks_with_attachments=2) == "Success"
|
|
assert _success_status(1, 4, tasks_with_downloads=1, tasks_with_attachments=2) == "Partial"
|
|
assert _success_status(0, 2, tasks_with_attachments=2) == "Download failed"
|
|
|
|
|
|
def test_format_status_changed():
|
|
assert _format_status_changed(0, 0) == "N/A"
|
|
assert _format_status_changed(2, 2, via_task_details=2) == "Yes - 2/2 tasks (2 via task details)"
|
|
assert _format_status_changed(3, 1, via_task_details=1) == "Partial - 1/3 tasks (1 via task details)"
|
|
assert _format_status_changed(1, 0) == "No - 0/1 tasks"
|
|
assert _format_status_changed(2, 1, via_task_details=1) == "Partial - 1/2 tasks (1 via task details)"
|
|
|
|
|
|
def test_write_download_summary_columns(tmp_path: Path, monkeypatch):
|
|
monkeypatch.setattr("planner_download.PROJECT_ROOT", tmp_path)
|
|
results = {
|
|
"Alpha": {
|
|
"original": [tmp_path / "a.pdf", tmp_path / "b.pdf"],
|
|
"tasks_total": 4,
|
|
"tasks_with_attachments": 2,
|
|
"tasks_skipped_no_attachment": 2,
|
|
"tasks_with_downloads": 2,
|
|
"status_changed": 1,
|
|
"status_via_task_details": 1,
|
|
},
|
|
"Beta": {
|
|
"original": [],
|
|
"tasks_total": 2,
|
|
"tasks_with_attachments": 0,
|
|
"tasks_skipped_no_attachment": 2,
|
|
"tasks_with_downloads": 0,
|
|
"status_changed": 0,
|
|
"status_via_task_details": 0,
|
|
},
|
|
}
|
|
failures = [("Gamma", "not found", "Plan not found")]
|
|
summary_path = tmp_path / "Summary" / "Download_Summary_2026-06-05.xlsx"
|
|
_write_download_summary(
|
|
results,
|
|
failures,
|
|
summary_path,
|
|
planner_url="https://example.com",
|
|
run_timestamp="2026-06-05 12:00:00",
|
|
)
|
|
df = pd.read_excel(summary_path, keep_default_na=False)
|
|
assert list(df.columns) == [
|
|
"Run Timestamp",
|
|
"Facility Name",
|
|
"Files Downloaded",
|
|
"Status",
|
|
"Status Changed",
|
|
]
|
|
alpha = df[df["Facility Name"] == "Alpha"].iloc[0]
|
|
assert alpha["Status"] == "Success"
|
|
assert "1/2 tasks" in alpha["Status Changed"]
|
|
assert "via task details" in alpha["Status Changed"]
|
|
beta = df[df["Facility Name"] == "Beta"].iloc[0]
|
|
assert beta["Status"] == "No attachments"
|
|
assert beta["Status Changed"] == "N/A"
|
|
gamma = df[df["Facility Name"] == "Gamma"].iloc[0]
|
|
assert gamma["Status"] == "Plan not found"
|
|
assert gamma["Status Changed"] == "N/A"
|
|
|
|
|
|
def test_build_workable_files_mirrors_to_medicare(tmp_path: Path):
|
|
src_dir = tmp_path / "original" / "FacA"
|
|
src_dir.mkdir(parents=True)
|
|
pdf = src_dir / "doc.pdf"
|
|
pdf.write_bytes(b"%PDF-1.4")
|
|
|
|
paths, workable_root = _build_workable_files(
|
|
facility_name="Fac A",
|
|
downloaded=[pdf],
|
|
output_root=tmp_path / "Output",
|
|
today_iso="2026-06-16",
|
|
)
|
|
medicare_root = tmp_path / "Output" / "2026-06-16" / "Workable_Medicare"
|
|
|
|
assert len(paths) == 1
|
|
assert paths[0].parent == workable_root
|
|
assert paths[0].is_file()
|
|
assert medicare_root.is_dir()
|
|
medicare_files = list(medicare_root.glob("*.pdf"))
|
|
assert len(medicare_files) == 1
|
|
assert medicare_files[0].name == paths[0].name
|
|
assert medicare_files[0].read_bytes() == pdf.read_bytes()
|