93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
from datetime import date
|
|
|
|
import pandas as pd
|
|
|
|
from pdf_export import (
|
|
DAGOVERZICHT_MAX_DAYS,
|
|
SECTION_AGP,
|
|
SECTION_DAGOVERZICHT,
|
|
SECTION_EVENTS,
|
|
SECTION_SUMMARY,
|
|
build_report_pdf,
|
|
)
|
|
|
|
|
|
def _sample_glucose():
|
|
timestamps = pd.date_range("2026-01-01", periods=48, freq="30min")
|
|
return pd.DataFrame({"timestamp": timestamps, "mmol": [6.0 + (i % 5) for i in range(48)]})
|
|
|
|
|
|
def _glucose_with_hypo_and_hyper_events():
|
|
"""3 dagen data met een echte hypo- (<3.9, >=15 min) en hyperepisode (>10.0)."""
|
|
timestamps = pd.date_range("2026-01-01", periods=3 * 24 * 12, freq="5min")
|
|
values = [7.0] * len(timestamps)
|
|
values[10:15] = [3.0] * 5 # 25 min hypo
|
|
values[100:106] = [12.0] * 6 # 30 min hyper
|
|
return pd.DataFrame({"timestamp": timestamps, "mmol": values})
|
|
|
|
|
|
def test_build_report_pdf_produces_valid_pdf_bytes():
|
|
glucose = _sample_glucose()
|
|
events_summary = {
|
|
"Hypo Level 1": {"count": 2, "avg_duration_minutes": 20.0, "events_per_week": 1.0},
|
|
}
|
|
pdf_bytes = build_report_pdf(
|
|
[SECTION_SUMMARY, SECTION_AGP, SECTION_EVENTS],
|
|
glucose,
|
|
events_summary,
|
|
date(2026, 1, 1),
|
|
date(2026, 1, 2),
|
|
)
|
|
assert isinstance(pdf_bytes, bytes)
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
assert len(pdf_bytes) > 500
|
|
|
|
|
|
def test_build_report_pdf_handles_empty_glucose():
|
|
empty = pd.DataFrame(columns=["timestamp", "mmol"])
|
|
pdf_bytes = build_report_pdf(
|
|
[SECTION_SUMMARY, SECTION_AGP], empty, {}, date(2026, 1, 1), date(2026, 1, 2)
|
|
)
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
|
|
|
|
def test_build_report_pdf_handles_no_sections_selected():
|
|
glucose = _sample_glucose()
|
|
pdf_bytes = build_report_pdf([], glucose, {}, date(2026, 1, 1), date(2026, 1, 2))
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
|
|
|
|
def test_build_report_pdf_includes_dagoverzicht_section():
|
|
glucose = _sample_glucose()
|
|
pdf_bytes = build_report_pdf(
|
|
[SECTION_DAGOVERZICHT], glucose, {}, date(2026, 1, 1), date(2026, 1, 2)
|
|
)
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
assert len(pdf_bytes) > 500
|
|
|
|
|
|
def test_build_report_pdf_caps_dagoverzicht_at_max_days():
|
|
timestamps = pd.date_range("2026-01-01", periods=(DAGOVERZICHT_MAX_DAYS + 10) * 24, freq="1h")
|
|
glucose = pd.DataFrame({"timestamp": timestamps, "mmol": [7.0] * len(timestamps)})
|
|
pdf_bytes = build_report_pdf(
|
|
[SECTION_DAGOVERZICHT],
|
|
glucose,
|
|
{},
|
|
timestamps[0].date(),
|
|
timestamps[-1].date(),
|
|
)
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
|
|
|
|
def test_build_report_pdf_draws_hypo_and_hyper_charts_when_events_present():
|
|
glucose = _glucose_with_hypo_and_hyper_events()
|
|
events_summary = {
|
|
"Hypo Level 1": {"count": 1, "avg_duration_minutes": 25.0, "events_per_week": 2.3},
|
|
"Hyper Level 1": {"count": 1, "avg_duration_minutes": 30.0, "events_per_week": 2.3},
|
|
}
|
|
pdf_bytes = build_report_pdf(
|
|
[SECTION_EVENTS], glucose, events_summary, date(2026, 1, 1), date(2026, 1, 4)
|
|
)
|
|
assert pdf_bytes.startswith(b"%PDF")
|
|
assert len(pdf_bytes) > 500
|