2023-01-08 17:15:02 +01:00
|
|
|
import copy
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...fake_data import create_student, create_students, create_year_group
|
|
|
|
from ...utils import (
|
|
|
|
_test_case_client,
|
|
|
|
_test_case_client_without_response,
|
|
|
|
assert_model_changes,
|
|
|
|
)
|
2023-01-08 17:15:02 +01:00
|
|
|
|
|
|
|
valid_data = {
|
2023-01-14 17:38:03 +01:00
|
|
|
"first_name": "Albert",
|
|
|
|
"last_name": "Rose",
|
|
|
|
"index": 234_343,
|
|
|
|
"email": "albert@gmail.com",
|
2023-01-08 17:15:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
new_data = {"first_name": "Martin", "last_name": "Green", "pesel": "93030312894"}
|
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",
|
|
|
|
"pesel": "93030312896",
|
|
|
|
"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:
|
|
|
|
yg = create_year_group()
|
|
|
|
create_students(yg, 45)
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
st = create_student(valid_data, yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data = _test_case_client_without_response(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/students/{st.index}/detail/",
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
st = create_student(valid_data, yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/students/{st.index}/",
|
|
|
|
None,
|
|
|
|
"Student was deleted!",
|
|
|
|
202,
|
|
|
|
method="delete",
|
|
|
|
)
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
st = create_student(valid_data, yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/students/{st.index}/",
|
|
|
|
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-14 17:38:03 +01:00
|
|
|
data["pesel"] = "43333333333433443"
|
2023-01-08 17:15:02 +01:00
|
|
|
with test_app_with_context.test_client() as client:
|
|
|
|
yg = create_year_group()
|
|
|
|
st = create_student(valid_data, yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
f"/api/coordinator/students/{st.index}/",
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
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-14 17:38:03 +01:00
|
|
|
data["pesel"] = "434343434343344"
|
2023-01-08 17:15:02 +01:00
|
|
|
with test_app_with_context.test_client() as client:
|
|
|
|
yg = create_year_group()
|
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",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_student_if_student_has_already_assigned_to_this_group(
|
|
|
|
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:
|
|
|
|
yg = create_year_group()
|
|
|
|
create_student(data, yg.id)
|
2023-01-14 17:38:03 +01:00
|
|
|
data["year_group_id"] = yg.id
|
|
|
|
_test_case_client(
|
|
|
|
client,
|
|
|
|
"/api/coordinator/students/",
|
|
|
|
data,
|
|
|
|
"You are assigned to this year group!",
|
|
|
|
400,
|
|
|
|
method="post",
|
|
|
|
key="error",
|
|
|
|
)
|