API: Departement: tests unitaires et corrections.

This commit is contained in:
Emmanuel Viennet 2022-05-05 18:50:59 +02:00
parent ba6b275973
commit 8486512f83
2 changed files with 79 additions and 112 deletions

View File

@ -6,24 +6,35 @@ import app
from app import models from app import models
from app.api import bp from app.api import bp
from app.api.auth import token_auth, token_permission_required from app.api.auth import token_auth, token_permission_required
from app.models import FormSemestre from app.models import Departement, FormSemestre
from app.scodoc.sco_permissions import Permission from app.scodoc.sco_permissions import Permission
def get_departement(dept_ident: str) -> Departement:
"Le departement, par id ou acronyme. Erreur 404 si pas trouvé."
try:
dept_id = int(dept_ident)
except ValueError:
dept_id = None
if dept_id is None:
return Departement.query.filter_by(acronym=dept_ident).first_or_404()
return Departement.query.get_or_404(dept_id)
@bp.route("/departements_ids", methods=["GET"]) @bp.route("/departements_ids", methods=["GET"])
@token_auth.login_required @token_auth.login_required
@token_permission_required(Permission.APIView) @token_permission_required(Permission.APIView)
def departements_ids(): def departements_ids():
"""Liste des ids de départements""" """Liste des ids de départements"""
return jsonify([dept.id for dept in models.Departement.query]) return jsonify([dept.id for dept in Departement.query])
@bp.route("/departement/<int:dept_id>", methods=["GET"]) @bp.route("/departement/<string:dept_ident>", methods=["GET"])
@token_auth.login_required @token_auth.login_required
@token_permission_required(Permission.APIView) @token_permission_required(Permission.APIView)
def departement(dept_id: int): def departement(dept_ident: str):
""" """
Info sur un département. Info sur un département. Accès par id ou acronyme.
Exemple de résultat : Exemple de résultat :
{ {
@ -34,7 +45,7 @@ def departement(dept_id: int):
"date_creation": "Fri, 15 Apr 2022 12:19:28 GMT" "date_creation": "Fri, 15 Apr 2022 12:19:28 GMT"
} }
""" """
dept = models.Departement.query.filter_by(dept_id=dept_id).first_or_404() dept = get_departement(dept_ident)
return jsonify(dept.to_dict()) return jsonify(dept.to_dict())
@ -43,13 +54,10 @@ def departement(dept_id: int):
@token_permission_required(Permission.APIView) @token_permission_required(Permission.APIView)
def departements(): def departements():
"""Liste les départements""" """Liste les départements"""
return jsonify([dept.to_dict() for dept in models.Departement.query]) return jsonify([dept.to_dict() for dept in Departement.query])
@bp.route("/departement/<string:dept_ident>/etudiants", methods=["GET"]) @bp.route("/departement/<string:dept_ident>/etudiants", methods=["GET"])
# @bp.route(
# "/departement/<string:dept_ident>/etudiants/list/<int:formsemestre_id>", methods=["GET"]
# )
@token_auth.login_required @token_auth.login_required
@token_permission_required(Permission.APIView) @token_permission_required(Permission.APIView)
def list_etudiants(dept_ident: str): def list_etudiants(dept_ident: str):
@ -76,18 +84,9 @@ def list_etudiants(dept_ident: str):
] ]
""" """
# Le département, spécifié par un id ou un acronyme # Le département, spécifié par un id ou un acronyme
try: dept = get_departement(dept_ident)
dept_id = int(dept_ident)
except ValueError:
dept_id = None
if dept_id is None:
departement = models.Departement.query.filter_by(
acronym=dept_ident
).first_or_404()
else:
departement = models.Departement.query.get_or_404(dept_id)
return jsonify([etud.to_dict_short() for etud in departement.etudiants]) return jsonify([etud.to_dict_short() for etud in dept.etudiants])
@bp.route("/departement/<string:dept_ident>/formsemestres_courants", methods=["GET"]) @bp.route("/departement/<string:dept_ident>/formsemestres_courants", methods=["GET"])
@ -135,20 +134,11 @@ def liste_semestres_courant(dept_ident: str):
] ]
""" """
# Le département, spécifié par un id ou un acronyme # Le département, spécifié par un id ou un acronyme
try: dept = get_departement(dept_ident)
dept_id = int(dept_ident)
except ValueError:
dept_id = None
if dept_id is None:
departement = models.Departement.query.filter_by(
acronym=dept_ident
).first_or_404()
else:
departement = models.Departement.query.get_or_404(dept_id)
# Les semestres en cours de ce département # Les semestres en cours de ce département
formsemestres = models.FormSemestre.query.filter( formsemestres = models.FormSemestre.query.filter(
dept_id == departement.id, FormSemestre.dept_id == dept.id,
FormSemestre.date_debut <= app.db.func.now(), FormSemestre.date_debut <= app.db.func.now(),
FormSemestre.date_fin >= app.db.func.now(), FormSemestre.date_fin >= app.db.func.now(),
) )

