From 96e2a13ffee4428880a7ace6af2b9441f38a0ade Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 16 Dec 2022 23:26:22 -0300 Subject: [PATCH] Tests unitaires yaml: reset sequences to get same ids --- app/__init__.py | 20 +++++++++++++++++++ tests/unit/setup_test_yaml.py | 37 +++++++++++------------------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index ecbae300..6946415f 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -465,6 +465,26 @@ def truncate_database(): except: db.session.rollback() raise + # Remet les compteurs (séquences sql) à zéro + db.session.execute( + """ + CREATE OR REPLACE FUNCTION reset_sequences(username IN VARCHAR) RETURNS void AS $$ + DECLARE + statements CURSOR FOR + SELECT sequence_name + FROM information_schema.sequences + ORDER BY sequence_name ; + BEGIN + FOR stmt IN statements LOOP + EXECUTE 'ALTER SEQUENCE ' || quote_ident(stmt.sequence_name) || ' RESTART;'; + END LOOP; + END; + $$ LANGUAGE plpgsql; + + SELECT reset_sequences('scodoc'); + """ + ) + db.session.commit() def clear_scodoc_cache(): diff --git a/tests/unit/setup_test_yaml.py b/tests/unit/setup_test_yaml.py index 835b74a5..c4ad34f8 100644 --- a/tests/unit/setup_test_yaml.py +++ b/tests/unit/setup_test_yaml.py @@ -166,33 +166,18 @@ def _un_semestre( a_user = User.query.first() formsemestre.responsables = [a_user] db.session.add(formsemestre) - # Ajoute pour chaque UE une ressource avec un coef vers cette UE - added_ressources = set() - for parcour in parcours + [None]: - for ue in formation.query_ues_parcour(parcour): - ressource = ( - Module.query.filter_by( - formation=formation, - semestre_id=1, - module_type=scu.ModuleType.RESSOURCE, - ) - .join(ModuleUECoef) - .filter_by(ue=ue) - .first() - ) - if ressource is not None: - if ressource.id not in added_ressources: - modimpl = ModuleImpl(module=ressource, responsable_id=a_user.id) - db.session.add(modimpl) - formsemestre.modimpls.append(modimpl) - added_ressources.add(ressource.id) + # Ajoute tous les modules du semestre sans parcours OU avec l'un des parcours indiqués + sem_parcours_ids = {p.id for p in parcours} + modules = [ + m + for m in formsemestre.formation.modules.filter_by(semestre_id=semestre_id) + if (not m.parcours) or ({p.id for p in m.parcours} & sem_parcours_ids) + ] + for module in modules: + modimpl = ModuleImpl(module=module, responsable_id=a_user.id) + db.session.add(modimpl) + formsemestre.modimpls.append(modimpl) - # Ajoute la première SAE - sae = formation.modules.filter_by( - semestre_id=1, module_type=scu.ModuleType.SAE - ).first() - modimpl = ModuleImpl(module=sae, responsable_id=a_user.id) - formsemestre.modimpls.append(modimpl) # Crée une évaluation dans chaque module create_evaluations(formsemestre)