251 lines
10 KiB
Python
251 lines
10 KiB
Python
from dataclasses import replace
|
||
from math import fsum
|
||
from unittest.mock import Mock
|
||
|
||
import pytest
|
||
from test_shift_report import END, START, composition
|
||
|
||
from production_analytics.calculations.fiber_allocation import (
|
||
FiberBooking,
|
||
allocate_fiber_materials,
|
||
)
|
||
from production_analytics.calculations.shift_material import ShiftMaterialSummary
|
||
from production_analytics.presentation.shift_report import (
|
||
MaterialPieBlock,
|
||
TableBlock,
|
||
present_report,
|
||
render_pdf,
|
||
)
|
||
from production_analytics.service.fiber_allocation import K7FiberAllocationService
|
||
|
||
ORDER = "12026000994"
|
||
NAMES = ("PP 1A, weiß, 6,0, hf - 90- Drittland", "PP 1A, weiß, 6,0, hf - 90")
|
||
BOOKINGS = (
|
||
FiberBooking(ORDER, "41070418", NAMES[0], 6878),
|
||
FiberBooking(ORDER, "41070419", NAMES[1], 3122),
|
||
)
|
||
|
||
|
||
def measured_item(order=f"K 7-{ORDER}", quantity=3468.77):
|
||
_, stubs, _, _ = composition("K7")
|
||
return replace(
|
||
stubs["materials"].result.items[0], production_order=order, consumption_kg=quantity
|
||
)
|
||
|
||
|
||
def test_ratios_allocate_measured_mass_without_rounding():
|
||
item = measured_item()
|
||
allocated = allocate_fiber_materials(item, BOOKINGS, erp_order=ORDER)
|
||
assert [value.material_number for value in allocated] == ["41070418", "41070419"]
|
||
assert [value.erp_booking_share for value in allocated] == pytest.approx([0.6878, 0.3122])
|
||
assert [value.consumption_kg for value in allocated] == pytest.approx(
|
||
[2385.819006, 1082.950994]
|
||
)
|
||
assert fsum(value.consumption_kg for value in allocated) == pytest.approx(item.consumption_kg)
|
||
assert allocated[0].consumption_kg != pytest.approx(item.consumption_kg * 0.69)
|
||
assert all(value.baseline_timestamp == item.baseline_timestamp for value in allocated)
|
||
assert all(value.end_timestamp == item.end_timestamp for value in allocated)
|
||
assert all(value.baseline_missing == item.baseline_missing for value in allocated)
|
||
assert item.material_number is None
|
||
|
||
|
||
def test_duplicate_bookings_aggregate_by_number_and_name():
|
||
allocated = allocate_fiber_materials(
|
||
measured_item(),
|
||
[
|
||
FiberBooking(ORDER, "M1", "Fiber", 20),
|
||
FiberBooking(ORDER, "M1", "Fiber", 30),
|
||
FiberBooking(ORDER, "M2", "Fiber", 50),
|
||
],
|
||
erp_order=ORDER,
|
||
)
|
||
assert len(allocated) == 2
|
||
assert [item.material_number for item in allocated] == ["M1", "M2"]
|
||
assert [item.erp_booking_share for item in allocated] == [0.5, 0.5]
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"bookings",
|
||
[
|
||
[],
|
||
[FiberBooking(ORDER, "181900", "Product", 0)],
|
||
[
|
||
FiberBooking(ORDER, "M1", "Fiber", -1),
|
||
FiberBooking(ORDER, "", "Fiber", 10),
|
||
FiberBooking(ORDER, None, "Fiber", 10),
|
||
FiberBooking(ORDER, "M1", "Fiber", None),
|
||
],
|
||
],
|
||
)
|
||
def test_no_usable_bookings_preserves_unassigned_measured_item(bookings):
|
||
item = measured_item()
|
||
assert allocate_fiber_materials(item, bookings, erp_order=ORDER) == (item,)
|
||
|
||
|
||
def test_invalid_rows_ignored_and_single_material_has_full_share():
|
||
allocated = allocate_fiber_materials(
|
||
measured_item(),
|
||
[
|
||
FiberBooking(ORDER, "M1", "Fiber", 25),
|
||
FiberBooking(ORDER, "M2", "Fiber", 0),
|
||
FiberBooking(ORDER, "M3", "Fiber", -10),
|
||
FiberBooking(ORDER, " ", "Fiber", 100),
|
||
FiberBooking(ORDER, "M4", "Fiber", float("nan")),
|
||
FiberBooking("other", "M5", "Fiber", 1000),
|
||
],
|
||
erp_order=ORDER,
|
||
)
|
||
assert len(allocated) == 1
|
||
assert allocated[0].material_number == "M1"
|
||
assert allocated[0].erp_booking_share == 1
|
||
assert allocated[0].consumption_kg == 3468.77
|
||
|
||
|
||
def test_order_aware_service_preserves_totals_runs_and_caches_order_reads():
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.side_effect = lambda order: (
|
||
BOOKINGS if order == ORDER else [FiberBooking(order, "M3", "Other fiber", 1)]
|
||
)
|
||
items = (
|
||
measured_item(quantity=100),
|
||
replace(measured_item(quantity=200), run_id="run2"),
|
||
measured_item("K 7-123", 400),
|
||
)
|
||
measured = ShiftMaterialSummary(items[0].machine_id, START, END, 700, items)
|
||
allocated = K7FiberAllocationService(reader).allocate(measured)
|
||
assert allocated.total_consumption_kg == measured.total_consumption_kg
|
||
assert fsum(item.consumption_kg for item in allocated.items) == pytest.approx(700)
|
||
assert len(allocated.items) == 5
|
||
assert allocated.items[-1].material_number == "M3"
|
||
assert allocated.items[-1].production_order == "K 7-123"
|
||
assert reader.get_fiber_bookings.call_count == 2
|
||
assert measured.items == items
|
||
|
||
|
||
@pytest.mark.parametrize("order", ["K 7-123-K 7-456", "unknown", ""])
|
||
def test_ambiguous_order_never_queries_or_invents_recipe(order):
|
||
reader = Mock()
|
||
item = measured_item(order)
|
||
measured = ShiftMaterialSummary(item.machine_id, START, END, item.consumption_kg, (item,))
|
||
allocated = K7FiberAllocationService(reader).allocate(measured)
|
||
assert allocated.items == measured.items
|
||
reader.get_fiber_bookings.assert_not_called()
|
||
|
||
|
||
@pytest.mark.parametrize("machine", ["B1", "E1", "K7"])
|
||
def test_report_wiring_applies_adapter_only_to_k7(machine):
|
||
service, stubs, _, _ = composition(machine)
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.return_value = BOOKINGS
|
||
service.material_allocators = {"K7": K7FiberAllocationService(reader)}
|
||
stubs["materials"].result = (
|
||
replace(stubs["materials"].result, items=(measured_item(quantity=42),))
|
||
if machine == "K7"
|
||
else stubs["materials"].result
|
||
)
|
||
value = service.build(machine_name=machine, start=START, end=END)
|
||
if machine == "K7":
|
||
assert len(value.materials.items) == 2
|
||
reader.get_fiber_bookings.assert_called_once_with(ORDER)
|
||
else:
|
||
reader.get_fiber_bookings.assert_not_called()
|
||
assert (
|
||
value.materials is stubs["channel_materials" if machine == "E1" else "materials"].result
|
||
)
|
||
|
||
|
||
def test_same_allocation_behavior_for_differently_named_machine():
|
||
service, stubs, _, config = composition("K7")
|
||
service.machines = {"Synthetic": config}
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.return_value = BOOKINGS
|
||
service.material_allocators = {"Synthetic": K7FiberAllocationService(reader)}
|
||
item = measured_item(quantity=42)
|
||
stubs["materials"].result = replace(stubs["materials"].result, items=(item,))
|
||
report = service.build(machine_name="Synthetic", start=START, end=END)
|
||
assert len(report.materials.items) == 2
|
||
assert sum(part.consumption_kg for part in report.materials.items) == pytest.approx(42)
|
||
reader.get_fiber_bookings.assert_called_once_with(ORDER)
|
||
|
||
|
||
def test_pdf_displays_whole_shares_and_estimation_note():
|
||
service, stubs, _, _ = composition("K7")
|
||
item = measured_item()
|
||
stubs["materials"].result = replace(
|
||
stubs["materials"].result, items=(item,), total_consumption_kg=item.consumption_kg
|
||
)
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.return_value = BOOKINGS
|
||
service.material_allocators = {"K7": K7FiberAllocationService(reader)}
|
||
report = service.build(machine_name="K7", start=START, end=END)
|
||
blocks = present_report(report)
|
||
table = next(
|
||
block for block in blocks if isinstance(block, TableBlock) and "ERP-Anteil" in block.headers
|
||
)
|
||
rows = {row[2]: row for row in table.rows}
|
||
assert rows["41070418"][1:] == (NAMES[0], "41070418", "2.385,82 kg", "69%")
|
||
assert rows["41070419"][1:] == (NAMES[1], "41070419", "1.082,95 kg", "31%")
|
||
assert any(
|
||
getattr(block, "text", "")
|
||
== "Verbrauchsaufteilung geschätzt nach ERP-Buchungsverhältnis; Gesamtverbrauch aus ENLYZE."
|
||
for block in blocks
|
||
)
|
||
chart = next(block for block in blocks if isinstance(block, MaterialPieBlock))
|
||
assert fsum(chart.consumption_kg) == pytest.approx(3468.77)
|
||
|
||
|
||
def test_source_ratios_retain_precision_beyond_displayed_percentage():
|
||
bookings = (replace(BOOKINGS[0], quantity_kg=6877.8), replace(BOOKINGS[1], quantity_kg=3122.2))
|
||
allocated = allocate_fiber_materials(measured_item(), bookings, erp_order=ORDER)
|
||
assert [item.consumption_kg for item in allocated] == pytest.approx(
|
||
[2385.7496306, 1083.0203694]
|
||
)
|
||
assert [f"{item.erp_booking_share * 100:.0f}%" for item in allocated] == ["69%", "31%"]
|
||
assert fsum(item.consumption_kg for item in allocated) == pytest.approx(3468.77)
|
||
|
||
|
||
def test_pdf_keeps_different_order_shares_separate_and_combines_material_pie():
|
||
service, stubs, _, _ = composition("K7")
|
||
items = (measured_item(quantity=100), measured_item("K 7-123", 200))
|
||
stubs["materials"].result = replace(
|
||
stubs["materials"].result, items=items, total_consumption_kg=300
|
||
)
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.side_effect = lambda order: (
|
||
BOOKINGS if order == ORDER else [replace(BOOKINGS[0], production_order=order)]
|
||
)
|
||
service.material_allocators = {"K7": K7FiberAllocationService(reader)}
|
||
report = service.build(machine_name="K7", start=START, end=END)
|
||
blocks = present_report(report)
|
||
table = next(
|
||
block for block in blocks if isinstance(block, TableBlock) and "ERP-Anteil" in block.headers
|
||
)
|
||
assert len(table.rows) == 3
|
||
assert {(row[0], row[-1]) for row in table.rows} == {
|
||
(f"K 7-{ORDER}", "69%"),
|
||
(f"K 7-{ORDER}", "31%"),
|
||
("K 7-123", "100%"),
|
||
}
|
||
chart = next(block for block in blocks if isinstance(block, MaterialPieBlock))
|
||
assert len(chart.labels) == 2
|
||
assert sorted(chart.consumption_kg) == pytest.approx([31.22, 268.78])
|
||
assert render_pdf(report) == render_pdf(report)
|
||
|
||
|
||
def test_missing_erp_data_keeps_pdf_unassigned_and_measured_total():
|
||
service, stubs, _, _ = composition("K7")
|
||
stubs["materials"].result = replace(
|
||
stubs["materials"].result, items=(measured_item(),), total_consumption_kg=3468.77
|
||
)
|
||
reader = Mock()
|
||
reader.get_fiber_bookings.return_value = []
|
||
service.material_allocators = {"K7": K7FiberAllocationService(reader)}
|
||
report = service.build(machine_name="K7", start=START, end=END)
|
||
table = next(
|
||
block
|
||
for block in present_report(report)
|
||
if isinstance(block, TableBlock) and "Materialname" in block.headers
|
||
)
|
||
assert table.rows == (("Materialzuordnung fehlt", "–", "3.468,77 kg"),)
|
||
assert report.materials.total_consumption_kg == 3468.77
|