system-pri/backend/tests/functional_tests/coordinator/test_groups.py

287 lines
8.7 KiB
Python

import copy
from app.dependencies import db
from app.students.models import Group
from ...factory import (
GroupFactory,
ProjectSupervisorFactory,
StudentFactory,
YearGroupFactory,
)
from ...utils import (
_test_case_client,
_test_case_client_without_response,
_test_case_group,
assert_model_changes,
create_many_models,
create_one_model,
)
new_data = {"name": "Mobile app"}
invalid_data = {
"name": "Mobile app v2",
"students": [283, 12234],
"project_supervisor_id": 1,
}
def test_list_groups(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
yg = create_one_model(YearGroupFactory)
create_many_models(33, GroupFactory, year_group_id=yg.id)
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
def test_detail_group(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
yg = create_one_model(YearGroupFactory)
group = create_one_model(GroupFactory, year_group_id=yg.id)
data = _test_case_client_without_response(
client,
f"/api/coordinator/groups/{group.id}/detail/",
None,
200,
method="get",
)
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:
_test_case_client(
client,
"/api/coordinator/groups/11/detail/",
None,
"Not found group!",
404,
method="get",
key="error",
)
def test_delete_group(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
yg = create_one_model(YearGroupFactory)
group = create_one_model(GroupFactory, year_group_id=yg.id)
_test_case_client(
client,
f"/api/coordinator/groups/{group.id}/",
None,
"Group was deleted!",
202,
method="delete",
)
def test_delete_group_if_group_doesnt_exist(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
_test_case_client(
client,
"/api/coordinator/groups/32/",
None,
"Not found group!",
404,
method="delete",
key="error",
)
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_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]
data["project_supervisor_id"] = ps.id
_test_case_client(
client,
f"/api/coordinator/groups/{group.id}/",
data,
"Group was updated!",
200,
method="put",
)
_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_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]
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",
)
def test_edit_group_with_invalid_data(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
data = {"students": ["23", "hello", 3]}
response_data = _test_case_client(
client,
f"/api/coordinator/groups/3/",
data,
"Validation error",
400,
method="put",
)
assert (
response_data["detail"]["json"]["students"]["1"][0]
== "Not a valid integer."
)
def test_edit_group_with_invalid_student_ids(test_app_with_context) -> None:
data = copy.deepcopy(new_data)
with test_app_with_context.test_client() as client:
yg = create_one_model(YearGroupFactory)
group = create_one_model(GroupFactory, year_group_id=yg.id)
data["students"] = [2, 6, 4]
_test_case_client(
client,
f"/api/coordinator/groups/{group.id}/",
data,
"Not found students!",
404,
method="put",
key="error",
)
def test_edit_group_if_group_doesnt_exist(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
_test_case_client(
client,
"/api/coordinator/groups/333/",
new_data,
"Not found group!",
404,
method="put",
key="error",
)
def test_edit_group_if_you_pass_empty_data(test_app_with_context) -> None:
with test_app_with_context.test_client() as client:
_test_case_client(
client,
"/api/coordinator/groups/333/",
{},
"You have passed empty data!",
400,
method="put",
key="error",
)
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_one_model(YearGroupFactory)
students = create_many_models(3, StudentFactory)
ps = create_one_model(ProjectSupervisorFactory)
data["students"] = [student.id 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",
)
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:
_test_case_client(
client,
"/api/coordinator/groups/22/",
invalid_data,
"Not found year group!",
404,
method="post",
key="error",
)
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_one_model(YearGroupFactory)
_test_case_client(
client,
f"/api/coordinator/groups/{yg.id}/",
invalid_data,
"Not found project supervisor!",
404,
method="post",
key="error",
)
def test_create_group_if_students_dont_exist(test_app_with_context) -> None:
data = copy.deepcopy(invalid_data)
with test_app_with_context.test_client() as client:
yg = create_one_model(YearGroupFactory)
ps = create_one_model(ProjectSupervisorFactory, year_group_id=yg.id)
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:
data = copy.deepcopy(invalid_data)
with test_app_with_context.test_client() as client:
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)
data["project_supervisor_id"] = ps.id
student = create_one_model(StudentFactory, year_group_id=yg.id)
group.students.append(student)
db.session.commit()
data["students"].extend([student.id])
_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",
)