50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
import pandas as pd
|
|
|
|
from app.prediction import (
|
|
FEATURES,
|
|
ID_COLUMN,
|
|
PredictionError,
|
|
estimate_remaining_life,
|
|
grade_info,
|
|
interpolate_probability,
|
|
secure_upload_name,
|
|
validate_input_frame,
|
|
)
|
|
|
|
|
|
class PredictionHelpersTest(unittest.TestCase):
|
|
def test_secure_upload_name_accepts_chinese_filename(self) -> None:
|
|
filename, suffix = secure_upload_name("管道数据.xlsx", "run123")
|
|
|
|
self.assertEqual(suffix, ".xlsx")
|
|
self.assertTrue(filename.endswith("_run123.xlsx"))
|
|
|
|
def test_secure_upload_name_rejects_unsupported_extension(self) -> None:
|
|
with self.assertRaises(PredictionError):
|
|
secure_upload_name("管道数据.txt", "run123")
|
|
|
|
def test_probability_helpers(self) -> None:
|
|
self.assertEqual(interpolate_probability([1, 5, 10], [0.9, 0.8, 0.6], 6), 0.6)
|
|
self.assertEqual(estimate_remaining_life([1, 5, 10], [0.9, 0.4, 0.2]), 5.0)
|
|
|
|
def test_grade_boundaries(self) -> None:
|
|
self.assertEqual(grade_info(0.2)[0], "I级")
|
|
self.assertEqual(grade_info(0.8)[0], "IV级")
|
|
self.assertEqual(grade_info(0.81)[0], "V级")
|
|
|
|
def test_validate_input_frame_reports_missing_columns(self) -> None:
|
|
df = pd.DataFrame({ID_COLUMN: [1], FEATURES[0]: [1]})
|
|
|
|
with self.assertRaises(PredictionError) as ctx:
|
|
validate_input_frame(df)
|
|
|
|
self.assertIn("缺少必要字段", ctx.exception.message)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|