Files
tableau-de-bord/tests/test_travel_calc.py

91 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from app.business.travel_calc import (
compute_km_for_entry,
compute_co2_grams,
compute_frais_reels,
)
VEHICLES = {
"familiale": {"co2_per_km": 142, "cv": 5, "type": "moteur"},
"citadine": {"co2_per_km": 0, "cv": 3, "type": "moteur"},
"velo": {"co2_per_km": 0, "type": "velo"},
}
JOURNEYS = {
"moteur_seul": {"distances": {"moteur": 25}},
"moteur_velo": {"distances": {"moteur": 14, "velo": 8}},
"velo_seul": {"distances": {"velo": 24}},
}
TRANCHES_CV3 = [
{"km_max": 5000, "taux": 0.529, "forfait": 0},
{"km_max": 20000, "taux": 0.316, "forfait": 1065},
{"km_max": 0, "taux": 0.370, "forfait": 0},
]
TRANCHES_CV5 = [
{"km_max": 5000, "taux": 0.636, "forfait": 0},
{"km_max": 20000, "taux": 0.357, "forfait": 1395},
{"km_max": 0, "taux": 0.427, "forfait": 0},
]
def test_compute_km_moteur_seul_avec_vehicule():
km = compute_km_for_entry("moteur_seul", JOURNEYS, "familiale")
assert km == {"familiale": 25}
def test_compute_km_moteur_velo_avec_vehicule():
km = compute_km_for_entry("moteur_velo", JOURNEYS, "citadine")
assert km == {"citadine": 14, "velo": 8}
def test_compute_km_velo_seul():
km = compute_km_for_entry("velo_seul", JOURNEYS)
assert km == {"velo": 24}
def test_compute_km_sans_vehicule_moteur_ignore():
km = compute_km_for_entry("moteur_seul", JOURNEYS, None)
assert km == {}
def test_compute_km_no_journey():
assert compute_km_for_entry(None, JOURNEYS) == {}
def test_compute_co2_familiale():
assert compute_co2_grams({"familiale": 25}, VEHICLES) == 25 * 142
def test_compute_co2_citadine_electrique():
assert compute_co2_grams({"citadine": 25}, VEHICLES) == 0
def test_compute_co2_velo():
assert compute_co2_grams({"velo": 24}, VEHICLES) == 0
def test_frais_reels_tranche1():
result = compute_frais_reels(2000, TRANCHES_CV5)
assert abs(result - 1272.0) < 0.01 # 2000 × 0.636
def test_frais_reels_tranche2():
result = compute_frais_reels(10000, TRANCHES_CV5)
assert abs(result - 4965.0) < 0.01 # 10000 × 0.357 + 1395
def test_frais_reels_tranche3():
result = compute_frais_reels(25000, TRANCHES_CV5)
assert abs(result - 10675.0) < 0.01 # 25000 × 0.427
def test_frais_reels_electrique_majoration_20_pct():
result = compute_frais_reels(2000, TRANCHES_CV3, electric=True)
assert abs(result - 1269.6) < 0.01 # 2000 × 0.529 × 1.2
def test_frais_reels_non_electrique_sans_majoration():
result = compute_frais_reels(2000, TRANCHES_CV3, electric=False)
assert abs(result - 1058.0) < 0.01 # 2000 × 0.529