View File

@ -22,11 +22,21 @@ import requests
from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers
from tests.api.tools_test_api import verify_fields from tests.api.tools_test_api import verify_fields
DEPARTEMENT_FIELDS = [
"id",
"acronym",
"description",
"visible",
"date_creation",
]
def test_departements_ids(api_headers):
def test_departements(api_headers):
""" " """ "
Route: /departements_ids Routes: /departements_ids, /departement
""" """
# --- Liste des ids
r = requests.get( r = requests.get(
API_URL + "/departements_ids", API_URL + "/departements_ids",
headers=api_headers, headers=api_headers,
@ -38,89 +48,38 @@ def test_departements_ids(api_headers):
assert len(departements_ids) > 0 assert len(departements_ids) > 0
assert all(isinstance(x, int) for x in departements_ids) assert all(isinstance(x, int) for x in departements_ids)
dept_id = departements_ids[0]
def test_departements(api_headers): # --- Infos sur un département, accès par id
""" "
Route: /departements
"""
fields = [
"id",
"acronym",
"description",
"visible",
"date_creation",
]
r = requests.get( r = requests.get(
API_URL + "/departements", f"{API_URL}/departement/{dept_id}",
headers=api_headers, headers=api_headers,
verify=CHECK_CERTIFICATE, verify=CHECK_CERTIFICATE,
) )
assert r.status_code == 200 assert r.status_code == 200
assert len(r.json()) == 1 dept_a = r.json()
assert verify_fields(dept_a, DEPARTEMENT_FIELDS) is True
dept = r.json()[0] # --- Infos sur un département, accès par acronyme4
r = requests.get(
fields_OK = verify_fields(dept, fields) f"{API_URL}/departement/{dept_a['acronym']}",
headers=api_headers,
assert fields_OK is True verify=CHECK_CERTIFICATE,
)
assert r.status_code == 200
dept_b = r.json()
assert dept_a == dept_b
def test_list_etudiants(api_headers): def test_list_etudiants(api_headers):
fields = { fields = {"id", "nip", "ine", "nom", "nom_usuel", "prenom", "civilite"}
"civilite",
"code_ine",
"code_nip",
"date_naissance",
"email",
"emailperso",
"etudid",
"nom",
"prenom",
"nomprenom",
"lieu_naissance",
"dept_naissance",
"nationalite",
"boursier",
"id",
"codepostaldomicile",
"paysdomicile",
"telephonemobile",
"typeadresse",
"domicile",
"villedomicile",
"telephone",
"fax",
"description",
}
r = requests.get( r = requests.get(
API_URL + "/departements/TAPI/etudiants/list", API_URL + "/departement/TAPI/etudiants",
headers=api_headers, headers=api_headers,
verify=CHECK_CERTIFICATE, verify=CHECK_CERTIFICATE,
) )
etu = r.json()[0]
fields_OK = verify_fields(etu, fields)
assert r.status_code == 200 assert r.status_code == 200
assert len(r.json()) == 16 etud = r.json()[0]
assert fields_OK is True assert verify_fields(etud, fields) is True
r = requests.get(
API_URL + "/departements/TAPI/etudiants/list/1",
headers=api_headers,
verify=CHECK_CERTIFICATE,
)
etu = r.json()[0]
fields_OK = verify_fields(etu, fields)
assert r.status_code == 200
assert len(r.json()) == 16
assert fields_OK is True
# liste_semestres_courant # liste_semestres_courant
@ -148,21 +107,39 @@ def test_semestres_courant(api_headers):
"block_moyennes", "block_moyennes",
"formsemestre_id", "formsemestre_id",
"titre_num", "titre_num",
"titre_formation",
"date_debut_iso", "date_debut_iso",
"date_fin_iso", "date_fin_iso",
"responsables", "responsables",
] ]
dept_id = 1
r = requests.get( r = requests.get(
API_URL + "/departements/TAPI/semestres_courants", f"{API_URL}/departement/{dept_id}",
headers=api_headers, headers=api_headers,
verify=CHECK_CERTIFICATE, verify=CHECK_CERTIFICATE,
) )
sem = r.json()[0]
fields_OK = verify_fields(sem, fields)
assert r.status_code == 200 assert r.status_code == 200
assert len(r.json()) == 1 dept = r.json()
assert fields_OK is True assert dept["id"] == dept_id
# Accès via acronyme
r = requests.get(
f"{API_URL}/departement/{dept['acronym']}/formsemestres_courants",
headers=api_headers,
verify=CHECK_CERTIFICATE,
)
assert r.status_code == 200
result_a = r.json()
assert isinstance(result_a, list) # liste de formsemestres
assert len(result_a) > 0
sem = result_a[0]
assert verify_fields(sem, fields) is True
# accès via dept_id
r = requests.get(
f"{API_URL}/departement/{dept['id']}/formsemestres_courants",
headers=api_headers,
verify=CHECK_CERTIFICATE,
)
assert r.status_code == 200
result_b = r.json()
assert result_a == result_b