/
nevers
/
hackINTERPOL
Обзор
Документация
Войти
/
nevers
/
hackINTERPOL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/test_metrics.py
81 строка
3 KB
nevers
upload files
13 янв 2026, 17:05
13 янв 2026, 17:05
949f79e
Код
Авторство
О чём код?
"""Tests for metrics computation.""" import pandas as pd import pytest from studypulse.metrics import ( compute_descriptive_stats, compute_risk_flags, compute_risk_score, get_top_risks, ) @pytest.fixture def sample_df() -> pd.DataFrame: """Create sample DataFrame for testing.""" return pd.DataFrame( { "student_id": ["STU001", "STU002", "STU003"], "student_name": ["Alice", "Bob", "Charlie"], "assignment_score": [85.0, 45.0, 95.0], "assignments_submitted": [8, 4, 9], "attendance_rate": [0.9, 0.6, 0.98], "days_since_last_submission": [3, 18, 1], "days_to_deadline": [5, -3, 7], } ) def test_compute_descriptive_stats(sample_df: pd.DataFrame) -> None: """Test descriptive stats computation.""" stats = compute_descriptive_stats(sample_df) assert "total_students" in stats assert stats["total_students"] == 3 assert "mean_assignment_score" in stats assert "mean_attendance_rate" in stats assert "low_score_count" in stats def test_compute_risk_flags(sample_df: pd.DataFrame) -> None: """Test risk flag computation.""" df_with_flags = compute_risk_flags(sample_df) assert "risk_flag" in df_with_flags.columns # STU002 should be flagged (low score, low attendance, inactive, behind deadline) assert df_with_flags.loc[df_with_flags["student_id"] == "STU002", "risk_flag"].iloc[0] == 1 # STU003 should not be flagged (high score, good attendance) assert df_with_flags.loc[df_with_flags["student_id"] == "STU003", "risk_flag"].iloc[0] == 0 def test_compute_risk_score(sample_df: pd.DataFrame) -> None: """Test risk score computation.""" df_with_score = compute_risk_score(sample_df) assert "risk_score" in df_with_score.columns # Risk scores should be between 0 and 100 assert df_with_score["risk_score"].min() >= 0 assert df_with_score["risk_score"].max() <= 100 # Higher risk students should have higher scores assert ( df_with_score.loc[df_with_score["student_id"] == "STU002", "risk_score"].iloc[0] > df_with_score.loc[df_with_score["student_id"] == "STU003", "risk_score"].iloc[0] ) def test_get_top_risks(sample_df: pd.DataFrame) -> None: """Test top risks retrieval.""" df_with_score = compute_risk_score(sample_df) top_risks = get_top_risks(df_with_score, top_n=2) assert len(top_risks) == 2 assert "risk_score" in top_risks.columns # Should be sorted descending by risk_score assert top_risks["risk_score"].iloc[0] >= top_risks["risk_score"].iloc[1] # STU002 should be in top risks assert "STU002" in top_risks["student_id"].values def test_metrics_stable_ordering(sample_df: pd.DataFrame) -> None: """Test that metrics return stable ordering.""" df_with_score1 = compute_risk_score(sample_df) df_with_score2 = compute_risk_score(sample_df) # Risk scores should be deterministic pd.testing.assert_frame_equal(df_with_score1, df_with_score2)