2023-01-10 00:17:25 +01:00
|
|
|
import datetime
|
2023-01-16 19:18:03 +01:00
|
|
|
from typing import List, Type, Union
|
2023-01-06 22:35:47 +01:00
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
from factory.alchemy import SQLAlchemyModelFactory
|
2023-01-06 22:35:47 +01:00
|
|
|
from flask.testing import FlaskClient
|
|
|
|
|
2023-01-09 22:32:58 +01:00
|
|
|
from app.dependencies import db
|
2023-01-16 22:51:52 +01:00
|
|
|
from app.examination_schedule.models import ExaminationSchedule
|
2023-01-09 22:32:58 +01:00
|
|
|
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(
|
2023-01-16 22:51:52 +01:00
|
|
|
test_client: FlaskClient,
|
|
|
|
url: str,
|
|
|
|
data: Union[dict, None],
|
|
|
|
status_code: int,
|
|
|
|
method: str = "post",
|
2023-01-14 17:38:03 +01:00
|
|
|
) -> 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)
|
2023-01-16 19:18:03 +01:00
|
|
|
# raise Exception(response.status_code, response.json)
|
2023-01-06 22:35:47 +01:00
|
|
|
assert response.status_code == status_code
|
|
|
|
return response.json
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
def _test_case_client(
|
2023-01-16 22:51:52 +01:00
|
|
|
test_client: FlaskClient,
|
|
|
|
url: str,
|
|
|
|
data: Union[dict, None],
|
|
|
|
message: str,
|
|
|
|
status_code: int,
|
|
|
|
key: str = "message",
|
|
|
|
method: str = "post",
|
2023-01-16 19:18:03 +01:00
|
|
|
) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
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-16 19:18:03 +01:00
|
|
|
return response_data
|
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-16 19:18:03 +01:00
|
|
|
assert st.id in data["students"]
|
|
|
|
|
|
|
|
|
|
|
|
def create_many_models(
|
2023-01-16 22:51:52 +01:00
|
|
|
amount: int, factory: Type[SQLAlchemyModelFactory], **kwargs
|
2023-01-16 19:18:03 +01:00
|
|
|
) -> List[SQLAlchemyModelFactory]:
|
|
|
|
models = [factory(**kwargs) for _ in range(amount)]
|
|
|
|
db.session.add_all(models)
|
|
|
|
db.session.commit()
|
|
|
|
return models
|
|
|
|
|
|
|
|
|
|
|
|
def create_one_model(
|
2023-01-16 22:51:52 +01:00
|
|
|
model: Union[Type[db.Model], Type[SQLAlchemyModelFactory]],
|
|
|
|
data: dict = None,
|
|
|
|
**kwargs
|
2023-01-16 19:18:03 +01:00
|
|
|
) -> Union[db.Model, SQLAlchemyModelFactory]:
|
|
|
|
if issubclass(model, SQLAlchemyModelFactory):
|
|
|
|
m = model(**kwargs) # it's a factory
|
|
|
|
else:
|
|
|
|
m = model(**data, **kwargs)
|
|
|
|
db.session.add(m)
|
|
|
|
db.session.commit()
|
|
|
|
return m
|
2023-01-16 21:59:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_data_of_term_of_defence(timedelta_minutes: int = 30) -> dict:
|
|
|
|
ex = ExaminationSchedule.query.first()
|
|
|
|
if ex is None:
|
|
|
|
date = datetime.datetime.now()
|
|
|
|
else:
|
|
|
|
date = ex.start_date
|
|
|
|
return {
|
|
|
|
"start_date": (date + datetime.timedelta(days=1)).strftime(
|
|
|
|
"%Y-%m-%dT%H:%M:%S.000Z"
|
|
|
|
),
|
|
|
|
"end_date": (
|
2023-01-16 22:51:52 +01:00
|
|
|
date + datetime.timedelta(days=1, minutes=timedelta_minutes)
|
2023-01-16 21:59:49 +01:00
|
|
|
).strftime("%Y-%m-%dT%H:%M:%S.000Z"),
|
|
|
|
}
|