2023-01-08 17:15:02 +01:00
|
|
|
import copy
|
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
from app.students.models import Student, YearGroup
|
|
|
|
|
|
|
|
from ...factory import StudentFactory, YearGroupFactory
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...utils import (
|
|
|
|
_test_case_client,
|
|
|
|
_test_case_client_without_response,
|
|
|
|
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 17:15:02 +01:00
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
new_data = {"first_name": "Martin", "last_name": "Green"}
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
data_to_create_student = {
|
2023-01-14 17:38:03 +01:00
|
|
|
"first_name": "Albert",
|
|
|
|
"last_name": "Marcus",
|
|
|
|
"index": 123_456,
|
2023-01-08 17:15:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_list_students(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(45, StudentFactory, year_group_id=yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = _test_case_client_without_response(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/students/{yg.id}/?per_page=10",
|
|
|
|
None,
|
|
|
|
200,
|
|
|
|
method="get",
|
|
|
|
)
|
|
|
|
assert data.get("max_pages") == 5
|
|
|
|
assert len(data.get("students")) == 10
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_detail_student(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
st = create_one_model(StudentFactory)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = _test_case_client_without_response(
|
|
|
|
client,
|
2023-01-16 19:18:03 +01:00
|
|
|
f"/api/coordinator/students/{st.id}/detail/",
|
2023-01-14 17:38:03 +01:00
|
|
|
None,
|
|
|
|
200,
|
|
|
|
method="get",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
assert_model_changes(st, data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_detail_student_if_student_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/students/43/detail/",
|
|
|
|
None,
|
|
|
|
"Not found student!",
|
|
|
|
404,
|
|
|
|
method="get",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_student(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
st = create_one_model(StudentFactory)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
2023-01-16 19:18:03 +01:00
|
|
|
f"/api/coordinator/students/{st.id}/",
|
2023-01-14 17:38:03 +01:00
|
|
|
None,
|
|
|
|
"Student was deleted!",
|
|
|
|
202,
|
|
|
|
method="delete",
|
|
|
|
)
|
2023-01-16 19:18:03 +01:00
|
|
|
assert len(Student.query.all()) == 0
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_student_if_student_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/students/43/",
|
|
|
|
None,
|
|
|
|
"Not found student!",
|
|
|
|
404,
|
|
|
|
method="delete",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_student(test_app_with_context) -> None:
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
st = create_one_model(StudentFactory)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
2023-01-16 19:18:03 +01:00
|
|
|
f"/api/coordinator/students/{st.id}/",
|
2023-01-14 17:38:03 +01:00
|
|
|
new_data,
|
|
|
|
"Student was updated!",
|
|
|
|
200,
|
|
|
|
method="put",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_student_with_invalid_data(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(new_data)
|
2023-01-16 19:18:03 +01:00
|
|
|
data["index"] = 3_232_323
|
2023-01-08 17:15:02 +01:00
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
st = create_one_model(StudentFactory)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
2023-01-16 19:18:03 +01:00
|
|
|
f"/api/coordinator/students/{st.id}/",
|
2023-01-14 17:38:03 +01:00
|
|
|
data,
|
|
|
|
"Validation error",
|
|
|
|
400,
|
|
|
|
method="put",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_student_if_student_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/students/54/",
|
|
|
|
new_data,
|
|
|
|
"Not found student!",
|
|
|
|
404,
|
|
|
|
method="put",
|
|
|
|
key="error",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_student(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(data_to_create_student)
|
|
|
|
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
|
|
|
data["year_group_id"] = yg.id
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/students/",
|
|
|
|
data,
|
|
|
|
"Student was created!",
|
|
|
|
200,
|
|
|
|
method="post",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_student_with_invalid_data(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(data_to_create_student)
|
2023-01-16 19:18:03 +01:00
|
|
|
data["index"] = 1_123_432
|
2023-01-08 17:15:02 +01:00
|
|
|
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
|
|
|
data["year_group_id"] = yg.id
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/students/",
|
|
|
|
data,
|
|
|
|
"Validation error",
|
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_student_if_year_group_doesnt_exist(test_app_with_context) -> None:
|
|
|
|
data = copy.copy(data_to_create_student)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-14 17:38:03 +01:00
|
|
|
data["year_group_id"] = 34
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/students/",
|
|
|
|
data,
|
|
|
|
"Not found year group!",
|
|
|
|
404,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-16 19:18:03 +01:00
|
|
|
def test_create_student_if_student_has_already_assigned_to_this_year_group(
|
2023-01-14 17:38:03 +01:00
|
|
|
test_app_with_context,
|
|
|
|
) -> None:
|
2023-01-08 17:15:02 +01:00
|
|
|
data = copy.copy(data_to_create_student)
|
|
|
|
with test_app_with_context.test_client() as client:
|
2023-01-16 19:18:03 +01:00
|
|
|
yg = create_one_model(YearGroupFactory)
|
|
|
|
create_one_model(Student, data, year_group_id=yg.id, email="tmp@gmail.com")
|
2023-01-14 17:38:03 +01:00
|
|
|
data["year_group_id"] = yg.id
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/students/",
|
|
|
|
data,
|
2023-01-16 19:18:03 +01:00
|
|
|
"Student has already assigned to this year group!",
|
2023-01-14 17:38:03 +01:00
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|