File size: 2,260 Bytes
ecd72cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac1cdc
ecd72cb
 
 
967454e
9ac1cdc
967454e
ecd72cb
 
 
 
9ac1cdc
 
967454e
 
ecd72cb
967454e
ecd72cb
 
 
 
9ac1cdc
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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