From b8767c753602de0e278f7082febc302189f65e9e Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 30 Jul 2023 21:48:51 +0200 Subject: [PATCH] Test unitaire ScoDocSiteConfig. Correction d'un mini bug. --- app/models/config.py | 2 +- tests/unit/test_site_config.py | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_site_config.py diff --git a/app/models/config.py b/app/models/config.py index 27f86127..ca1af288 100644 --- a/app/models/config.py +++ b/app/models/config.py @@ -253,7 +253,7 @@ class ScoDocSiteConfig(db.Model): cfg = ScoDocSiteConfig.query.filter_by(name=name).first() if cfg is None: return default - return cfg.value or "" + return cls.NAMES.get(name, lambda x: x)(cfg.value or "") @classmethod def set(cls, name: str, value: str) -> bool: diff --git a/tests/unit/test_site_config.py b/tests/unit/test_site_config.py new file mode 100644 index 00000000..92fb5e40 --- /dev/null +++ b/tests/unit/test_site_config.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- + +"""Test ScoDocSiteConfig (paramétrage général) + + +Utiliser comme: + pytest tests/unit/test_site_config.py + +""" + +from app.models import ScoDocSiteConfig +from app.comp.bonus_spo import BonusIUTRennes1 +from app.scodoc import sco_utils as scu + + +def test_scodoc_site_config(test_client): + """Classe pour paramètres généraux""" + cfg = ScoDocSiteConfig.get_dict() + assert cfg == {} # aucune valeur au départ + try: + ScoDocSiteConfig.set_bonus_sport_class("invalid") + assert False # la ligne precédente doit lancer une exception + except NameError: + pass + bonus_sport = "bonus_iut_rennes1" + ScoDocSiteConfig.set_bonus_sport_class(bonus_sport) + cfg = ScoDocSiteConfig.get_dict() + assert tuple(ScoDocSiteConfig.get_dict().values()) == (bonus_sport,) + assert ScoDocSiteConfig.get_bonus_sport_class_name() == bonus_sport + assert ScoDocSiteConfig.get_bonus_sport_class() == BonusIUTRennes1 + bonus_sport_class_names = ScoDocSiteConfig.get_bonus_sport_class_names() + assert isinstance(bonus_sport_class_names, list) + assert "" in bonus_sport_class_names + assert bonus_sport in bonus_sport_class_names + assert len(bonus_sport_class_names) > 5 + assert all([x == "" or x.startswith("bonus_") for x in bonus_sport_class_names]) + apo_dict = ScoDocSiteConfig.get_codes_apo_dict() + assert isinstance(apo_dict, dict) + assert "ABAN" in apo_dict + assert "ADM" in apo_dict + assert len(apo_dict) > 5 + ScoDocSiteConfig.set_code_apo("ADSUP", "AZERTY") + assert ScoDocSiteConfig.get_codes_apo_dict()["ADSUP"] == "AZERTY" + assert ScoDocSiteConfig.get_code_apo("ADSUP") == "AZERTY" + assert ScoDocSiteConfig.get("azerty") == "" # default empty string + assert ScoDocSiteConfig.set("azerty", 99) + assert ScoDocSiteConfig.get("azerty") == "99" # converted to string + assert "azerty" in ScoDocSiteConfig.get_dict() + assert ScoDocSiteConfig.set("always_require_ine", 99) + # Converti en bool car déclaré dans NAMES: + assert ScoDocSiteConfig.get_dict()["always_require_ine"] == True + assert ScoDocSiteConfig.get("always_require_ine") == True + # + assert ( + ScoDocSiteConfig.get_month_debut_annee_scolaire() + == scu.MONTH_DEBUT_ANNEE_SCOLAIRE + )