2023-01-10 00:17:25 +01:00
|
|
|
import datetime
|
2023-01-06 22:35:47 +01:00
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
from flask.testing import FlaskClient
|
|
|
|
|
2023-01-09 22:32:58 +01:00
|
|
|
from app.dependencies import db
|
|
|
|
from app.students.models import Group
|
2023-01-06 22:35:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_model_changes(model: db.Model, expected_data: dict) -> None:
|
|
|
|
for key, val in expected_data.items():
|
2023-01-10 00:17:25 +01:00
|
|
|
value = getattr(model, key)
|
|
|
|
if isinstance(value, datetime.datetime):
|
|
|
|
value = value.strftime("%Y-%m-%dT%H:%M:%S.000Z")
|
|
|
|
assert value == val
|
2023-01-06 22:35:47 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
def _test_case_client_without_response(
|
|
|
|
test_client: FlaskClient,
|
|
|
|
url: str,
|
|
|
|
data: Union[dict, None],
|
|
|
|
status_code: int,
|
|
|
|
method: str = "post",
|
|
|
|
) -> dict:
|
2023-01-06 22:35:47 +01:00
|
|
|
method_func = getattr(test_client, method)
|
|
|
|
if data is not None:
|
|
|
|
response = method_func(url, json=data)
|
|
|
|
else:
|
|
|
|
response = method_func(url)
|
|
|
|
|
|
|
|
assert response.status_code == status_code
|
|
|
|
return response.json
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
def _test_case_client(
|
|
|
|
test_client: FlaskClient,
|
|
|
|
url: str,
|
|
|
|
data: Union[dict, None],
|
|
|
|
message: str,
|
|
|
|
status_code: int,
|
|
|
|
key: str = "message",
|
|
|
|
method: str = "post",
|
|
|
|
) -> None:
|
|
|
|
response_data = _test_case_client_without_response(
|
|
|
|
test_client, url, data, status_code, method
|
|
|
|
)
|
2023-01-06 22:35:47 +01:00
|
|
|
assert key in response_data.keys()
|
|
|
|
assert response_data.get(key) == message
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _test_case_group(group: Group, data: dict) -> None:
|
2023-01-14 17:38:03 +01:00
|
|
|
assert group.name == data["name"]
|
|
|
|
assert group.project_supervisor_id == data["project_supervisor_id"]
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
for st in group.students:
|
2023-01-14 17:38:03 +01:00
|
|
|
assert st.index in data["students"]
|