2023-04-17 15:39:32 +02:00
|
|
|
"""
|
|
|
|
Test de l'api Assiduité
|
|
|
|
|
|
|
|
Ecrit par HARTMANN Matthias
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from random import randint
|
2023-09-27 23:14:45 +02:00
|
|
|
from types import NoneType
|
2023-04-17 15:39:32 +02:00
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
from tests.api.setup_test_api import (
|
|
|
|
GET,
|
|
|
|
POST_JSON,
|
2024-01-05 13:42:55 +01:00
|
|
|
DEPT_ACRONYM,
|
2023-07-04 15:04:58 +02:00
|
|
|
APIError,
|
|
|
|
api_headers,
|
|
|
|
api_admin_headers,
|
2023-08-25 17:59:57 +02:00
|
|
|
check_failure_get,
|
|
|
|
check_failure_post,
|
|
|
|
check_fields,
|
2023-07-04 15:04:58 +02:00
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
ETUDID = 1
|
|
|
|
FAUX = 42069
|
|
|
|
FORMSEMESTREID = 1
|
|
|
|
MODULE = 1
|
|
|
|
|
|
|
|
|
|
|
|
ASSIDUITES_FIELDS = {
|
|
|
|
"assiduite_id": int,
|
|
|
|
"etudid": int,
|
2023-07-29 19:04:35 +02:00
|
|
|
"code_nip": str,
|
2023-04-17 15:39:32 +02:00
|
|
|
"moduleimpl_id": int,
|
|
|
|
"date_debut": str,
|
|
|
|
"date_fin": str,
|
|
|
|
"etat": str,
|
|
|
|
"desc": str,
|
|
|
|
"entry_date": str,
|
2023-09-27 23:14:45 +02:00
|
|
|
"user_id": (int, NoneType),
|
|
|
|
"user_name": (str, NoneType),
|
2023-10-07 08:40:17 +02:00
|
|
|
"user_nom_complet": (str, NoneType),
|
2023-04-17 15:39:32 +02:00
|
|
|
"est_just": bool,
|
2023-07-31 09:41:32 +02:00
|
|
|
"external_data": dict,
|
2023-04-17 15:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CREATE_FIELD = {"assiduite_id": int}
|
2023-07-27 18:00:40 +02:00
|
|
|
BATCH_FIELD = {"errors": list, "success": list}
|
2023-04-17 15:39:32 +02:00
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
COUNT_FIELDS = {"compte": int, "journee": int, "demi": int, "heure": int | float}
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
TO_REMOVE = []
|
|
|
|
|
|
|
|
|
|
|
|
def create_data(etat: str, day: str, module: int = None, desc: str = None):
|
|
|
|
"""
|
|
|
|
Permet de créer un dictionnaire assiduité
|
|
|
|
|
|
|
|
Args:
|
|
|
|
etat (str): l'état de l'assiduité (PRESENT,ABSENT,RETARD)
|
|
|
|
day (str): Le jour de l'assiduité
|
|
|
|
module (int, optional): Le moduleimpl_id associé
|
|
|
|
desc (str, optional): Une description de l'assiduité (eg: motif retard )
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: la représentation d'une assiduité
|
|
|
|
"""
|
|
|
|
data = {
|
|
|
|
"date_debut": f"2022-01-{day}T08:00",
|
|
|
|
"date_fin": f"2022-01-{day}T10:00",
|
|
|
|
"etat": etat,
|
|
|
|
}
|
|
|
|
|
|
|
|
if module is not None:
|
|
|
|
data["moduleimpl_id"] = module
|
|
|
|
if desc is not None:
|
|
|
|
data["desc"] = desc
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_assiduite(api_headers):
|
|
|
|
"""test de la route /assiduite/<assiduite_id:int>"""
|
|
|
|
|
|
|
|
# Bon fonctionnement == id connu
|
2024-01-05 13:42:55 +01:00
|
|
|
data = GET(path="/assiduite/1", headers=api_headers, dept=DEPT_ACRONYM)
|
2023-08-27 22:18:57 +02:00
|
|
|
check_fields(data, fields=ASSIDUITES_FIELDS)
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# Mauvais Fonctionnement == id inconnu
|
|
|
|
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduite/{FAUX}",
|
|
|
|
api_headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_count_assiduites(api_headers):
|
|
|
|
"""test de la route /assiduites/<etudid:int>/count"""
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/{ETUDID}/count", headers=api_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(data, COUNT_FIELDS)
|
|
|
|
|
|
|
|
metrics = {"heure", "compte"}
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/{ETUDID}/count/query?metric={','.join(metrics)}",
|
|
|
|
headers=api_headers,
|
2024-01-05 13:42:55 +01:00
|
|
|
dept=DEPT_ACRONYM,
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert set(data.keys()) == metrics
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
|
|
|
|
check_failure_get(f"/assiduites/{FAUX}/count", api_headers)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_assiduites(api_headers):
|
|
|
|
"""test de la route /assiduites/<etudid:int>"""
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
data = GET(path=f"/assiduites/{ETUDID}", headers=api_headers, dept=DEPT_ACRONYM)
|
2023-04-17 15:39:32 +02:00
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/{ETUDID}/query?", headers=api_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
check_failure_get(f"/assiduites/{FAUX}", api_headers)
|
|
|
|
check_failure_get(f"/assiduites/{FAUX}/query?", api_headers)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_formsemestre_assiduites(api_headers):
|
|
|
|
"""test de la route /assiduites/formsemestre/<formsemestre_id:int>"""
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}",
|
|
|
|
headers=api_headers,
|
|
|
|
dept=DEPT_ACRONYM,
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
|
|
|
data = GET(
|
2024-01-05 13:42:55 +01:00
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/query?",
|
|
|
|
headers=api_headers,
|
|
|
|
dept=DEPT_ACRONYM,
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduites/formsemestre/{FAUX}",
|
|
|
|
api_headers,
|
|
|
|
err="le paramètre 'formsemestre_id' n'existe pas",
|
|
|
|
)
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduites/formsemestre/{FAUX}/query?",
|
|
|
|
api_headers,
|
|
|
|
err="le paramètre 'formsemestre_id' n'existe pas",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_count_formsemestre_assiduites(api_headers):
|
|
|
|
"""test de la route /assiduites/formsemestre/<formsemestre_id:int>/count"""
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = GET(
|
2024-01-05 13:42:55 +01:00
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/count",
|
|
|
|
headers=api_headers,
|
|
|
|
dept=DEPT_ACRONYM,
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
2024-01-05 13:42:55 +01:00
|
|
|
|
|
|
|
print("data: ", data)
|
|
|
|
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(data, COUNT_FIELDS)
|
|
|
|
metrics = {"heure", "compte"}
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/count/query?metric={','.join(metrics)}",
|
|
|
|
headers=api_headers,
|
2024-01-05 13:42:55 +01:00
|
|
|
dept=DEPT_ACRONYM,
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
|
|
|
assert set(data.keys()) == metrics
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduites/formsemestre/{FAUX}/count",
|
|
|
|
api_headers,
|
|
|
|
err="le paramètre 'formsemestre_id' n'existe pas",
|
|
|
|
)
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduites/formsemestre/{FAUX}/count/query?",
|
|
|
|
api_headers,
|
|
|
|
err="le paramètre 'formsemestre_id' n'existe pas",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def test_route_create(api_admin_headers):
|
2023-04-17 15:39:32 +02:00
|
|
|
"""test de la route /assiduite/<etudid:int>/create"""
|
|
|
|
|
|
|
|
# -== Unique ==-
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
2024-01-05 13:42:55 +01:00
|
|
|
data = create_data("present", "03")
|
2023-04-17 15:39:32 +02:00
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{ETUDID}/create", [data], api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["success"]) == 1
|
|
|
|
|
2023-07-27 18:00:40 +02:00
|
|
|
TO_REMOVE.append(res["success"][0]["message"]["assiduite_id"])
|
2023-08-11 16:14:29 +02:00
|
|
|
data = GET(
|
|
|
|
path=f'/assiduite/{res["success"][0]["message"]["assiduite_id"]}',
|
|
|
|
headers=api_admin_headers,
|
2024-01-05 13:42:55 +01:00
|
|
|
dept=DEPT_ACRONYM,
|
2023-08-11 16:14:29 +02:00
|
|
|
)
|
2023-08-27 22:18:57 +02:00
|
|
|
check_fields(data, fields=ASSIDUITES_FIELDS)
|
2023-04-17 15:39:32 +02:00
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
data2 = create_data("absent", "04", MODULE, "desc")
|
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{ETUDID}/create", [data2], api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["success"]) == 1
|
|
|
|
|
2023-07-27 18:00:40 +02:00
|
|
|
TO_REMOVE.append(res["success"][0]["message"]["assiduite_id"])
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
2023-07-04 15:04:58 +02:00
|
|
|
check_failure_post(f"/assiduite/{FAUX}/create", api_admin_headers, [data])
|
2023-04-17 15:39:32 +02:00
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{ETUDID}/create", [data], api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 1
|
|
|
|
assert (
|
2023-07-27 18:00:40 +02:00
|
|
|
res["errors"][0]["message"]
|
2023-10-09 23:34:03 +02:00
|
|
|
== "Duplication: la période rentre en conflit avec une plage enregistrée"
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
res = POST_JSON(
|
2023-07-04 15:04:58 +02:00
|
|
|
f"/assiduite/{ETUDID}/create",
|
2024-01-05 13:42:55 +01:00
|
|
|
[create_data("absent", "05", FAUX)],
|
2023-07-04 15:04:58 +02:00
|
|
|
api_admin_headers,
|
2024-01-05 13:42:55 +01:00
|
|
|
dept=DEPT_ACRONYM,
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 1
|
2023-07-27 18:00:40 +02:00
|
|
|
assert res["errors"][0]["message"] == "param 'moduleimpl_id': invalide"
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# -== Multiple ==-
|
|
|
|
|
|
|
|
# Bon Fonctionnement
|
|
|
|
|
|
|
|
etats = ["present", "absent", "retard"]
|
|
|
|
data = [
|
|
|
|
create_data(etats[d % 3], 10 + d, MODULE if d % 2 else None)
|
2024-01-05 13:42:55 +01:00
|
|
|
for d in range(randint(2, 4))
|
2023-04-17 15:39:32 +02:00
|
|
|
]
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{ETUDID}/create", data, api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
for dat in res["success"]:
|
2023-07-27 18:00:40 +02:00
|
|
|
check_fields(dat["message"], CREATE_FIELD)
|
|
|
|
TO_REMOVE.append(dat["message"]["assiduite_id"])
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# Mauvais Fonctionnement
|
|
|
|
|
|
|
|
data2 = [
|
2024-01-05 13:42:55 +01:00
|
|
|
create_data("present", "03"),
|
2023-04-17 15:39:32 +02:00
|
|
|
create_data("present", "25", FAUX),
|
|
|
|
create_data("blabla", 26),
|
|
|
|
create_data("absent", 32),
|
2024-01-05 13:42:55 +01:00
|
|
|
create_data("absent", "01"),
|
2023-04-17 15:39:32 +02:00
|
|
|
]
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{ETUDID}/create", data2, api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
2024-01-05 13:42:55 +01:00
|
|
|
assert len(res["errors"]) == 5
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
assert (
|
2023-07-27 18:00:40 +02:00
|
|
|
res["errors"][0]["message"]
|
2023-10-09 23:34:03 +02:00
|
|
|
== "Duplication: la période rentre en conflit avec une plage enregistrée"
|
2023-04-17 15:39:32 +02:00
|
|
|
)
|
2023-07-27 18:00:40 +02:00
|
|
|
assert res["errors"][1]["message"] == "param 'moduleimpl_id': invalide"
|
|
|
|
assert res["errors"][2]["message"] == "param 'etat': invalide"
|
2023-04-17 15:39:32 +02:00
|
|
|
assert (
|
2023-07-27 18:00:40 +02:00
|
|
|
res["errors"][3]["message"]
|
2023-04-17 15:39:32 +02:00
|
|
|
== "param 'date_debut': format invalide, param 'date_fin': format invalide"
|
|
|
|
)
|
2024-01-05 13:42:55 +01:00
|
|
|
assert res["errors"][4]["message"] == "La date de début n'est pas un jour travaillé"
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def test_route_edit(api_admin_headers):
|
2023-04-17 15:39:32 +02:00
|
|
|
"""test de la route /assiduite/<assiduite_id:int>/edit"""
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = {"etat": "retard", "moduleimpl_id": MODULE}
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{TO_REMOVE[0]}/edit", data, api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
assert res == {"OK": True}
|
|
|
|
|
|
|
|
data["moduleimpl_id"] = None
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON(
|
|
|
|
f"/assiduite/{TO_REMOVE[1]}/edit", data, api_admin_headers, dept=DEPT_ACRONYM
|
|
|
|
)
|
2023-04-17 15:39:32 +02:00
|
|
|
assert res == {"OK": True}
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
check_failure_post(f"/assiduite/{FAUX}/edit", api_admin_headers, data)
|
2023-04-17 15:39:32 +02:00
|
|
|
data["etat"] = "blabla"
|
|
|
|
check_failure_post(
|
|
|
|
f"/assiduite/{TO_REMOVE[2]}/edit",
|
2023-07-04 15:04:58 +02:00
|
|
|
api_admin_headers,
|
2023-04-17 15:39:32 +02:00
|
|
|
data,
|
|
|
|
err="param 'etat': invalide",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def test_route_delete(api_admin_headers):
|
2023-04-17 15:39:32 +02:00
|
|
|
"""test de la route /assiduite/delete"""
|
|
|
|
# -== Unique ==-
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
data = TO_REMOVE[0]
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON("/assiduite/delete", [data], api_admin_headers, dept=DEPT_ACRONYM)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
for dat in res["success"]:
|
2023-07-27 18:00:40 +02:00
|
|
|
assert dat["message"] == "OK"
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON("/assiduite/delete", [data], api_admin_headers, dept=DEPT_ACRONYM)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 1
|
|
|
|
|
|
|
|
# -== Multiple ==-
|
|
|
|
|
|
|
|
# Bon Fonctionnement
|
|
|
|
|
|
|
|
data = TO_REMOVE[1:]
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON("/assiduite/delete", data, api_admin_headers, dept=DEPT_ACRONYM)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
for dat in res["success"]:
|
2023-07-27 18:00:40 +02:00
|
|
|
assert dat["message"] == "OK"
|
2023-04-17 15:39:32 +02:00
|
|
|
|
|
|
|
# Mauvais Fonctionnement
|
|
|
|
|
|
|
|
data2 = [
|
|
|
|
FAUX,
|
|
|
|
FAUX + 1,
|
|
|
|
FAUX + 2,
|
|
|
|
]
|
|
|
|
|
2024-01-05 13:42:55 +01:00
|
|
|
res = POST_JSON("/assiduite/delete", data2, api_admin_headers, dept=DEPT_ACRONYM)
|
2023-04-17 15:39:32 +02:00
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 3
|
|
|
|
|
2023-07-27 18:00:40 +02:00
|
|
|
assert all(i["message"] == "Assiduite non existante" for i in res["errors"])
|