Files
production-analytics/tests/test_shift_report_html.py
T

93 lines
3.4 KiB
Python

from dataclasses import replace
from datetime import date, datetime
from html import escape
from zoneinfo import ZoneInfo
import pytest
from test_shift_report_pdf import material_report
from production_analytics.presentation.shift_report import MaterialPieBlock, present_report
from production_analytics.presentation.shift_report_html import render_page
SHIFT_TIMEZONE = ZoneInfo("Europe/Berlin")
def page(value):
return render_page(
machines={value.machine_name: None},
labels={"early": "Frühschicht"},
day=date(2026, 10, 14),
shift="early",
machine=value.machine_name,
report=value,
generated_at=datetime.now(SHIFT_TIMEZONE),
error=None,
)
def test_multiple_materials_chart_and_table():
html = page(material_report((10, 30)))
assert '<figure class="material-chart">' in html
assert '<svg viewBox="0 0 120 120"' in html
assert "Material-0: 10,00 kg (25,00 %)" in html
assert "Material-1: 30,00 kg (75,00 %)" in html
assert '<th scope="col">Materialname</th>' in html
assert "<td>Material-0</td>" in html and "<td>30,00 kg</td>" in html
def test_zero_entries_omitted_and_one_material_supported():
value = material_report((0, 12, 0))
html = page(value)
assert "Material-0" not in html and "Material-2" not in html
assert "Material-1: 12,00 kg (100,00 %)" in html
assert html.count("<circle ") == 1
# Existing PDF presentation still omits single-material charts.
assert not any(isinstance(block, MaterialPieBlock) for block in present_report(value))
@pytest.mark.parametrize("quantities", [(), (0, 0), (-2,)])
def test_no_positive_materials_no_chart(quantities):
html = page(material_report(quantities))
assert '<figure class="material-chart">' not in html
assert "<svg " not in html and "<circle " not in html
if not any(quantities):
assert "Keine Materialverbrauchsdaten vorhanden." in html
def test_sensitive_names_are_text_not_code():
value = material_report((10, 20))
name = '</script><script>alert("x")</script>&\' ${evil} \u2028'
items = tuple(
replace(item, material_name=name, material_number=f"M{index}")
for index, item in enumerate(value.materials.items)
)
html = page(replace(value, materials=replace(value.materials, items=items)))
assert "<script>" not in html and "</script>" not in html
assert escape(name + " (M0)") in html
assert escape(name + " (M1)") in html
assert "<td>" + escape(name) + "</td>" in html
@pytest.mark.parametrize("machine", ["B1", "E1", "K7"])
def test_chart_uses_shared_consolidated_material_totals(machine):
value = material_report((10, 20, 30))
items = tuple(
replace(
item,
material_name="Harz",
material_number="M1",
production_order=f"order-{index}",
erp_booking_share=0.5 if machine == "K7" else None,
)
for index, item in enumerate(value.materials.items)
)
value = replace(value, machine_name=machine, materials=replace(value.materials, items=items))
html = page(value)
assert "Harz: 60,00 kg (100,00 %)" in html
assert html.count("<circle ") == 1
if machine == "K7":
# The order-specific table is preserved alongside the material-level pie.
for index in range(3):
assert f"<td>order-{index}</td>" in html
assert "ERP-Anteil" in html