Download tests/test_core.py from entropy25/IndustrialProductionIntelligence: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/spaces/entropy25/IndustrialProductionIntelligence/resolve/main/tests/test_core.py
- Command line
-
hf download hf://spaces/entropy25/IndustrialProductionIntelligence/tests/test_core.py
-
curl -L -o test_core.py https://huggingface.co/spaces/entropy25/IndustrialProductionIntelligence/resolve/main/tests/test_core.py
4.72 kB
| import io | |
| import pandas as pd | |
| import pytest | |
| import ai_engine | |
| from analytics import ( | |
| answer_quick_question, | |
| create_shift_trend_chart, | |
| create_total_production_chart, | |
| detect_outliers, | |
| get_material_stats, | |
| ) | |
| from data_handler import create_validated_tsv, load_uploaded_data, validate_dataframe | |
| from pdf_generator import _flagged_rows, create_csv_export, create_pdf_report | |
| def test_validation_and_download_round_trip(): | |
| raw = pd.DataFrame({ | |
| "date": ["01/01/2025", "01/02/2025", "bad", "01/04/2025", | |
| "01/05/2025", "01/06/2025"], | |
| "weight_kg": [0, 12, 5, -2, float("inf"), 8], | |
| "material_type": ["solid", "solid", "solid", "solid", "solid", " "], | |
| "shift": ["day", "night", "day", "night", "day", "day"], | |
| }) | |
| valid = validate_dataframe(raw) | |
| assert len(valid) == 2 | |
| assert valid.attrs["excluded_rows"] == 4 | |
| assert list(valid.index) == [0, 1] | |
| exported = pd.read_csv(io.StringIO(create_validated_tsv(valid)), sep="\t") | |
| again = validate_dataframe(exported) | |
| assert again["weight_kg"].tolist() == [0, 12] | |
| assert again["date"].equals(valid["date"]) | |
| def test_weekly_shift_chart_aggregates_across_calendar_year(): | |
| df = validate_dataframe(pd.DataFrame({ | |
| "date": ["12/31/2024", "01/01/2025"], | |
| "weight_kg": [10, 20], | |
| "material_type": ["solid", "solid"], | |
| "shift": ["day", "day"], | |
| })) | |
| total = create_total_production_chart(df, "weekly") | |
| shift = create_shift_trend_chart(df, "weekly") | |
| assert list(total.data[0].y) == [30] | |
| assert list(shift.data[0].y) == [30] | |
| def test_report_uses_the_same_flagged_rows_as_dashboard(): | |
| df = validate_dataframe(pd.DataFrame({ | |
| "date": ["01/01/2025"] * 4, | |
| "weight_kg": [10, 10, 10, 1000], | |
| "material_type": ["solid"] * 4, | |
| "shift": ["day"] * 4, | |
| })) | |
| flags = detect_outliers(df) | |
| assert flags["solid"]["row_indices"] == [3] | |
| assert _flagged_rows(df, flags)[0][0] == 4 | |
| report = create_pdf_report(df, get_material_stats(df), flags) | |
| assert report.getvalue().startswith(b"%PDF-") | |
| flags["solid"]["count"] = 2 | |
| with pytest.raises(ValueError, match="stale"): | |
| _flagged_rows(df, flags) | |
| def test_zero_total_does_not_claim_a_share(): | |
| df = validate_dataframe(pd.DataFrame({ | |
| "date": ["01/01/2025"], | |
| "weight_kg": [0], | |
| "material_type": ["solid"], | |
| })) | |
| stats = get_material_stats(df) | |
| assert stats["solid"]["percentage"] == 0 | |
| assert stats["_total_"]["percentage"] == 0 | |
| assert "Recorded_Days" in create_csv_export(stats).columns | |
| def test_norwegian_csv_and_cp1252_upload(): | |
| class Upload: | |
| name = "produksjon.csv" | |
| def getvalue(self): | |
| return ( | |
| "date;weight_kg;material_type;shift\n" | |
| "15.11.2026;1 234,5;Stål;dag\n" | |
| "16.11.2026;2.345,5;stål;natt\n" | |
| ).encode("cp1252") | |
| df = load_uploaded_data(Upload()) | |
| assert df["weight_kg"].tolist() == [1234.5, 2345.5] | |
| assert df["material_type"].tolist() == ["stål", "stål"] | |
| assert df["date"].dt.strftime("%Y-%m-%d").tolist() == ["2026-11-15", "2026-11-16"] | |
| def test_comma_csv_and_local_quick_answer(): | |
| class Upload: | |
| name = "production.csv" | |
| def getvalue(self): | |
| return ( | |
| "date,weight_kg,material_type,shift\n" | |
| "11/13/2026,10,Steel,day\n" | |
| "11/14/2026,20,steel,night\n" | |
| ).encode("utf-8-sig") | |
| df = load_uploaded_data(Upload()) | |
| stats = get_material_stats(df) | |
| assert len(stats) == 2 # one normalized material plus the total | |
| assert "Weekdays:" in answer_quick_question("calendar", df, stats) | |
| assert "night: 20" in answer_quick_question("shift", df, stats) | |
| def test_iso_and_unambiguous_day_first_slash_dates(): | |
| df = validate_dataframe(pd.DataFrame({ | |
| "date": ["2026-11-14", "15/11/2026"], | |
| "weight_kg": ["1,234.5", "2 000,0"], | |
| "material_type": ["Steel", "steel"], | |
| })) | |
| assert df["date"].dt.strftime("%Y-%m-%d").tolist() == [ | |
| "2026-11-14", "2026-11-15" | |
| ] | |
| assert df["weight_kg"].tolist() == [1234.5, 2000.0] | |
| def test_incomplete_gemini_answer_is_not_displayed(monkeypatch): | |
| class Response: | |
| def raise_for_status(self): | |
| pass | |
| def json(self): | |
| return {"candidates": [{ | |
| "finishReason": "MAX_TOKENS", | |
| "content": {"parts": [{"text": "Partial answer"}]}, | |
| }]} | |
| monkeypatch.setattr(ai_engine.requests, "post", lambda *args, **kwargs: Response()) | |
| with pytest.raises(ValueError, match="MAX_TOKENS"): | |
| ai_engine._generate_text("test-key", "prompt") | |