Repository Public MIT Research preview
1"""Tests de la démonstration S14 : chaque chiffre affiché dans les diapositives et les figures est vérifié ici."""2from __future__ import annotations34import subprocess5import sys6from pathlib import Path78SESSION = Path(__file__).resolve().parents[2]9sys.path.insert(0, str(SESSION / "demos" / "src"))1011from atlas_s14.consignes import examiner_les_deux, note # noqa: E40212from atlas_s14.contexte import estimer_jetons, postes, scenarios # noqa: E40213from atlas_s14.harnais import (decider, deux_appels_de, diff_unifie, ecart_de_mars, mesurer, # noqa: E40214 tout_evaluer)15from atlas_s14.projet import CANDIDATS, PERIMETRE, VERSIONS, contenu, hors_perimetre # noqa: E4021617BILANS = {b.nom: b for b in tout_evaluer()}18SCRIPTS = SESSION / "demos" / "src" / "atlas_s14"192021def test_quatre_versions_dans_l_ordre():22 assert list(BILANS) == [v.nom for v in VERSIONS] == ["original", "candidat A", "candidat B", "corrigé"]23 assert PERIMETRE == ("boutique/",)242526def test_fichier_de_consignes_faible():27 """Diapositives S14-D08, S14-D09 et S14-D28 : 0 sur 6, avec le détail de chaque défaut."""28 faible, _ = examiner_les_deux()29 assert note(faible) == 0 and [c.reussi for c in faible] == [False] * 630 details = {c.cle: c.detail for c in faible}31 assert details["commandes"] == "0 commande, dont 0 de test"32 assert details["secrets"] == "ligne 12" and details["contradictions"] == "lignes 6 et 7"33 assert details["precision"] == "4 règles vagues" and details["termine"] == "aucune rubrique"343536def test_bon_fichier_de_consignes():37 _, bonne = examiner_les_deux()38 assert note(bonne) == 639 details = {c.cle: c.detail for c in bonne}40 assert details["commandes"] == "4 commandes, dont 2 de test"41 assert details["limites"] == "2 règles" and details["termine"] == "3 critères"424344def test_structure_mesuree():45 """Diapositives S14-D02 et S14-D14, figure S14-F05 : 77 lignes au départ, 14 à l'arrivée."""46 s = {nom: b.structure for nom, b in BILANS.items()}47 assert (s["original"].fonctions, s["original"].plus_longue, s["original"].calculs_de_taxe) == (1, 77, 2)48 assert s["original"].nom_plus_longue == "produire" and s["original"].instructions_global == 149 assert (s["candidat A"].fonctions, s["candidat A"].plus_longue, s["candidat A"].instructions_global) == (6, 14, 1)50 assert (s["candidat B"].fonctions, s["candidat B"].plus_longue, s["candidat B"].instructions_global) == (7, 14, 0)51 assert (s["corrigé"].fonctions, s["corrigé"].plus_longue, s["corrigé"].calculs_de_taxe) == (7, 14, 1)52 assert s["corrigé"].instructions_global == 053 assert mesurer("TAUX = 1\ndef f(x):\n return x * TAUX\n").calculs_de_taxe == 1545556def test_original_fonctionne_et_porte_le_bogue():57 b = BILANS["original"]58 assert b.base == (4, 4) and b.caracterisation == (6, 6) and not b.deux_appels_identiques59 assert (b.verdict, b.modifies, b.hors_perimetre) == ("DÉPART", (), ())606162def test_candidat_a_preserve_le_comportement():63 b = BILANS["candidat A"]64 assert b.modifies == ("boutique/rapport.py",) and b.hors_perimetre == ()65 assert b.base == (4, 4) and b.caracterisation == (6, 6) and not b.deux_appels_identiques66 assert (b.verdict, b.motif) == ("ACCEPTER", "comportement préservé ; bogue latent à traiter")67 assert ecart_de_mars(CANDIDATS[0]) == []686970def test_candidat_b_n_est_arrete_que_par_la_caracterisation():71 b = BILANS["candidat B"]72 assert b.modifies == ("boutique/rapport.py",) and b.hors_perimetre == ()73 assert b.base == (4, 4) and b.deux_appels_identiques # ni les tests de base ni le test des deux appels74 assert b.caracterisation == (4, 6)75 assert b.echecs_caracterisation == ("test_rapport_csv_de_mars", "test_rapport_texte_de_mars")76 assert (b.verdict, b.motif) == ("REFUSER", "change le comportement caractérisé")777879def test_l_ecart_du_candidat_b_est_d_un_cent():80 """Diapositives S14-D02 et S14-D26 : 50,53 devient 50,54 ; 102,14 devient 102,15."""81 assert ecart_de_mars(CANDIDATS[1]) == [("livres,18,336.88,50.53,387.41", "livres,18,336.88,50.54,387.42"),82 ("TOTAL,57,681.02,102.14,783.16", "TOTAL,57,681.02,102.15,783.17")]838485def test_corrige():86 b = BILANS["corrigé"]87 assert b.base == (4, 4) and b.caracterisation == (6, 6) and b.deux_appels_identiques88 assert b.verdict == "RÉFÉRENCE" and b.hors_perimetre == ()89 assert set(b.modifies) == {"boutique/rapport.py", "tests/test_regression.py"}909192def test_bogue_des_deux_appels():93 """Diapositive S14-D27 : 57 articles au premier appel, 114 au second."""94 premier, second = deux_appels_de(VERSIONS[0])95 assert premier == "TOTAL,57,681.02,102.14,783.16" and second.startswith("TOTAL,114,")96 assert deux_appels_de(VERSIONS[3]) == (premier, premier)97 assert deux_appels_de(CANDIDATS[0])[1].startswith("TOTAL,114,")9899100def test_regle_de_verdict():101 assert decider((), (4, 4), (6, 6), True) == ("ACCEPTER", "comportement préservé")102 assert decider((), (4, 4), (6, 6), False)[0] == "ACCEPTER"103 assert decider(("attendu/rapport-mars.txt",), (4, 4), (6, 6), True) == ("REFUSER", "modifie un fichier hors périmètre")104 assert decider((), (3, 4), (6, 6), True) == ("REFUSER", "les tests de base échouent")105 assert decider((), (4, 4), (5, 6), True) == ("REFUSER", "change le comportement caractérisé")106 assert decider(("x",), (0, 4), (0, 6), False)[1] == "modifie un fichier hors périmètre" # le périmètre d'abord107108109def test_perimetre(tmp_path):110 assert all(hors_perimetre(v) == () for v in VERSIONS)111 from atlas_s14 import projet112 surcouche = tmp_path / "candidat-c"113 (surcouche / "attendu").mkdir(parents=True)114 (surcouche / "attendu" / "rapport-mars.csv").write_text("modifié\n", encoding="utf-8")115 (surcouche / "tests").mkdir()116 (surcouche / "tests" / "test_nouveau.py").write_text("", encoding="utf-8")117 fictif = projet.Version("candidat C", surcouche, True, True)118 assert hors_perimetre(fictif) == ("attendu/rapport-mars.csv",) # un nouveau fichier de tests est permis119120121def test_les_candidats_ne_changent_que_rapport_py():122 for version in CANDIDATS:123 assert diff_unifie(version).startswith("--- a/boutique/rapport.py\n+++ b/boutique/rapport.py\n")124 assert set(contenu(version)) == set(contenu(VERSIONS[0]))125 assert diff_unifie(VERSIONS[0]) == ""126127128def test_contexte_simule():129 """Figure S14-F03 et diapositives S14-D17, S14-D18, S14-D33."""130 assert estimer_jetons("chat stationnement") == 1 + 4 # règle du cours (S05)131 p = postes()132 assert p == {"consignes": 532, "tache_1": 3170, "note": 306, "tache_2": 2197}133 fin, meme, neuve = scenarios()134 assert (fin.total, meme.total, neuve.total) == (3702, 5899, 3035)135 assert (meme.part_de_la_tache, neuve.part_de_la_tache) == (37, 72)136 assert meme.historique == p["tache_1"] and neuve.historique == 0 and neuve.note == 306137 assert p["tache_1"] > 10 * p["note"] # S14-D18 : « dix fois moins »138139140def test_demo_s_execute_et_affiche_les_lignes_des_diapositives():141 sortie = subprocess.run([sys.executable, str(SCRIPTS / "demo.py")], capture_output=True, text=True,142 encoding="utf-8", check=True).stdout143 for ligne in (" commande de test à copier non oui",144 " note 0/6 6/6",145 " Fichier faible : contradiction, lignes 6 et 7 ; allure secrète, ligne 12 ;",146 " 4 règles vagues. La note juge la forme, pas la vérité des consignes.",147 " original 1 77 lignes 1 écrit 2 fois",148 " corrigé 7 14 lignes 0 écrit 1 fois",149 " original 4/4 6/6 rapports différents",150 " candidat A 4/4 6/6 rapports différents",151 " candidat B 4/4 4/6 même rapport (2 échecs)",152 " corrigé 4/4 6/6 même rapport",153 " candidat A ACCEPTER comportement préservé ; bogue latent à traiter",154 " candidat B REFUSER change le comportement caractérisé",155 " Total des taxes : 102.14 attendu, 102.15 obtenu ; écart de 0.01.",156 " tâche 2, même session 532 3170 0 2197 5899 37 %",157 " tâche 2, session neuve 532 0 306 2197 3035 72 %"):158 assert ligne in sortie, ligne159 assert max(len(ligne) for ligne in sortie.splitlines()) <= 80160 readme = (SESSION / "demos" / "README.md").read_text(encoding="utf-8")161 assert sortie.strip() in readme162163164def test_diffs_et_donnees_de_figures_a_jour():165 for script in ("generer_diffs.py", "exporter_donnees_figure.py"):166 assert subprocess.run([sys.executable, str(SCRIPTS / script), "--check"]).returncode == 0, script167168169def test_donnees_de_figures_contiennent_les_valeurs_evaluees():170 filet = (SESSION / "figures" / "src" / "S14-F05-donnees.tex").read_text(encoding="utf-8")171 for attendu in (r"\def\AtlasFigOriginalLongue{77}%", r"\def\AtlasFigCandidatBCarac{4/6}%",172 r"\def\AtlasFigCandidatBCaracOk{0}%", r"\def\AtlasFigCandidatAAppelsOk{0}%",173 r"\def\AtlasFigCandidatAVerdict{Accepter}%", r"\def\AtlasFigCandidatBVerdict{Refuser}%",174 r"\def\AtlasFigCorrigeAppels{identiques}%"):175 assert attendu in filet, attendu176 contexte = (SESSION / "figures" / "src" / "S14-F03-donnees.tex").read_text(encoding="utf-8")177 for attendu in (r"\def\AtlasFigCtxMemePart{37}%", r"\def\AtlasFigCtxNeuvePart{72}%",178 r"\def\AtlasFigCtxMemeHistorique{3170}%", r"\def\AtlasFigCtxNeuveNote{306}%",179 r"\def\AtlasFigCtxMemeFinTache{1.0000}%"):180 assert attendu in contexte, attendu181 for donnees in (filet, contexte):182 assert all(ligne.endswith("%") for ligne in donnees.splitlines())183184185def test_chiffres_des_diapositives():186 """Les nombres écrits en toutes lettres dans le diaporama correspondent aux valeurs calculées."""187 sections = "\n".join(f.read_text(encoding="utf-8") for f in sorted((SESSION / "slides" / "sections").glob("*.tex")))188 for fragment in ("passe de 77 lignes à 14", "affiche 102,15 \\$ de taxes", "il affichait 102,14 \\$",189 "4 commandes, dont 2 de test", "lignes 6 et 7", "4 règles vagues", "\\textbf{0 sur 6}",190 "1 fonction de 77 lignes, la taxe calculée à 2 endroits", "7 fonctions, la plus longue de 14 lignes",191 "50,54 \\$ de taxes au lieu de 50,53 \\$", "57 articles au premier, 114 au second",192 "37 \\% du contexte", "72 \\%", "11 tests"):193 assert fragment in sections, fragment194