ScoDoc/tests/unit/test_etudiants.py

188 lines
5.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""Test modèle étudiant (identite)
Utiliser comme:
pytest tests/unit/test_etudiants.py
"""
import datetime
from pathlib import Path
from flask import current_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 tests.unit import setup
def test_identite(test_client):
"Test de base du modèle identité: création, clonage, ..."
args = {
"civilite": "X",
"code_ine": "code_ine",
"code_nip": "code_nip",
"date_naissance": datetime.date(2000, 1, 2),
"dept_id": 1,
"dept_naissance": "dept_naissance",
"lieu_naissance": "lieu_naissance",
"nationalite": "nationalite",
"nom_usuel": "nom_usuel",
"nom": "nom",
"prenom_etat_civil": "prenom_etat_civil",
"prenom": "prenom",
}
e = Identite.create_etud(**args)
db.session.add(e)
db.session.flush()
admission_id = e.admission_id
admission = db.session.get(Admission, admission_id)
assert admission is not None
assert e.boursier is False
assert e.adresses.count() == 1
adresses_ids = [a.id for a in e.adresses]
# --- Teste cascade admission:
db.session.delete(e)
db.session.flush()
assert db.session.get(Admission, admission_id) is None
assert db.session.get(Departement, 1) is not None
# --- Teste cascade adresses
for adresse_id in adresses_ids:
assert db.session.get(Adresse, adresse_id) is None
# --- Test cascade département
dept = Departement(acronym="test_identite")
db.session.add(dept)
db.session.flush()
args1 = args | {"dept_id": dept.id}
e = Identite.create_etud(**args1)
db.session.add(e)
db.session.flush()
etudid = e.id
db.session.delete(dept)
db.session.flush()
assert db.session.get(Identite, etudid) is None
def test_etat_civil(test_client):
"Test des attributs état civil"
dept = Departement.query.first()
args = {"nom": "nom", "prenom": "prénom", "civilite": "M", "dept_id": dept.id}
# Homme
e = Identite(**args)
db.session.add(e)
db.session.flush()
assert e.civilite_etat_civil_str == "M."
assert e.e == ""
# Femme
e = Identite(**args | {"civilite": "F"})
db.session.add(e)
db.session.flush()
assert e.civilite_etat_civil_str == "Mme"
assert e.e == "e"
# Homme devenu femme
e = Identite(**(args | {"civilite_etat_civil": "F"}))
db.session.add(e)
db.session.flush()
assert e.civilite_etat_civil_str == "Mme"
assert e.civilite_str == "M."
assert e.e == ""
# Femme devenue neutre
e = Identite(**(args | {"civilite": "X", "civilite_etat_civil": "F"}))
db.session.add(e)
db.session.flush()
assert e.civilite_etat_civil_str == "Mme"
assert e.civilite_str == ""
assert e.e == "(e)"
assert e.prenom_etat_civil is None
# La version dict
e_d = e.to_dict_scodoc7()
assert e_d["civilite"] == "X"
assert e_d["civilite_etat_civil"] == "F"
assert e_d["ne"] == "(e)"
def test_etud_legacy(test_client):
"Test certaines fonctions scodoc7 (sco_etud)"
dept = Departement.query.first()
args = {"nom": "nom", "prenom": "prénom", "civilite": "M", "dept_id": dept.id}
# Prénom état civil
e = Identite(**(args))
db.session.add(e)
db.session.flush()
e_dict = e.to_dict_bul()
sco_etud.format_etud_ident(e_dict)
assert e_dict["nom_disp"] == "NOM"
assert e_dict["prenom_etat_civil"] == ""
def test_import_etuds_xlsx(test_client):
"test import étudiant depuis xlsx"
G, formation_id, (ue1_id, ue2_id, ue3_id), module_ids = setup.build_formation_test(
acronyme="IMPXLSX"
)
formsemestre_id = G.create_formsemestre(
formation_id=formation_id,
semestre_id=1,
date_debut="01/01/2021",
date_fin="30/06/2021",
)
filename = (
Path(current_app.config["SCODOC_DIR"])
/ "tests/ressources/misc/ImportEtudiants.xlsx"
)
with open(filename, mode="rb") as f:
sco_import_etuds.scolars_import_excel_file(
f, formsemestre_id=formsemestre_id, exclude_cols=["photo_filename"]
)
formsemestre = db.session.get(FormSemestre, formsemestre_id)
# Vérifie tous les champs du premier étudiant
etud = formsemestre.etuds.first()
assert etud.code_nip == "nip1"
assert etud.code_ine == "ine1"
assert etud.nom == "NOM1"
assert etud.nom_usuel == "nom_usuel1"
assert etud.prenom == "PRÉNOM1"
assert etud.civilite == "M"
assert etud.prenom_etat_civil == "PRÉNOM_CIVIL1"
assert etud.civilite_etat_civil == "M"
assert etud.date_naissance == datetime.date(2001, 5, 1)
assert etud.lieu_naissance == "Paris"
assert etud.nationalite == "Belge"
assert etud.boursier is True
# Admission
adm = etud.admission
assert adm.bac == "C"
assert adm.specialite == "SPÉ"
assert adm.annee_bac == 2023
assert adm.math == "11.0" # deprecated field
assert adm.physique == "12.0" # deprecated field
assert adm.anglais == "13.0" # deprecated field
assert adm.francais == "14.0" # deprecated field
assert adm.boursier_prec is False
assert adm.qualite == 10
assert adm.rapporteur == "xx"
assert adm.score == 5
assert adm.classement == 111
assert adm.nomlycee == "nomlycée"
assert adm.codepostallycee == "75005"
# Adresse
adresse: Adresse = etud.adresses.first()
assert adresse.email == "etud1@etud.no"
assert adresse.emailperso == "etud1@perso.no"
assert adresse.domicile == "1 rue A"
assert adresse.codepostaldomicile == "12345"
assert adresse.villedomicile == "Lima"
assert adresse.paysdomicile == "Pérou"
assert adresse.telephone == "102030405"
assert adresse.telephonemobile == "605040302"
#
# mapp.set_sco_dept("TEST_")
# ctx.push()
# login_user(User.query.filter_by(user_name="admin").first())