2023-01-08 20:26:41 +01:00
|
|
|
import copy
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
2023-01-09 22:32:58 +01:00
|
|
|
from app.dependencies import db
|
|
|
|
from app.project_supervisor.models import YearGroupProjectSupervisors
|
2023-01-14 17:38:03 +01:00
|
|
|
from app.students.models import Group
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...fake_data import (
|
|
|
|
create_group,
|
|
|
|
create_groups,
|
|
|
|
create_project_supervisors,
|
|
|
|
create_students,
|
|
|
|
create_year_group,
|
|
|
|
)
|
|
|
|
from ...utils import (
|
|
|
|
_test_case_client,
|
|
|
|
_test_case_client_without_response,
|
|
|
|
_test_case_group,
|
|
|
|
assert_model_changes,
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
valid_data = {"name": "System Pri"}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"students": [123_344, 455_444],
|
|
|
|
"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:
|
|
|
|
yg = create_year_group()
|
|
|
|
create_groups(yg, 33)
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
group = create_group(valid_data, yg)
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
group = create_group(valid_data, yg)
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
students = create_students(yg, 3)
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
|
|
|
group = create_group(valid_data, yg)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"] = [student.index for student in students]
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
students = create_students(yg, 3)
|
|
|
|
group = create_group(valid_data, yg)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"] = [student.index for student in students]
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
group = create_group(valid_data, yg)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = {"students": [123_4356, 243_533, 434_343]}
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{group.id}/",
|
|
|
|
data,
|
|
|
|
"Validation error",
|
|
|
|
400,
|
|
|
|
method="put",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_group_with_invalid_student_indexes(test_app_with_context) -> None:
|
|
|
|
data = copy.deepcopy(new_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
|
|
|
yg = create_year_group()
|
|
|
|
group = create_group(valid_data, yg)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"] = [123_456, 243_533, 434_343]
|
|
|
|
_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:
|
|
|
|
yg = create_year_group()
|
|
|
|
students = create_students(yg, 3)
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"] = [student.index for student in students]
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_you_exceed_the_group_limit(test_app_with_context) -> None:
|
|
|
|
data = copy.deepcopy(invalid_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
|
|
|
yg = create_year_group()
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
|
|
|
limit_group = current_app.config.get("LIMIT_STUDENTS_PER_GROUP")
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"].extend([999_000 + i for i in range(limit_group + 4)])
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
data,
|
|
|
|
f"Too much students you want add to group, The group can have only {limit_group}"
|
|
|
|
" students",
|
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 20:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_students_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
data = copy.deepcopy(invalid_data)
|
|
|
|
with test_app_with_context.test_client() as client:
|
|
|
|
yg = create_year_group()
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
|
|
|
group = create_group(valid_data, yg)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
2023-01-08 20:26:41 +01:00
|
|
|
student = create_students(yg, 1)[0]
|
|
|
|
group.students.append(student)
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
data["students"].extend([student.index])
|
|
|
|
_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",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_group_if_limit_of_group_was_exceed_for_project_supervisor(
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
ps = create_project_supervisors(yg, 1)[0]
|
2023-01-14 17:38:03 +01:00
|
|
|
data["project_supervisor_id"] = ps.id
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
ygps = YearGroupProjectSupervisors.query.filter_by(
|
|
|
|
project_supervisor_id=ps.id, year_group_id=yg.id
|
|
|
|
).first()
|
2023-01-08 20:26:41 +01:00
|
|
|
limit_group = ygps.limit_group
|
|
|
|
|
|
|
|
groups = create_groups(yg, limit_group)
|
|
|
|
for group in groups:
|
|
|
|
group.project_supervisor_id = ps.id
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/groups/{yg.id}/",
|
|
|
|
data,
|
|
|
|
"Can't create new group, project supervisor achieved a limit of groups",
|
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|