From 642283c7d8dee268ad85c808ad706eeaf7ef914c Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 10 Aug 2021 09:10:36 +0200 Subject: [PATCH] pass unit test_formations --- app/scodoc/notesdb.py | 8 +++----- app/scodoc/sco_cache.py | 5 +++-- app/scodoc/sco_formsemestre.py | 4 ++-- app/scodoc/sco_formsemestre_edit.py | 23 +++++++++++++++++++++-- app/scodoc/sco_groups.py | 10 +++++----- app/scodoc/sco_parcours_dut.py | 8 ++++---- app/scodoc/sco_permissions_check.py | 2 +- tests/conftest.py | 1 + tests/unit/sco_fake_gen.py | 24 ++++++++++++++++-------- tests/unit/test_formations.py | 5 ----- 10 files changed, 56 insertions(+), 34 deletions(-) diff --git a/app/scodoc/notesdb.py b/app/scodoc/notesdb.py index 02d024a9aa..cc2925d6fe 100644 --- a/app/scodoc/notesdb.py +++ b/app/scodoc/notesdb.py @@ -246,12 +246,10 @@ def DBUpdateArgs(cnx, table, vals, where=None, commit=False, convert_empty_to_nu cnx.commit() -def DBDelete(cnx, table, colid, val, commit=False): +def DBDelete(cnx, table, oid, commit=False): cursor = cnx.cursor(cursor_factory=ScoDocCursor) try: - cursor.execute( - "delete from " + table + " where %s=%%(%s)s" % (colid, colid), {colid: val} - ) + cursor.execute("delete from " + table + " where id=%(oid)s", {"oid": oid}) except: cnx.commit() # get rid of this transaction raise # and re-raise exception @@ -315,7 +313,7 @@ class EditableTable(object): def delete(self, cnx, oid, commit=True): "delete tuple" - DBDelete(cnx, self.table_name, self.id_name, oid, commit=commit) + DBDelete(cnx, self.table_name, oid, commit=commit) def list( self, diff --git a/app/scodoc/sco_cache.py b/app/scodoc/sco_cache.py index c2352f95f9..19f3dd63b4 100644 --- a/app/scodoc/sco_cache.py +++ b/app/scodoc/sco_cache.py @@ -76,7 +76,7 @@ class ScoDocCache: @classmethod def _get_key(cls, oid): - return g.scodoc_dept + "_" + cls.prefix + "_" + oid + return g.scodoc_dept + "_" + cls.prefix + "_" + str(oid) @classmethod def get(cls, oid): @@ -175,7 +175,8 @@ class SemBulletinsPDFCache(ScoDocCache): """Clear cached pdf for all given formsemestres""" for version in scu.BULLETINS_VERSIONS: oids = [ - formsemestre_id + "_" + version for formsemestre_id in formsemestre_ids + str(formsemestre_id) + "_" + version + for formsemestre_id in formsemestre_ids ] cls.delete_many(oids) diff --git a/app/scodoc/sco_formsemestre.py b/app/scodoc/sco_formsemestre.py index 8cd91cd58c..3e321b551e 100644 --- a/app/scodoc/sco_formsemestre.py +++ b/app/scodoc/sco_formsemestre.py @@ -71,7 +71,7 @@ _formsemestreEditor = ndb.EditableTable( "date_fin": ndb.DateISOtoDMY, "gestion_compensation": str, "gestion_semestrielle": str, - "etat": str, + "etat": bool, "bul_hide_xml": str, }, input_formators={ @@ -79,7 +79,7 @@ _formsemestreEditor = ndb.EditableTable( "date_fin": ndb.DateDMYtoISO, "gestion_compensation": int, "gestion_semestrielle": int, - "etat": int, + "etat": bool, "bul_hide_xml": int, }, ) diff --git a/app/scodoc/sco_formsemestre_edit.py b/app/scodoc/sco_formsemestre_edit.py index cfd7d51c9e..39ca211a1c 100644 --- a/app/scodoc/sco_formsemestre_edit.py +++ b/app/scodoc/sco_formsemestre_edit.py @@ -1421,12 +1421,31 @@ def do_formsemestre_delete(context, formsemestre_id): req = "DELETE FROM sco_prefs WHERE formsemestre_id=%(formsemestre_id)s" cursor.execute(req, {"formsemestre_id": formsemestre_id}) # --- Suppression des groupes et partitions - req = "DELETE FROM group_membership WHERE group_id IN (SELECT gm.group_id FROM group_membership gm, partition p, group_descr gd WHERE gm.group_id = gd.group_id AND gd.partition_id = p.partition_id AND p.formsemestre_id=%(formsemestre_id)s)" + req = """DELETE FROM group_membership + WHERE group_id IN + (SELECT gm.group_id FROM group_membership gm, partition p, group_descr gd + WHERE gm.group_id = gd.id AND gd.partition_id = p.id + AND p.formsemestre_id=%(formsemestre_id)s) + """ cursor.execute(req, {"formsemestre_id": formsemestre_id}) - req = "DELETE FROM group_descr WHERE group_id IN (SELECT gd.group_id FROM group_descr gd, partition p WHERE gd.partition_id = p.partition_id AND p.formsemestre_id=%(formsemestre_id)s)" + req = """DELETE FROM group_descr + WHERE id IN + (SELECT gd.id FROM group_descr gd, partition p + WHERE gd.partition_id = p.id + AND p.formsemestre_id=%(formsemestre_id)s) + """ cursor.execute(req, {"formsemestre_id": formsemestre_id}) req = "DELETE FROM partition WHERE formsemestre_id=%(formsemestre_id)s" cursor.execute(req, {"formsemestre_id": formsemestre_id}) + # --- Responsables + req = """DELETE FROM notes_formsemestre_responsables + WHERE formsemestre_id=%(formsemestre_id)s""" + cursor.execute(req, {"formsemestre_id": formsemestre_id}) + # --- Etapes + req = """DELETE FROM notes_formsemestre_etapes + WHERE formsemestre_id=%(formsemestre_id)s""" + cursor.execute(req, {"formsemestre_id": formsemestre_id}) + # --- Destruction du semestre sco_formsemestre._formsemestreEditor.delete(cnx, formsemestre_id) diff --git a/app/scodoc/sco_groups.py b/app/scodoc/sco_groups.py index 798ede77ba..24a12e0483 100644 --- a/app/scodoc/sco_groups.py +++ b/app/scodoc/sco_groups.py @@ -185,7 +185,7 @@ def get_partition_groups(context, partition): """SELECT gd.id AS group_id, p.id AS partition_id, gd.*, p.* FROM group_descr gd, partition p WHERE gd.partition_id=%(partition_id)s - AND gd.partition_id=p.partition_id + AND gd.partition_id=p.id ORDER BY group_name """, partition, @@ -200,7 +200,7 @@ def get_default_group(formsemestre_id, fix_if_missing=False): FROM group_descr gd, partition p WHERE p.formsemestre_id=%(formsemestre_id)s AND p.partition_name is NULL - AND p.partition_id = gd.partition_id + AND p.id = gd.partition_id """, {"formsemestre_id": formsemestre_id}, ) @@ -230,10 +230,10 @@ def get_default_group(formsemestre_id, fix_if_missing=False): def get_sem_groups(context, formsemestre_id): """Returns groups for this sem (in all partitions).""" return ndb.SimpleDictFetch( - """SELECT gd.id AS group_id, p.id AS partition_id, gd.*, p.* + """SELECT gd.id AS group_id, p.id AS partition_id, gd.*, p.* FROM group_descr gd, partition p WHERE p.formsemestre_id=%(formsemestre_id)s - AND p.partition_id = gd.partition_id + AND p.id = gd.partition_id """, {"formsemestre_id": formsemestre_id}, ) @@ -244,7 +244,7 @@ def get_group_members(context, group_id, etat=None): Si etat, filtre selon l'état de l'inscription Trié par nom_usuel (ou nom) puis prénom """ - req = """SELECT i.id as etudid, i.*, a.*, gm.*, ins.etat + req = """SELECT i.id as etudid, i.*, a.*, gm.*, ins.etat FROM identite i, adresse a, group_membership gm, group_descr gd, partition p, notes_formsemestre_inscription ins WHERE i.id = gm.etudid diff --git a/app/scodoc/sco_parcours_dut.py b/app/scodoc/sco_parcours_dut.py index c2a84b41d4..5d5e640b0d 100644 --- a/app/scodoc/sco_parcours_dut.py +++ b/app/scodoc/sco_parcours_dut.py @@ -1094,13 +1094,13 @@ def list_formsemestre_utilisateurs_uecap(context, formsemestre_id): )[0] cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) cursor.execute( - """select sem.formsemestre_id - from notes_formsemestre sem, notes_formations F - where sem.formation_id = F.formation_id + """SELECT sem.id + FROM notes_formsemestre sem, notes_formations F + WHERE sem.formation_id = F.id and F.formation_code = %(formation_code)s and sem.semestre_id = %(semestre_id)s and sem.date_debut >= %(date_debut)s - and sem.formsemestre_id != %(formsemestre_id)s; + and sem.id != %(formsemestre_id)s; """, { "formation_code": F["formation_code"], diff --git a/app/scodoc/sco_permissions_check.py b/app/scodoc/sco_permissions_check.py index 9cb5838aa2..2647d34c72 100644 --- a/app/scodoc/sco_permissions_check.py +++ b/app/scodoc/sco_permissions_check.py @@ -176,7 +176,7 @@ def can_change_groups(formsemestre_id): context = None # XXX #context sem = sco_formsemestre.get_formsemestre(context, formsemestre_id) - if sem["etat"] != "1": + if not sem["etat"]: return False # semestre verrouillé if current_user.has_permission(Permission.ScoEtudChangeGroups): return True # admin, chef dept diff --git a/tests/conftest.py b/tests/conftest.py index 3e77cf572f..2b19624759 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,7 @@ def test_client(): admin_role = Role.query.filter_by(name="Admin").first() u.add_role(admin_role, "TEST00") db.session.add(u) + db.session.commit() ndb.set_sco_dept("TEST") # set db connection yield client # ndb.close_dept_connection() diff --git a/tests/unit/sco_fake_gen.py b/tests/unit/sco_fake_gen.py index 42c58452ef..3cdde226ad 100644 --- a/tests/unit/sco_fake_gen.py +++ b/tests/unit/sco_fake_gen.py @@ -9,14 +9,15 @@ facilement des tests ou de reproduire des bugs. """ from functools import wraps +import random import sys import string -import collections -import pprint -import random +import typing + import scodoc_manager from config import Config +from app.auth.models import User from app.scodoc import notesdb as ndb from app.scodoc import sco_codes_parcours from app.scodoc import sco_edit_formation @@ -70,6 +71,9 @@ class ScoFake(object): def __init__(self, verbose=True): self.verbose = verbose + self.default_user = User.query.filter_by(user_name="bach").first() + if not self.default_user: + raise ScoValueError('User test "bach" not found !') def log(self, msg): if self.verbose: @@ -231,8 +235,10 @@ class ScoFake(object): elt_sem_apo=None, elt_annee_apo=None, etapes=None, - responsables=("bach",), + responsables=None, # sequence of resp. ids ): + if responsables is None: + responsables = (self.default_user.id,) oid = sco_formsemestre.do_formsemestre_create(locals()) oids = sco_formsemestre.do_formsemestre_list( context, args={"formsemestre_id": oid} @@ -244,10 +250,12 @@ class ScoFake(object): @logging_meth def create_moduleimpl( self, - module_id=None, - formsemestre_id=None, - responsable_id=None, + module_id: int = None, + formsemestre_id: int = None, + responsable_id: typing.Optional[int] = None, ): + if not responsable_id: + responsable_id = self.default_user.id oid = sco_moduleimpl.do_moduleimpl_create(context, locals()) oids = sco_moduleimpl.do_moduleimpl_list( context, moduleimpl_id=oid @@ -299,7 +307,7 @@ class ScoFake(object): etud=None, note=None, comment=None, - uid="bach", + uid: typing.Optional[int] = None, ): return sco_saisie_notes._notes_add( context, diff --git a/tests/unit/test_formations.py b/tests/unit/test_formations.py index 274a75cf84..eeb4d86303 100644 --- a/tests/unit/test_formations.py +++ b/tests/unit/test_formations.py @@ -138,19 +138,16 @@ def test_formations(test_client): mi = G.create_moduleimpl( module_id=mod["module_id"], formsemestre_id=sem1["formsemestre_id"], - responsable_id="bach", ) mi2 = G.create_moduleimpl( module_id=mod2["module_id"], formsemestre_id=sem1["formsemestre_id"], - responsable_id="bach", ) mit = G.create_moduleimpl( module_id=modt["module_id"], formsemestre_id=sem2["formsemestre_id"], - responsable_id="bach", ) semt = G.create_formsemestre( @@ -163,7 +160,6 @@ def test_formations(test_client): mi3 = G.create_moduleimpl( module_id=mod3["module_id"], formsemestre_id=semt["formsemestre_id"], - responsable_id="bach", ) # --- Afficher la liste des formations @@ -379,7 +375,6 @@ def test_import_formation(test_client): mi = G.create_moduleimpl( module_id=mod["module_id"], formsemestre_id=sems[mod["semestre_id"] - 1]["formsemestre_id"], - responsable_id="bach", ) assert mi["ens"] == [] assert mi["module_id"] == mod["module_id"]