2022-12-22 21:36:09 +01:00
|
|
|
"""
|
|
|
|
Test de l'api Assiduité
|
|
|
|
|
|
|
|
Ecrit par HARTMANN Matthias
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from tests.api.setup_test_api import GET, POST_JSON, api_headers, APIError
|
|
|
|
from random import randint
|
|
|
|
|
|
|
|
ETUDID = 1
|
|
|
|
FAUX = 42069
|
|
|
|
FORMSEMESTREID = 1
|
|
|
|
MODULE = 1
|
|
|
|
|
|
|
|
|
|
|
|
ASSIDUITES_FIELDS = {
|
2023-01-05 17:47:32 +01:00
|
|
|
"assiduite_id": int,
|
2022-12-22 21:36:09 +01:00
|
|
|
"etudid": int,
|
|
|
|
"moduleimpl_id": int,
|
|
|
|
"date_debut": str,
|
|
|
|
"date_fin": str,
|
|
|
|
"etat": str,
|
|
|
|
}
|
|
|
|
|
2023-01-05 17:47:32 +01:00
|
|
|
CREATE_FIELD = {"assiduite_id": int}
|
2022-12-22 21:36:09 +01:00
|
|
|
BATCH_FIELD = {"errors": dict, "success": dict}
|
|
|
|
|
|
|
|
COUNT_FIELDS = {"compte": int, "journee": int, "demi": int, "heure": float}
|
|
|
|
|
|
|
|
TO_REMOVE = []
|
|
|
|
|
|
|
|
|
|
|
|
def check_fields(data, fields=ASSIDUITES_FIELDS):
|
|
|
|
assert set(data.keys()) == set(fields.keys())
|
|
|
|
for key in data:
|
|
|
|
if key == "moduleimpl_id":
|
|
|
|
assert isinstance(data[key], fields[key]) or data[key] is None
|
|
|
|
else:
|
|
|
|
assert isinstance(data[key], fields[key])
|
|
|
|
|
|
|
|
|
|
|
|
def check_failure_get(path, headers, err=None):
|
|
|
|
try:
|
|
|
|
data = GET(path=path, headers=headers)
|
|
|
|
# ^ Renvoi un 404
|
|
|
|
except APIError as api_err:
|
|
|
|
if err is not None:
|
|
|
|
assert api_err.payload["message"] == err
|
|
|
|
else:
|
|
|
|
raise APIError("Le GET n'aurait pas du fonctionner")
|
|
|
|
|
|
|
|
|
|
|
|
def check_failure_post(path, headers, data, err=None):
|
|
|
|
try:
|
|
|
|
data = POST_JSON(path=path, headers=headers, data=data)
|
|
|
|
# ^ Renvoi un 404
|
|
|
|
except APIError as api_err:
|
|
|
|
if err is not None:
|
|
|
|
assert api_err.payload["message"] == err
|
|
|
|
else:
|
|
|
|
raise APIError("Le GET n'aurait pas du fonctionner")
|
|
|
|
|
|
|
|
|
|
|
|
def create_data(etat: str, day: str, module=None):
|
|
|
|
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
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_assiduite(api_headers):
|
|
|
|
|
|
|
|
# Bon fonctionnement == id connu
|
|
|
|
data = GET(path="/assiduite/1", headers=api_headers)
|
|
|
|
check_fields(data)
|
|
|
|
|
|
|
|
# Mauvais Fonctionnement == id inconnu
|
|
|
|
|
|
|
|
check_failure_get(
|
|
|
|
f"/assiduite/{FAUX}",
|
|
|
|
api_headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_count_assiduites(api_headers):
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = GET(path=f"/assiduites/{ETUDID}/count", headers=api_headers)
|
|
|
|
check_fields(data, COUNT_FIELDS)
|
|
|
|
|
|
|
|
metrics = {"heure", "compte"}
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/{ETUDID}/count/query?metric={','.join(metrics)}",
|
|
|
|
headers=api_headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert set(data.keys()) == metrics
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
|
|
|
|
check_failure_get(f"/assiduites/{FAUX}/count", api_headers)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_assiduites(api_headers):
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = GET(path=f"/assiduites/{ETUDID}", headers=api_headers)
|
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
|
|
|
data = GET(path=f"/assiduites/{ETUDID}/query?", headers=api_headers)
|
|
|
|
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):
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = GET(path=f"/assiduites/formsemestre/{FORMSEMESTREID}", headers=api_headers)
|
|
|
|
assert isinstance(data, list)
|
|
|
|
for ass in data:
|
|
|
|
check_fields(ass, ASSIDUITES_FIELDS)
|
|
|
|
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/query?", headers=api_headers
|
|
|
|
)
|
|
|
|
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):
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/count", headers=api_headers
|
|
|
|
)
|
|
|
|
check_fields(data, COUNT_FIELDS)
|
|
|
|
metrics = {"heure", "compte"}
|
|
|
|
data = GET(
|
|
|
|
path=f"/assiduites/formsemestre/{FORMSEMESTREID}/count/query?metric={','.join(metrics)}",
|
|
|
|
headers=api_headers,
|
|
|
|
)
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_create(api_headers):
|
|
|
|
|
|
|
|
# -== Sans batch ==-
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
data = create_data("present", "01")
|
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/{ETUDID}/create", data, api_headers)
|
|
|
|
check_fields(res, CREATE_FIELD)
|
2023-01-05 17:47:32 +01:00
|
|
|
TO_REMOVE.append(res["assiduite_id"])
|
2022-12-22 21:36:09 +01:00
|
|
|
|
|
|
|
data2 = create_data("absent", "02", MODULE)
|
|
|
|
res = POST_JSON(f"/assiduite/{ETUDID}/create", data2, api_headers)
|
|
|
|
check_fields(res, CREATE_FIELD)
|
2023-01-05 17:47:32 +01:00
|
|
|
TO_REMOVE.append(res["assiduite_id"])
|
2022-12-22 21:36:09 +01:00
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
check_failure_post(f"/assiduite/{FAUX}/create", api_headers, data)
|
|
|
|
check_failure_post(
|
|
|
|
f"/assiduite/{ETUDID}/create",
|
|
|
|
api_headers,
|
|
|
|
data,
|
2023-01-05 17:47:32 +01:00
|
|
|
err="Duplication des assiduités (la période rentrée rentre en conflit avec une assiduité enregistrée)",
|
2022-12-22 21:36:09 +01:00
|
|
|
)
|
|
|
|
check_failure_post(
|
|
|
|
f"/assiduite/{ETUDID}/create",
|
|
|
|
api_headers,
|
|
|
|
create_data("absent", "03", FAUX),
|
2023-01-05 17:47:32 +01:00
|
|
|
err="param 'moduleimpl_id': invalide",
|
2022-12-22 21:36:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# -== Avec batch ==-
|
|
|
|
|
|
|
|
# Bon Fonctionnement
|
|
|
|
|
|
|
|
etats = ["present", "absent", "retard"]
|
|
|
|
data = {
|
|
|
|
"batch": [
|
|
|
|
create_data(etats[d % 3], 10 + d, MODULE if d % 2 else None)
|
|
|
|
for d in range(randint(3, 5))
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/{ETUDID}/create/batch", data, api_headers)
|
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
for dat in res["success"]:
|
|
|
|
check_fields(res["success"][dat], CREATE_FIELD)
|
2023-01-05 17:47:32 +01:00
|
|
|
TO_REMOVE.append(res["success"][dat]["assiduite_id"])
|
2022-12-22 21:36:09 +01:00
|
|
|
|
|
|
|
# Mauvais Fonctionnement
|
|
|
|
|
|
|
|
data2 = {
|
|
|
|
"batch": [
|
|
|
|
create_data("present", "01"),
|
|
|
|
create_data("present", "25", FAUX),
|
|
|
|
create_data("blabla", 26),
|
|
|
|
create_data("absent", 32),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/{ETUDID}/create/batch", data2, api_headers)
|
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 4
|
|
|
|
|
|
|
|
assert (
|
|
|
|
res["errors"]["0"]
|
2023-01-05 17:47:32 +01:00
|
|
|
== "Duplication des assiduités (la période rentrée rentre en conflit avec une assiduité enregistrée)"
|
2022-12-22 21:36:09 +01:00
|
|
|
)
|
2023-01-05 17:47:32 +01:00
|
|
|
assert res["errors"]["1"] == "param 'moduleimpl_id': invalide"
|
2022-12-22 21:36:09 +01:00
|
|
|
assert res["errors"]["2"] == "param 'etat': invalide"
|
|
|
|
assert (
|
|
|
|
res["errors"]["3"]
|
|
|
|
== "param 'date_debut': format invalide, param 'date_fin': format invalide"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_edit(api_headers):
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
|
|
|
|
|
|
|
data = {"etat": "retard", "moduleimpl_id": MODULE}
|
|
|
|
res = POST_JSON(f"/assiduite/{TO_REMOVE[0]}/edit", data, api_headers)
|
|
|
|
assert res == {"OK": True}
|
|
|
|
|
|
|
|
data["moduleimpl_id"] = None
|
|
|
|
res = POST_JSON(f"/assiduite/{TO_REMOVE[1]}/edit", data, api_headers)
|
|
|
|
assert res == {"OK": True}
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
|
|
|
|
check_failure_post(f"/assiduite/{FAUX}/edit", api_headers, data)
|
|
|
|
data["etat"] = "blabla"
|
|
|
|
check_failure_post(
|
|
|
|
f"/assiduite/{TO_REMOVE[2]}/edit",
|
|
|
|
api_headers,
|
|
|
|
data,
|
|
|
|
err="param 'etat': invalide",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_delete(api_headers):
|
|
|
|
# -== Sans batch ==-
|
|
|
|
|
|
|
|
# Bon fonctionnement
|
2023-01-05 17:47:32 +01:00
|
|
|
data = {"assiduite_id": TO_REMOVE[0]}
|
2022-12-22 21:36:09 +01:00
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/delete", data, api_headers)
|
|
|
|
assert res == {"OK": True}
|
|
|
|
|
|
|
|
# Mauvais fonctionnement
|
|
|
|
check_failure_post(
|
|
|
|
f"/assiduite/delete",
|
|
|
|
api_headers,
|
2023-01-05 17:47:32 +01:00
|
|
|
{"assiduite_id": FAUX},
|
2022-12-22 21:36:09 +01:00
|
|
|
err="Assiduite non existante",
|
|
|
|
)
|
|
|
|
# -== Avec batch ==-
|
|
|
|
|
|
|
|
# Bon Fonctionnement
|
|
|
|
|
|
|
|
data = {"batch": TO_REMOVE[1:]}
|
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/delete/batch", data, api_headers)
|
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
for dat in res["success"]:
|
|
|
|
assert res["success"][dat] == {"OK": True}
|
|
|
|
|
|
|
|
# Mauvais Fonctionnement
|
|
|
|
|
|
|
|
data2 = {
|
|
|
|
"batch": [
|
|
|
|
FAUX,
|
|
|
|
FAUX + 1,
|
|
|
|
FAUX + 2,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = POST_JSON(f"/assiduite/delete/batch", data2, api_headers)
|
|
|
|
check_fields(res, BATCH_FIELD)
|
|
|
|
assert len(res["errors"]) == 3
|
|
|
|
|
|
|
|
assert all([res["errors"][i] == "Assiduite non existante" for i in res["errors"]])
|