From 2f05e081ee6c77e6d51a8da78ce9be88182344e7 Mon Sep 17 00:00:00 2001 From: Jean-Marie Place Date: Thu, 20 Jul 2023 18:06:12 +0200 Subject: [PATCH] affine affichege des bonus/malus --- app/but/bulletin_but_pdf.py | 43 ++++++++++------ tests/unit/test_bulletin_bonus.py | 82 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 tests/unit/test_bulletin_bonus.py diff --git a/app/but/bulletin_but_pdf.py b/app/but/bulletin_but_pdf.py index 0d5b6b2df..ce215c14f 100644 --- a/app/but/bulletin_but_pdf.py +++ b/app/but/bulletin_but_pdf.py @@ -212,6 +212,34 @@ class BulletinGeneratorStandardBUT(BulletinGeneratorStandard): else: self.ue_std_rows(rows, ue, title_bg) + @staticmethod + def affichage_bonus_malus(ue: dict) -> list: + fields_bmr = [] + # lecture des bonus sport culture et malus (ou bonus autre) (0 si valeur non numérique) + try: + bonus_sc = float(ue.get("bonus", 0.0)) or 0 + except ValueError: + bonus_sc = 0 + try: + malus = float(ue.get("malus", 0.0)) or 0 + except ValueError: + malus = 0 + # Calcul de l affichage + if malus < 0: + if bonus_sc > 0: + fields_bmr.append(f"Bonus sport/culture: {bonus_sc}") + fields_bmr.append(f"Bonus autres: {-malus}") + else: + fields_bmr.append(f"Bonus: {-malus}") + elif malus > 0: + if bonus_sc > 0: + fields_bmr.append(f"Bonus: {bonus_sc}") + fields_bmr.append(f"Malus: {malus}") + else: + if bonus_sc > 0: + fields_bmr.append(f"Bonus: {bonus_sc}") + return fields_bmr + def ue_std_rows(self, rows: list, ue: dict, title_bg: tuple): "Lignes décrivant une UE standard dans la table de synthèse" # 2eme ligne titre UE (bonus/malus/ects) @@ -220,20 +248,7 @@ class BulletinGeneratorStandardBUT(BulletinGeneratorStandard): else: ects_txt = "" # case Bonus/Malus/Rang "bmr" - fields_bmr = [] - try: - value = float(ue.get("bonus", 0.0)) - if value != 0: - fields_bmr.append(f"Bonus: {ue['bonus']}") - except ValueError: - pass - try: - value = float(ue.get("malus", 0.0)) - if value != 0: - fields_bmr.append(f"Malus: {ue['malus']}") - except ValueError: - pass - + fields_bmr = BulletinGeneratorStandardBUT.affichage_bonus_malus(ue) moy_ue = ue.get("moyenne", "-") if isinstance(moy_ue, dict): # UE non capitalisées if self.preferences["bul_show_ue_rangs"]: diff --git a/tests/unit/test_bulletin_bonus.py b/tests/unit/test_bulletin_bonus.py new file mode 100644 index 000000000..21587f754 --- /dev/null +++ b/tests/unit/test_bulletin_bonus.py @@ -0,0 +1,82 @@ +"""Tests unitaires : bulletins de notes + +Utiliser comme: + pytest tests/unit/test_bulletin_bonus.py + +""" +from app.but.bulletin_but_pdf import BulletinGeneratorStandardBUT + + +def test_nobonus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({}) == [] + + +def test_bonus_sport_nul(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({"bonus": 0}) == [] + + +def test_malus_nul(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({"malus": 0}) == [] + + +def test_bonus_et_malus_nuls(): + assert ( + BulletinGeneratorStandardBUT.affichage_bonus_malus({"bonus": 0, "malus": 0}) + == [] + ) + + +def test_vrai_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({"malus": 0.1}) == [ + "Malus: 0.1" + ] + + +def test_bonus_sport_et_vrai_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus( + {"malus": 0.12, "bonus": 0.23} + ) == [ + "Bonus: 0.23", + "Malus: 0.12", + ] + + +def test_bonus_sport_seul(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({"bonus": 0.5}) == [ + "Bonus: 0.5" + ] + + +def test_bonus_sport_nul_et_vrai_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus( + {"bonus": 0, "malus": 0.5} + ) == ["Malus: 0.5"] + + +def test_bonus_sport_et_malus_nul(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus( + {"bonus": 0.5, "malus": 0} + ) == [ + "Bonus: 0.5", + ] + + +def test_faux_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus({"malus": -0.6}) == [ + "Bonus: 0.6" + ] + + +def test_sport_nul_faux_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus( + {"bonus": 0, "malus": -0.6} + ) == ["Bonus: 0.6"] + + +def test_bonus_sport_et_faux_malus(): + assert BulletinGeneratorStandardBUT.affichage_bonus_malus( + {"bonus": 0.3, "malus": -0.6} + ) == [ + "Bonus sport/culture: 0.3", + "Bonus autres: 0.6", + ]