67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
"""Tests for app.utils.health — get_health_level / get_health_level_label"""
|
|
import pytest
|
|
|
|
from app.utils.health import get_health_level, get_health_level_label
|
|
|
|
|
|
# ── get_health_level ──────────────────────────────────────────
|
|
|
|
|
|
class TestGetHealthLevel:
|
|
"""根据评分返回健康等级字符串"""
|
|
|
|
def test_excellent_lower_bound(self):
|
|
assert get_health_level(80) == "excellent"
|
|
|
|
def test_excellent_high(self):
|
|
assert get_health_level(100) == "excellent"
|
|
|
|
def test_good_upper_bound(self):
|
|
assert get_health_level(79) == "good"
|
|
|
|
def test_good_lower_bound(self):
|
|
assert get_health_level(60) == "good"
|
|
|
|
def test_pass_upper_bound(self):
|
|
assert get_health_level(59) == "pass"
|
|
|
|
def test_pass_lower_bound(self):
|
|
assert get_health_level(40) == "pass"
|
|
|
|
def test_danger_upper_bound(self):
|
|
assert get_health_level(39) == "danger"
|
|
|
|
def test_danger_zero(self):
|
|
assert get_health_level(0) == "danger"
|
|
|
|
def test_negative_score(self):
|
|
assert get_health_level(-10) == "danger"
|
|
|
|
def test_float_score(self):
|
|
assert get_health_level(80.5) == "excellent"
|
|
|
|
|
|
# ── get_health_level_label ────────────────────────────────────
|
|
|
|
|
|
class TestGetHealthLevelLabel:
|
|
"""根据等级返回中文标签"""
|
|
|
|
@pytest.mark.parametrize(
|
|
"level, label",
|
|
[
|
|
("excellent", "优秀"),
|
|
("good", "良好"),
|
|
("pass", "及格"),
|
|
("danger", "危险"),
|
|
],
|
|
)
|
|
def test_known_levels(self, level, label):
|
|
assert get_health_level_label(level) == label
|
|
|
|
def test_unknown_level(self):
|
|
assert get_health_level_label("unknown") == "未知"
|
|
|
|
def test_empty_string(self):
|
|
assert get_health_level_label("") == "未知"
|