1
0
forked from ScoDoc/ScoDoc

Add unit test for sco_find_etud.

This commit is contained in:
Emmanuel Viennet 2023-10-23 23:50:13 +02:00
parent 9bc0111ceb
commit b78e1b8be4
5 changed files with 66 additions and 49 deletions

View File

@ -219,26 +219,27 @@ _identiteEditor = ndb.EditableTable(
"identite", "identite",
"etudid", "etudid",
( (
"etudid", "admission_id",
"nom", "boursier",
"nom_usuel",
"prenom",
"prenom_etat_civil",
"cas_id",
"cas_allow_login", "cas_allow_login",
"cas_allow_scodoc_login", "cas_allow_scodoc_login",
"civilite", # 'M", "F", or "X" "cas_id",
"civilite_etat_civil", "civilite_etat_civil",
"date_naissance", "civilite", # 'M", "F", or "X"
"lieu_naissance",
"dept_naissance",
"nationalite",
"statut",
"boursier",
"foto",
"photo_filename",
"code_ine", "code_ine",
"code_nip", "code_nip",
"date_naissance",
"dept_naissance",
"etudid",
"foto",
"lieu_naissance",
"nationalite",
"nom_usuel",
"nom",
"photo_filename",
"prenom_etat_civil",
"prenom",
"statut",
), ),
filter_dept=True, filter_dept=True,
sortkey="nom", sortkey="nom",
@ -300,7 +301,7 @@ def check_nom_prenom_homonyms(
prenom = prenom.lower().strip() prenom = prenom.lower().strip()
# Don't allow some special cars (eg used in sql regexps) # Don't allow some special cars (eg used in sql regexps)
if scu.FORBIDDEN_CHARS_EXP.search(nom) or scu.FORBIDDEN_CHARS_EXP.search(prenom): if scu.FORBIDDEN_CHARS_EXP.search(nom) or scu.FORBIDDEN_CHARS_EXP.search(prenom):
return False, 0 return False, []
# Liste homonymes (dans tous les départements): # Liste homonymes (dans tous les départements):
query = Identite.query.filter( query = Identite.query.filter(
Identite.nom.ilike(nom + "%"), Identite.prenom.ilike(prenom + "%") Identite.nom.ilike(nom + "%"), Identite.prenom.ilike(prenom + "%")
@ -351,7 +352,7 @@ def _check_duplicate_code(cnx, args, code_name, disable_notify=False, edit=True)
parameters = {} parameters = {}
if not disable_notify: if not disable_notify:
err_page = f"""<h3><h3>Code étudiant ({code_name}) dupliqué !</h3> err_page = f"""<h3><h3>Code étudiant ({code_name}) dupliqué !</h3>
<p class="help">Le {code_name} {args[code_name]} est déjà utilisé: un seul étudiant peut avoir <p class="help">Le {code_name} {args[code_name]} est déjà utilisé: un seul étudiant peut avoir
ce code. Vérifier votre valeur ou supprimer l'autre étudiant avec cette valeur. ce code. Vérifier votre valeur ou supprimer l'autre étudiant avec cette valeur.
</p> </p>
<ul><li> <ul><li>
@ -566,7 +567,7 @@ admission_edit = _admissionEditor.edit
# Edition simultanee de identite et admission # Edition simultanee de identite et admission
class EtudIdentEditor(object): class EtudIdentEditor:
def create(self, cnx, args): def create(self, cnx, args):
admission_id = admission_create(cnx, args) admission_id = admission_create(cnx, args)
args["admission_id"] = admission_id args["admission_id"] = admission_id
@ -979,12 +980,12 @@ def descr_situation_etud(etudid: int, ne="") -> str:
cnx = ndb.GetDBConnexion() cnx = ndb.GetDBConnexion()
cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor)
cursor.execute( cursor.execute(
"""SELECT I.formsemestre_id, I.etat """SELECT I.formsemestre_id, I.etat
FROM notes_formsemestre_inscription I, notes_formsemestre S FROM notes_formsemestre_inscription I, notes_formsemestre S
WHERE etudid=%(etudid)s WHERE etudid=%(etudid)s
and S.id = I.formsemestre_id and S.id = I.formsemestre_id
and date_debut < now() and date_debut < now()
and date_fin > now() and date_fin > now()
ORDER BY S.date_debut DESC;""", ORDER BY S.date_debut DESC;""",
{"etudid": etudid}, {"etudid": etudid},
) )

View File

@ -99,6 +99,26 @@ def form_search_etud(
return "\n".join(H) return "\n".join(H)
def search_etuds_infos_from_exp(expnom: str = "") -> list[dict]:
"""Cherche étudiants, expnom peut être, dans cet ordre:
un etudid (int), un code NIP, ou le début d'un nom.
"""
if not isinstance(expnom, int) and len(expnom) <= 1:
return [] # si expnom est trop court, n'affiche rien
try:
etudid = int(expnom)
except ValueError:
etudid = None
if etudid is not None:
etuds = sco_etud.get_etud_info(filled=True, etudid=expnom)
if len(etuds) == 1:
return etuds
expnom_str = str(expnom)
if scu.is_valid_code_nip(expnom_str):
return search_etuds_infos(code_nip=expnom_str)
return search_etuds_infos(expnom=expnom_str)
def search_etud_in_dept(expnom=""): def search_etud_in_dept(expnom=""):
"""Page recherche d'un etudiant. """Page recherche d'un etudiant.
@ -111,21 +131,7 @@ def search_etud_in_dept(expnom=""):
Args: Args:
expnom: string, regexp sur le nom ou un code_nip ou un etudid expnom: string, regexp sur le nom ou un code_nip ou un etudid
""" """
if isinstance(expnom, int) or len(expnom) > 1: etuds = search_etuds_infos_from_exp(expnom)
try:
etudid = int(expnom)
except ValueError:
etudid = None
if etudid is not None:
etuds = sco_etud.get_etud_info(filled=True, etudid=expnom)
if (etudid is None) or len(etuds) != 1:
expnom_str = str(expnom)
if scu.is_valid_code_nip(expnom_str):
etuds = search_etuds_infos(code_nip=expnom_str)
else:
etuds = search_etuds_infos(expnom=expnom_str)
else:
etuds = [] # si expnom est trop court, n'affiche rien
if request.method == "POST": if request.method == "POST":
vals = request.form vals = request.form
@ -256,8 +262,8 @@ def search_etud_by_name(term: str) -> list:
"""SELECT nom, prenom, code_nip """SELECT nom, prenom, code_nip
FROM identite FROM identite
WHERE WHERE
dept_id = %(dept_id)s dept_id = %(dept_id)s
AND code_nip LIKE %(beginning)s AND code_nip LIKE %(beginning)s
ORDER BY nom ORDER BY nom
""", """,
{"beginning": term + "%", "dept_id": g.scodoc_dept_id}, {"beginning": term + "%", "dept_id": g.scodoc_dept_id},
@ -274,8 +280,8 @@ def search_etud_by_name(term: str) -> list:
r = ndb.SimpleDictFetch( r = ndb.SimpleDictFetch(
"""SELECT id AS etudid, nom, prenom """SELECT id AS etudid, nom, prenom
FROM identite FROM identite
WHERE WHERE
dept_id = %(dept_id)s dept_id = %(dept_id)s
AND nom LIKE %(beginning)s AND nom LIKE %(beginning)s
ORDER BY nom ORDER BY nom
""", """,
@ -350,7 +356,7 @@ def table_etud_in_accessible_depts(expnom=None):
else: else:
ss = "" ss = ""
H.append( H.append(
f"""<p>(recherche menée dans le{ss} département{ss}: f"""<p>(recherche menée dans le{ss} département{ss}:
{", ".join(accessible_depts)}) {", ".join(accessible_depts)})
</p> </p>
<p> <p>

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.6.47" SCOVERSION = "9.6.48"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"

View File

@ -3,7 +3,7 @@
"""Test modèle étudiant (identite) """Test modèle étudiant (identite)
Utiliser comme: Utiliser comme:
pytest tests/unit/test_etudiants.py pytest tests/unit/test_etudiants.py
""" """
@ -16,7 +16,7 @@ import app
from app import db from app import db
from app.models import Admission, Adresse, Departement, FormSemestre, Identite from app.models import Admission, Adresse, Departement, FormSemestre, Identite
from app.scodoc import sco_etud from app.scodoc import sco_etud
from app.scodoc import sco_import_etuds from app.scodoc import sco_find_etud, sco_import_etuds
from config import TestConfig from config import TestConfig
from tests.unit import setup, dict_include from tests.unit import setup, dict_include
@ -267,7 +267,7 @@ def test_import_etuds_xlsx(test_client):
"civilite_etat_civil": "F", "civilite_etat_civil": "F",
"civilite": "X", "civilite": "X",
"code_ine": "ine10", "code_ine": "ine10",
"code_nip": "nip10", "code_nip": "1000010",
"date_naissance": "", "date_naissance": "",
"dept_acronym": "TEST_", "dept_acronym": "TEST_",
"dept_naissance": "", "dept_naissance": "",
@ -302,7 +302,7 @@ def test_import_etuds_xlsx(test_client):
"civilite_etat_civil": "F", "civilite_etat_civil": "F",
"civilite": "X", "civilite": "X",
"code_ine": "ine10", "code_ine": "ine10",
"code_nip": "nip10", "code_nip": "1000010",
"date_naissance": "", "date_naissance": "",
"dept_acronym": "TEST_", "dept_acronym": "TEST_",
"dept_naissance": "", "dept_naissance": "",
@ -367,3 +367,13 @@ def test_import_etuds_xlsx(test_client):
"telephonemobilestr": "", "telephonemobilestr": "",
}, },
) )
# Test de search_etud_in_dept
etuds = sco_find_etud.search_etuds_infos_from_exp("NOM10")
assert len(etuds) == 1
assert etuds[0]["code_ine"] == "ine10"
etuds = sco_find_etud.search_etuds_infos_from_exp("NOM")
assert len(etuds) > 1
assert all(e["nom"].startswith("NOM") for e in etuds)
etuds = sco_find_etud.search_etuds_infos_from_exp("1000010")
assert len(etuds) == 1
assert etuds[0]["code_ine"] == "ine10"