PDC / backend /tests /test_formula_integrity.py
borndeveloper's picture
Align feasibility engine with client workflow and add GSM case selection.
9ac1cdc
Raw
History Blame Contribute Delete
2.26 kB
import math
from app.services.data_service import DataService
def _close(a: float, b: float, tol: float = 1e-9) -> bool:
return abs(a - b) <= tol
def test_change_formulas_match_manual_calculation() -> None:
ds = DataService()
ds.load_data()
assert ds.df is not None
epi_row = ds.df.dropna(subset=["Greige EPI", "FINISH EPI", "epi_change_pct"]).iloc[
0
]
epi_expected = (
(float(epi_row["FINISH EPI"]) - float(epi_row["Greige EPI"]))
/ float(epi_row["Greige EPI"])
) * 100
assert _close(epi_expected, float(epi_row["epi_change_pct"]))
ppi_row = ds.df.dropna(subset=["Greige PPI", "FINISH PPI", "ppi_change_pct"]).iloc[
0
]
ppi_expected = (
(float(ppi_row["FINISH PPI"]) - float(ppi_row["Greige PPI"]))
/ float(ppi_row["Greige PPI"])
) * 100
assert _close(ppi_expected, float(ppi_row["ppi_change_pct"]))
width_row = ds.df.dropna(
subset=["Greige Width in INCH", "FINISH WIDTH", "width_change_pct"]
).iloc[0]
width_expected = (
(float(width_row["Greige Width in INCH"]) - float(width_row["FINISH WIDTH"]))
/ float(width_row["Greige Width in INCH"])
) * 100
assert _close(width_expected, float(width_row["width_change_pct"]))
def test_prediction_returns_exact_match_structure() -> None:
ds = DataService()
ds.load_data()
assert ds.df is not None
row = ds.df.dropna(
subset=["weave", "blend", "FINISH EPI", "FINISH PPI", "warp_count", "weft_count"]
).iloc[0]
result = ds.predict_construction(
{
"weave": row["weave"],
"blend": row["blend"],
"warp_count": float(row["warp_count"]),
"weft_count": float(row["weft_count"]),
"finish_epi": float(row["FINISH EPI"]),
"finish_ppi": float(row["FINISH PPI"]),
"target_gsm": float(row["FINISH GSM"])
if row["FINISH GSM"] == row["FINISH GSM"]
else None,
}
)
assert "matches" in result
assert "count_cases" in result
if result["matches"]:
m = result["matches"][0]
assert m["rank"] == 1
assert "construction" in m
assert m["construction"]["greige_epi"] is not None