system-pri/backend/tests/utils.py

39 lines
1.3 KiB
Python
Raw Normal View History

from typing import Union
from flask.testing import FlaskClient
from ..app.dependencies import db
from ..app.students.models import Group
def assert_model_changes(model: db.Model, expected_data: dict) -> None:
for key, val in expected_data.items():
assert getattr(model, key) == val
def _test_case_client_without_response(test_client: FlaskClient, url: str, data: Union[dict, None], status_code: int,
method: str = 'post') -> dict:
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
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)
assert key in response_data.keys()
assert response_data.get(key) == message
def _test_case_group(group: Group, data: dict) -> None:
assert group.name == data['name']
assert group.project_supervisor_id == data['project_supervisor_id']
for st in group.students:
assert st.index in data['students']