Repository Public MIT Research preview
1"""Tests du laboratoire S03 : le corrigé réussit, le point de départ échoue proprement, les données concordent."""2from __future__ import annotations34import subprocess5import sys6from pathlib import Path78SESSION = Path(__file__).resolve().parents[2]91011def lancer(dossier: str, script: str = "verifier.py") -> subprocess.CompletedProcess:12 return subprocess.run([sys.executable, script], cwd=SESSION / "labs" / dossier, capture_output=True, text=True)131415def test_corrige_reussit():16 r = lancer("solution")17 assert r.returncode == 0, r.stdout + r.stderr18 assert "4/4 étapes obligatoires réussies" in r.stdout19 assert "[ RÉUSSI ] Défi — poids_avec_position" in r.stdout202122def test_point_de_depart_echoue_sans_planter():23 r = lancer("starter")24 assert r.returncode == 125 assert r.stdout.count("[ À FAIRE ]") == 4 and "Traceback" not in r.stderr26 visualisation = lancer("starter", "mini_attention.py")27 assert visualisation.returncode == 0 and "Pas encore prêt" in visualisation.stdout28 assert "Traceback" not in visualisation.stderr293031def test_point_de_depart_ne_contient_pas_le_corrige():32 depart = (SESSION / "labs" / "starter" / "mini_attention.py").read_text(encoding="utf-8")33 assert depart.count("raise NotImplementedError") == 434 assert "def poids_avec_position" not in depart35 assert "math.exp(s) for" not in depart363738def test_visualisation_du_corrige_concorde_avec_les_consignes():39 """Les valeurs annoncées dans labs/instructions.md et labs/rubric.md sont celles du corrigé."""40 r = lancer("solution", "mini_attention.py")41 assert r.returncode == 0, r.stderr42 for ligne in (" avocat 0.67 ++++ 0.09 .... 0.24 ::::",43 " nouveau vecteur de « avocat » : (1.58, 0.24) [droit, cuisine]",44 " nouveau vecteur de « avocat » : (0.24, 1.58) [droit, cuisine]",45 " cabinet 0.12 .... 0.88 **** masqué"):46 assert ligne in r.stdout, ligne47 consignes = (SESSION / "labs" / "instructions.md").read_text(encoding="utf-8")48 for ligne in (" avocat 0.67 ++++ 0.09 .... 0.24 ::::", "Résultat : 4/4 étapes obligatoires réussies."):49 assert ligne in consignes, ligne505152def test_valeurs_du_defi_et_de_la_grille():53 sys.path.insert(0, str(SESSION / "labs" / "solution"))54 try:55 import importlib56 m = importlib.import_module("mini_attention")57 f = importlib.import_module("fiches")58 finally:59 sys.path.pop(0)6061 def cas(phrase, pente):62 poids = m.poids_avec_position(f.vecteurs(phrase, "requete"), f.vecteurs(phrase, "cle"), pente)63 z = m.nouveaux_vecteurs(poids, f.vecteurs(phrase, "valeur"))[-1]64 return [round(p, 2) for p in poids[-1]], tuple(round(x, 2) for x in z)6566 assert cas(f.PHRASE_DROIT, 1.0) == ([0.24, 0.09, 0.67], (1.15, 0.67))67 assert cas(f.PHRASE_MELANGEE, 1.0) == ([0.02, 0.49, 0.49], (1.46, 0.49))68 assert cas(f.PHRASE_DROIT, 0.5) == ([0.45, 0.1, 0.45], (1.35, 0.45))69 assert cas(f.PHRASE_MELANGEE, 0.5) == ([0.05, 0.59, 0.36], (1.54, 0.36))70 assert cas(f.PHRASE_DROIT, 2.0) == ([0.05, 0.05, 0.91], (1.0, 0.91))71 assert cas(f.PHRASE_MELANGEE, 2.0) == ([0.0, 0.27, 0.73], (1.26, 0.73))72 # Parcours A, étape 4 : « d' salade avocat ».73 phrase = ["d'", "salade", "avocat"]74 poids = m.poids_attention(f.vecteurs(phrase, "requete"), f.vecteurs(phrase, "cle"))75 assert [round(p, 2) for p in poids[-1]] == [0.09, 0.67, 0.24]76 z = m.nouveaux_vecteurs(poids, f.vecteurs(phrase, "valeur"))77 assert tuple(round(x, 2) for x in z[-1]) == (0.24, 1.58)78 # Parcours A, étape 3 : les trois lignes de « salade d' avocat ».79 poids = m.poids_attention(f.vecteurs(f.PHRASE_CUISINE, "requete"), f.vecteurs(f.PHRASE_CUISINE, "cle"))80 z = m.nouveaux_vecteurs(poids, f.vecteurs(f.PHRASE_CUISINE, "valeur"))81 assert [tuple(round(x, 2) for x in v) for v in z] == [(0.0, 2.0), (0.0, 1.0), (0.24, 1.58)]828384def test_memes_donnees_partout():85 sys.path.insert(0, str(SESSION / "demos" / "src"))86 from atlas_s03 import donnees8788 for dossier in ("starter", "solution"):89 espace: dict = {}90 exec((SESSION / "labs" / dossier / "fiches.py").read_text(encoding="utf-8"), espace)91 assert espace["FICHES"] == donnees.FICHES92 assert espace["PHRASE_DROIT"] == donnees.PHRASE_DROIT93 assert espace["PHRASE_CUISINE"] == donnees.PHRASE_CUISINE94 assert espace["PHRASE_MELANGEE"] == donnees.PHRASE_MELANGEE95 for fichier in ("verifier.py", "fiches.py"):96 assert (SESSION / "labs" / "starter" / fichier).read_text() == (SESSION / "labs" / "solution" / fichier).read_text()97