2023-01-08 20:26:41 +01:00
|
|
|
import copy
|
|
|
|
|
2023-01-09 22:32:58 +01:00
|
|
|
from app.dependencies import db
|
2023-01-14 17:38:03 +01:00
|
|
|
from app.students.models import Group
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
from ...factory import (
|
|
|
|
GroupFactory,
|
|
|
|
ProjectSupervisorFactory,
|
|
|
|
StudentFactory,
|
|
|
|
YearGroupFactory,
|
2023-01-14 17:38:03 +01:00
|
|
|
)
|
|
|
|
from ...utils import (
|
|
|
|
_test_case_client,
|
|
|
|
_test_case_client_without_response,
|
|
|
|
_test_case_group,
|
|
|
|
assert_model_changes,
|
2023-01-16 19:18:03 +01:00
|
|
|
create_many_models,
|
|
|
|
create_one_model,
|
2023-01-14 17:38:03 +01:00
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
new_data = {"name": "Mobile app"}
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
invalid_data = {
|
2023-01-14 17:38:03 +01:00
|
|
|
"name": "Mobile app v2",
|
2023-01-16 19:18:03 +01:00
|
|
|
"students": [283, 12234],
|
2023-01-14 17:38:03 +01:00
|
|
|
"project_supervisor_id": 1,
|
2023-01-08 20:26:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_list_groups(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
create_many_models(33, GroupFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = _test_case_client_without_response(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/?per_page=10",
|
|
|
|
None,
|
|
|
|
200,
|
|
|
|
method="get",
|
|
|
|
)
|
|
|
|
assert data.get("max_pages") == 4
|
|
|
|
assert len(data.get("groups")) == 10
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_detail_group(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = _test_case_client_without_response(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/detail/",
|
|
|
|
None,
|
|
|
|
200,
|
|
|
|
method="get",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
assert_model_changes(group, data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_detail_group_if_group_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/groups/11/detail/",
|
|
|
|
None,
|
|
|
|
"Not found group!",
|
|
|
|
404,
|
|
|
|
method="get",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_group(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/",
|
|
|
|
None,
|
|
|
|
"Group was deleted!",
|
|
|
|
202,
|
|
|
|
method="delete",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_group_if_group_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/groups/32/",
|
|
|
|
None,
|
|
|
|
"Not found group!",
|
|
|
|
404,
|
|
|
|
method="delete",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(new_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
students = create_many_models(3, StudentFactory, year_group_id=yg.id)
|
|
|
|
ps = create_one_model(ProjectSupervisorFactory, year_group_id=yg.id)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
|
|
|
data["students"] = [student.id for student in students]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
|
|
|
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/",
|
|
|
|
data,
|
|
|
|
"Group was updated!",
|
|
|
|
200,
|
|
|
|
method="put",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
_test_case_group(group, data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group_with_invalid_project_supervisor_id(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(new_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
students = create_many_models(3, StudentFactory, year_group_id=yg.id)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
|
|
|
data["students"] = [student.id for student in students]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = 10
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/",
|
|
|
|
data,
|
|
|
|
"Not found project supervisor!",
|
|
|
|
404,
|
|
|
|
method="put",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group_with_invalid_data(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
data = {"students": ["23", "hello", 3]}
|
|
|
|
response_data = _test_case_client(
|
2023-01-14 17:38:03 +01:00
|
|
|
client,
|
2023-01-16 19:18:03 +01:00
|
|
|
f"/api/coordinator/groups/3/",
|
2023-01-14 17:38:03 +01:00
|
|
|
data,
|
|
|
|
"Validation error",
|
|
|
|
400,
|
|
|
|
method="put",
|
|
|
|
)
|
2023-01-16 19:18:03 +01:00
|
|
|
assert (
|
|
|
|
response_data["detail"]["json"]["students"]["1"][0]
|
|
|
|
== "Not a valid integer."
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
def test_edit_group_with_invalid_student_ids(test_app_with_context) -> None:
|
2023-01-08 20:26:41 +01:00
|
|
|
data = copy.deepcopy(new_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
|
|
|
data["students"] = [2, 6, 4]
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/",
|
|
|
|
data,
|
|
|
|
"Not found students!",
|
|
|
|
404,
|
|
|
|
method="put",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group_if_group_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/groups/333/",
|
|
|
|
new_data,
|
|
|
|
"Not found group!",
|
|
|
|
404,
|
|
|
|
method="put",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group_if_you_pass_empty_data(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/groups/333/",
|
|
|
|
{},
|
|
|
|
"You have passed empty data!",
|
|
|
|
400,
|
|
|
|
method="put",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_group(test_app_with_context) -> None:
|
|
|
|
data = copy.deepcopy(new_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
students = create_many_models(3, StudentFactory)
|
|
|
|
ps = create_one_model(ProjectSupervisorFactory)
|
|
|
|
data["students"] = [student.id for student in students]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
|
|
|
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
data,
|
|
|
|
"Group was created!",
|
|
|
|
201,
|
|
|
|
method="post",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
assert Group.query.count() == 1
|
|
|
|
_test_case_group(Group.query.first(), data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_year_group_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/groups/22/",
|
|
|
|
invalid_data,
|
|
|
|
"Not found year group!",
|
|
|
|
404,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_project_supervisor_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
invalid_data,
|
|
|
|
"Not found project supervisor!",
|
|
|
|
404,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
def test_create_group_if_students_dont_exist(test_app_with_context) -> None:
|
2023-01-08 20:26:41 +01:00
|
|
|
data = copy.deepcopy(invalid_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
ps = create_one_model(ProjectSupervisorFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
data,
|
|
|
|
"Not found students!",
|
|
|
|
404,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_at_least_one_student_belong_to_other_group(
|
|
|
|
test_app_with_context,
|
|
|
|
) -> None:
|
2023-01-08 20:26:41 +01:00
|
|
|
data = copy.deepcopy(invalid_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
ps = create_one_model(ProjectSupervisorFactory, year_group_id=yg.id)
|
|
|
|
group = create_one_model(GroupFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
2023-01-16 19:18:03 +01:00
|
|
|
student = create_one_model(StudentFactory, year_group_id=yg.id)
|
2023-01-08 20:26:41 +01:00
|
|
|
group.students.append(student)
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
data["students"].extend([student.id])
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
data,
|
|
|
|
"One or more students have already belonged to group!",
|
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|