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",
"etudid",
(
"etudid",
"nom",
"nom_usuel",
"prenom",
"prenom_etat_civil",
"cas_id",
"admission_id",
"boursier",
"cas_allow_login",
"cas_allow_scodoc_login",
"civilite", # 'M", "F", or "X"
"cas_id",
"civilite_etat_civil",
"date_naissance",
"lieu_naissance",
"dept_naissance",
"nationalite",
"statut",
"boursier",
"foto",
"photo_filename",
"civilite", # 'M", "F", or "X"
"code_ine",
"code_nip",
"date_naissance",
"dept_naissance",
"etudid",
"foto",
"lieu_naissance",
"nationalite",
"nom_usuel",
"nom",
"photo_filename",
"prenom_etat_civil",
"prenom",
"statut",
),
filter_dept=True,
sortkey="nom",
@ -300,7 +301,7 @@ def check_nom_prenom_homonyms(
prenom = prenom.lower().strip()
# 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):
return False, 0
return False, []
# Liste homonymes (dans tous les départements):
query = Identite.query.filter(
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 = {}
if not disable_notify:
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.
</p>
<ul><li>
@ -566,7 +567,7 @@ admission_edit = _admissionEditor.edit
# Edition simultanee de identite et admission
class EtudIdentEditor(object):
class EtudIdentEditor:
def create(self, cnx, args):
admission_id = admission_create(cnx, args)
args["admission_id"] = admission_id
@ -979,12 +980,12 @@ def descr_situation_etud(etudid: int, ne="") -> str:
cnx = ndb.GetDBConnexion()
cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor)
cursor.execute(
"""SELECT I.formsemestre_id, I.etat
FROM notes_formsemestre_inscription I, notes_formsemestre S
WHERE etudid=%(etudid)s
and S.id = I.formsemestre_id
and date_debut < now()
and date_fin > now()
"""SELECT I.formsemestre_id, I.etat
FROM notes_formsemestre_inscription I, notes_formsemestre S
WHERE etudid=%(etudid)s
and S.id = I.formsemestre_id
and date_debut < now()
and date_fin > now()
ORDER BY S.date_debut DESC;""",
{"etudid": etudid},
)

View File

@ -99,6 +99,26 @@ def form_search_etud(
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=""):
"""Page recherche d'un etudiant.
@ -111,21 +131,7 @@ def search_etud_in_dept(expnom=""):
Args:
expnom: string, regexp sur le nom ou un code_nip ou un etudid
"""
if isinstance(expnom, int) or len(expnom) > 1:
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
etuds = search_etuds_infos_from_exp(expnom)
if request.method == "POST":
vals = request.form
@ -256,8 +262,8 @@ def search_etud_by_name(term: str) -> list:
"""SELECT nom, prenom, code_nip
FROM identite
WHERE
dept_id = %(dept_id)s
AND code_nip LIKE %(beginning)s
dept_id = %(dept_id)s
AND code_nip LIKE %(beginning)s
ORDER BY nom
""",
{"beginning": term + "%", "dept_id": g.scodoc_dept_id},
@ -274,8 +280,8 @@ def search_etud_by_name(term: str) -> list:
r = ndb.SimpleDictFetch(
"""SELECT id AS etudid, nom, prenom
FROM identite
WHERE
dept_id = %(dept_id)s
WHERE
dept_id = %(dept_id)s
AND nom LIKE %(beginning)s
ORDER BY nom
""",
@ -350,7 +356,7 @@ def table_etud_in_accessible_depts(expnom=None):
else:
ss = ""
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)})
</p>
<p>

View File

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

View File

@ -3,7 +3,7 @@
"""Test modèle étudiant (identite)
Utiliser comme:
Utiliser comme:
pytest tests/unit/test_etudiants.py
"""
@ -16,7 +16,7 @@ import app
from app import db
from app.models import Admission, Adresse, Departement, FormSemestre, Identite
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 tests.unit import setup, dict_include
@ -267,7 +267,7 @@ def test_import_etuds_xlsx(test_client):
"civilite_etat_civil": "F",
"civilite": "X",
"code_ine": "ine10",
"code_nip": "nip10",
"code_nip": "1000010",
"date_naissance": "",
"dept_acronym": "TEST_",
"dept_naissance": "",
@ -302,7 +302,7 @@ def test_import_etuds_xlsx(test_client):
"civilite_etat_civil": "F",
"civilite": "X",
"code_ine": "ine10",
"code_nip": "nip10",
"code_nip": "1000010",
"date_naissance": "",
"dept_acronym": "TEST_",
"dept_naissance": "",
@ -367,3 +367,13 @@ def test_import_etuds_xlsx(test_client):
"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"