import datetime from app.base.mode import EnrollmentsMode from app.dependencies import db from app.examination_schedule.models import ExaminationSchedule, TermOfDefence from ...factory import ( ExaminationScheduleFactory, GroupFactory, ProjectSupervisorFactory, TemporaryAvailabilityFactory, TermOfDefenceFactory, YearGroupFactory, ) from ...utils import ( _test_case_client, _test_case_client_without_response, assert_model_changes, create_many_models, create_one_model, ) def get_data_of_term_of_defence(timedelta_minutes: int = 30) -> dict: ex = ExaminationSchedule.query.first() if ex is None: date = datetime.datetime.now() else: date = ex.start_date return { "start_date": (date + datetime.timedelta(days=1)).strftime( "%Y-%m-%dT%H:%M:%S.000Z" ), "end_date": ( date + datetime.timedelta(days=1, minutes=timedelta_minutes) ).strftime("%Y-%m-%dT%H:%M:%S.000Z"), } def test_list_of_term_of_defences(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) create_many_models(32, TermOfDefenceFactory, examination_schedule_id=ex.id) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defences/" data = _test_case_client_without_response(client, url, None, 200, method="get") assert len(data.get("term_of_defences")) == 32 def test_delete_term_of_defence(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/delete/{td.id}/", None, "Term of defence was deleted!", 200, method="delete", ) def test_delete_term_of_defence_if_term_of_defence_or_examination_schedule_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: _test_case_client( client, "/api/coordinator/enrollments/2/delete/1/", None, "Not found examination schedule or term of defence!", 404, method="delete", key="error", ) def test_create_term_of_defence(test_app_with_context, monkeypatch) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence() data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add", data, "Term of defence was created!", 201, method="post", ) td = TermOfDefence.query.first() assert_model_changes( td, {"start_date": data["start_date"], "end_date": data["end_date"]} ) assert {member.id for member in td.members_of_committee} == set( data["project_supervisors"] ) def test_create_many_term_of_defences(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "Term of defences was created!", 201, method="post", ) assert TermOfDefence.query.count() == 5 for td in TermOfDefence.query.all(): assert {member.id for member in td.members_of_committee} == set( data["project_supervisors"] ) def test_create_many_term_of_defences_with_invalid_id_of_chairman( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = 353 _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "Invalid id of chairman committee!", 400, method="post", key="error", ) def test_create_many_term_of_defences_if_examination_schedule_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id _test_case_client( client, "/api/coordinator/enrollments/32/add-term-of-defences/", data, "Not found examination schedule!", 404, method="post", key="error", ) def test_create_many_term_of_defences_if_project_supervisors_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [1, 2, 3] data["chairman_of_committee"] = 1 _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "Not found project supervisors!", 404, method="post", key="error", ) def test_create_many_term_of_defences_with_invalid_duration_time( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(133) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "Invalid duration time!", 400, method="post", key="error", ) def test_create_many_term_of_defences_if_start_date_is_greater_than_end_date( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(133) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id data["start_date"], data["end_date"] = data["end_date"], data["start_date"] _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "End date must be greater than start date!", 400, method="post", key="error", ) def test_create_many_term_of_defences_with_invalid_date_range( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) data = get_data_of_term_of_defence(120) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id data["end_date"] = (ex.end_date + datetime.timedelta(days=1)).strftime( "%Y-%m-%dT%H:%M:%S.000Z" ) _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/add-term-of-defences/", data, "Invalid date range!", 400, method="post", key="error", ) def test_update_term_of_defence(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) td.members_of_committee = project_supervisors td.chairman_of_committee = project_supervisors[0].id db.session.commit() data = get_data_of_term_of_defence(150) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[2].id _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "Term of defence was updated!", 200, method="put", ) assert td.chairman_of_committee == project_supervisors[2].id def test_update_term_of_defence_with_invalid_id_of_chairman( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = 353 _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "Invalid id of chairman committee!", 400, method="put", key="error", ) def test_update_term_of_defence_if_examination_schedule_or_term_of_defence_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: data = get_data_of_term_of_defence(150) data["project_supervisors"] = [1, 2, 3] data["chairman_of_committee"] = 1 _test_case_client( client, f"/api/coordinator/enrollments/32/update/23/", data, "Not found term of defence!", 404, method="put", key="error", ) def test_update_term_of_defence_if_project_supervisors_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) data = get_data_of_term_of_defence(150) data["project_supervisors"] = [1, 2, 3] data["chairman_of_committee"] = 1 _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "Not found project supervisors!", 404, method="put", key="error", ) def test_update_term_of_defence_with_invalid_duration_time( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) data = get_data_of_term_of_defence(133) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "Invalid duration time!", 400, method="put", key="error", ) def test_update_term_of_defence_if_start_date_is_greater_than_end_date( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) data = get_data_of_term_of_defence(133) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id data["start_date"], data["end_date"] = data["end_date"], data["start_date"] _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "End date must be greater than start date!", 400, method="put", key="error", ) def test_update_term_of_defence_with_invalid_date_range(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) project_supervisors = create_many_models( 3, ProjectSupervisorFactory, year_group_id=year_group.id ) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) data = get_data_of_term_of_defence(120) data["project_supervisors"] = [ps.id for ps in project_supervisors] data["chairman_of_committee"] = project_supervisors[0].id data["end_date"] = (ex.end_date + datetime.timedelta(days=1)).strftime( "%Y-%m-%dT%H:%M:%S.000Z" ) _test_case_client( client, f"/api/coordinator/enrollments/{ex.id}/update/{td.id}/", data, "Invalid date range!", 400, method="put", key="error", ) def test_list_temporary_availabilities(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) ps = create_one_model(ProjectSupervisorFactory, year_group_id=year_group.id) amount = 24 create_many_models( amount, TemporaryAvailabilityFactory, examination_schedule_id=ex.id, project_supervisor_id=ps.id, ) url = f"/api/coordinator/enrollments/{ex.id}/temporary-availabilities/" data = _test_case_client_without_response(client, url, None, 200, method="get") assert len(data.get("temporary_availabilities")) == amount def test_list_temporary_availabilities_if_examination_schedule_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: url = "/api/coordinator/enrollments/43/temporary-availabilities/" _test_case_client( client, url, None, "Not found examination schedule!", 404, method="get", key="error", ) def test_list_of_assigned_group_to_term_of_defences(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) amount = 5 groups = create_many_models(amount, GroupFactory) tds = create_many_models( 24, TermOfDefenceFactory, examination_schedule_id=ex.id ) for group, td in zip(groups, tds): td.group_id = group.id db.session.commit() url = ( f"/api/coordinator/enrollments/{ex.id}/assigned-group-to-term-of-defences/" ) data = _test_case_client_without_response(client, url, None, 200, method="get") term_of_defences = data.get("term_of_defences") assert len(term_of_defences) == amount def test_list_of_assigned_group_to_term_of_defences_if_examination_schedule_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: url = "/api/coordinator/enrollments/43/assigned-group-to-term-of-defences/" _test_case_client( client, url, None, "Not found examination schedule!", 404, method="get", key="error", ) def test_set_new_group_to_term_of_defence(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) group = create_one_model(GroupFactory) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": group.id}, "Group was added to term of defence!", 201, method="post", ) def test_set_new_group_to_term_of_defence_if_examination_schedule_or_term_of_defence_dont_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: url = "/api/coordinator/enrollments/34/term-of-defence/4/group/" _test_case_client( client, url, {"group_id": 2}, "Not found examination schedule or term of defence!", 404, method="post", key="error", ) def test_set_new_group_to_term_of_defence_if_group_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": 1}, "Not found group!", 404, method="post", key="error", ) def test_set_new_group_to_term_of_defence_if_(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) group = create_one_model(GroupFactory) create_one_model( TermOfDefenceFactory, examination_schedule_id=ex.id, group_id=group.id ) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": group.id}, "Group has already assigned to term of defence!", 400, method="post", key="error", ) assert td.group_id is None def test_delete_group_to_term_of_defence(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) group = create_one_model(GroupFactory) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": group.id}, "Group was deleted from this term of defence!", 200, method="delete", ) assert td.group_id is None def test_delete_group_to_term_of_defence_if_examination_schedule_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: url = "/api/coordinator/enrollments/3/term-of-defence/1/group/" _test_case_client( client, url, None, "Not found examination schedule or term of defence!", 404, method="delete", key="error", ) def test_update_group_for_term_of_defence(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) group2 = create_one_model(GroupFactory) td = create_one_model( TermOfDefenceFactory, examination_schedule_id=ex.id, group_id=group2.id ) group = create_one_model(GroupFactory) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": group.id}, "Group for term of defence was updated!", 200, method="put", ) assert td.group_id == group.id def test_update_group_for_term_of_defence_if_examination_schedule_or_term_of_defence_dont_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: url = "/api/coordinator/enrollments/34/term-of-defence/4/group/" _test_case_client( client, url, {"group_id": 2}, "Not found examination schedule or term of defence!", 404, method="put", key="error", ) def test_update_group_for_term_of_defence_if_group_doesnt_exist( test_app_with_context, ) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": 1}, "Not found group!", 404, method="put", key="error", ) def test_update_group_for_term_of_defence_of_defence_if_(test_app_with_context) -> None: with test_app_with_context.test_client() as client: year_group = create_one_model(YearGroupFactory) ex = create_one_model(ExaminationScheduleFactory, year_group_id=year_group.id) td = create_one_model(TermOfDefenceFactory, examination_schedule_id=ex.id) group = create_one_model(GroupFactory) create_one_model( TermOfDefenceFactory, examination_schedule_id=ex.id, group_id=group.id ) url = f"/api/coordinator/enrollments/{ex.id}/term-of-defence/{td.id}/group/" _test_case_client( client, url, {"group_id": group.id}, "Group has already assigned to term of defence!", 400, method="put", key="error", ) assert td.group_id is None