File size: 7,270 Bytes
d3a6aa2 | 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import pandas as pd
import pytest
from app.main import depart, inconsistency, promotion, developpement, interpret_shap
import os
# ---------------------------------------------------------------------------
# depart
# ---------------------------------------------------------------------------
def test_depart():
assert depart(0) == "The staff has a LOW probability of resigning"
assert depart(1) == "The staff has a HIGH probability of resigning"
# ---------------------------------------------------------------------------
# inconsistency
# The function is called via df.apply(inconsistency, axis=1), so each input
# is a pandas Series representing a single row.
# ---------------------------------------------------------------------------
class TestInconsistency:
# --- Commercial ---
def test_commercial_marketing_is_consistent(self):
row = pd.Series({"departement": "Commercial", "domaine_etude": "Marketing"})
assert inconsistency(row) == 0
def test_commercial_infra_is_inconsistent(self):
row = pd.Series({"departement": "Commercial", "domaine_etude": "Infra & Cloud"})
assert inconsistency(row) == 1
def test_commercial_rh_is_inconsistent(self):
row = pd.Series({"departement": "Commercial", "domaine_etude": "Ressources Humaines"})
assert inconsistency(row) == 1
def test_commercial_transformation_is_inconsistent(self):
row = pd.Series({"departement": "Commercial", "domaine_etude": "Transformation Digitale"})
assert inconsistency(row) == 1
# --- Consulting ---
def test_consulting_infra_is_consistent(self):
row = pd.Series({"departement": "Consulting", "domaine_etude": "Infra & Cloud"})
assert inconsistency(row) == 0
def test_consulting_transformation_is_consistent(self):
row = pd.Series({"departement": "Consulting", "domaine_etude": "Transformation Digitale"})
assert inconsistency(row) == 0
def test_consulting_marketing_is_inconsistent(self):
row = pd.Series({"departement": "Consulting", "domaine_etude": "Marketing"})
assert inconsistency(row) == 1
def test_consulting_rh_is_inconsistent(self):
row = pd.Series({"departement": "Consulting", "domaine_etude": "Ressources Humaines"})
assert inconsistency(row) == 1
# --- Ressources Humaines ---
def test_rh_rh_is_consistent(self):
row = pd.Series({"departement": "Ressources Humaines", "domaine_etude": "Ressources Humaines"})
assert inconsistency(row) == 0
def test_rh_entrepreneuriat_is_consistent(self):
row = pd.Series({"departement": "Ressources Humaines", "domaine_etude": "Entrepreunariat"})
assert inconsistency(row) == 0
def test_rh_marketing_is_inconsistent(self):
row = pd.Series({"departement": "Ressources Humaines", "domaine_etude": "Marketing"})
assert inconsistency(row) == 1
def test_rh_infra_is_inconsistent(self):
row = pd.Series({"departement": "Ressources Humaines", "domaine_etude": "Infra & Cloud"})
assert inconsistency(row) == 1
# ---------------------------------------------------------------------------
# promotion
# Returns 1 (stagnant) only when BOTH values are strictly greater than 4.
# ---------------------------------------------------------------------------
class TestPromotion:
def test_both_above_4_is_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 5, "annees_depuis_la_derniere_promotion": 5})
assert promotion(row) == 1
def test_large_values_are_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 10, "annees_depuis_la_derniere_promotion": 10})
assert promotion(row) == 1
def test_manager_years_exactly_4_not_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 4, "annees_depuis_la_derniere_promotion": 5})
assert promotion(row) == 0
def test_promotion_years_exactly_4_not_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 5, "annees_depuis_la_derniere_promotion": 4})
assert promotion(row) == 0
def test_both_below_threshold_not_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 2, "annees_depuis_la_derniere_promotion": 2})
assert promotion(row) == 0
def test_only_manager_years_high_not_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 6, "annees_depuis_la_derniere_promotion": 1})
assert promotion(row) == 0
def test_only_promotion_years_high_not_stagnant(self):
row = pd.Series({"annes_sous_responsable_actuel": 1, "annees_depuis_la_derniere_promotion": 6})
assert promotion(row) == 0
# ---------------------------------------------------------------------------
# developpement
# Returns 0 for new employees (tenure == 0), 1 when tenure >= 2 with <= 1
# training, 0 otherwise (sufficient training, or tenure == 1 year).
# ---------------------------------------------------------------------------
class TestDeveloppement:
def test_new_employee_returns_0(self):
row = pd.Series({"annees_dans_l_entreprise": 0, "nb_formations_suivies": 0})
assert developpement(row) == 0
def test_one_year_tenure_returns_0(self):
# tenure == 1 meets neither condition β 0
row = pd.Series({"annees_dans_l_entreprise": 1, "nb_formations_suivies": 0})
assert developpement(row) == 0
def test_two_years_no_training_is_stagnant(self):
row = pd.Series({"annees_dans_l_entreprise": 2, "nb_formations_suivies": 0})
assert developpement(row) == 1
def test_two_years_one_training_is_stagnant(self):
row = pd.Series({"annees_dans_l_entreprise": 2, "nb_formations_suivies": 1})
assert developpement(row) == 1
def test_two_years_two_trainings_not_stagnant(self):
row = pd.Series({"annees_dans_l_entreprise": 2, "nb_formations_suivies": 2})
assert developpement(row) == 0
def test_long_tenure_no_training_is_stagnant(self):
row = pd.Series({"annees_dans_l_entreprise": 10, "nb_formations_suivies": 0})
assert developpement(row) == 1
def test_long_tenure_sufficient_training_not_stagnant(self):
row = pd.Series({"annees_dans_l_entreprise": 10, "nb_formations_suivies": 5})
assert developpement(row) == 0
def test_interpret_shap():
assert interpret_shap(0, 0.5) == "Primary driver β increases resignation risk"
assert interpret_shap(0, -0.5) == "Primary driver β decreases resignation risk"
assert interpret_shap(1, 0.5) == "Strong factor β increases resignation risk"
assert interpret_shap(1, -0.5) == "Strong factor β decreases resignation risk"
assert interpret_shap(2, 0.5) == "Moderate factor β increases resignation risk"
assert interpret_shap(2, -0.5) == "Moderate factor β decreases resignation risk"
assert interpret_shap(3, 0.5) == "Contributing factor β increases resignation risk"
assert interpret_shap(3, -0.5) == "Contributing factor β decreases resignation risk"
assert interpret_shap(4, 0.5) == "Notable factor β increases resignation risk"
assert interpret_shap(4, -0.5) == "Notable factor β decreases resignation risk"
|