Example #1
0
def test_create_division(client: TestClient,
                         superuser_token_headers: dict[str, str],
                         db: Session) -> None:
    course_id = create_random_course(db).id
    division_code = randint(1, 20)
    professor_id = create_random_professor(db).user_id
    data = {
        "course_id": course_id,
        "division_code": division_code,
        "professor_id": professor_id,
    }
    r = client.post(f"{settings.API_V1_STR}/divisions/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_division = r.json()
    fetched_division = crud.division.get_by_details(
        db,
        course_id=course_id,
        division_code=division_code,
    )
    assert fetched_division
    compare_api_and_db_query_results(created_division,
                                     to_json(fetched_division))
    compare_api_and_db_query_results(data, to_json(fetched_division))
Example #2
0
def test_get_all_timeslots(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    timeslot = create_random_timeslot(db)
    r = client.get(f"{settings.API_V1_STR}/timeslots/", headers=superuser_token_headers)
    assert r.status_code == 200
    results = r.json()
    assert results
    compare_api_and_db_query_results(api_result=results[-1], db_dict=to_json(timeslot))
Example #3
0
def test_create_year(client: TestClient, superuser_token_headers: dict[str,
                                                                       str],
                     db: Session) -> None:
    school_id = create_random_school(db).id
    start_year = datetime.now().year
    end_year = start_year + 1
    name = random_lower_string()
    data = {
        "name": name,
        "school_id": school_id,
        "start_year": start_year,
        "end_year": end_year,
    }
    r = client.post(f"{settings.API_V1_STR}/years/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_year = r.json()
    year = crud.year.get_by_details(db,
                                    name=name,
                                    school_id=school_id,
                                    start_year=start_year,
                                    end_year=end_year)
    assert year
    compare_api_and_db_query_results(api_result=created_year,
                                     db_dict=to_json(year))
    compare_api_and_db_query_results(data, created_year)
Example #4
0
def test_create_term(client: TestClient, superuser_token_headers: dict[str,
                                                                       str],
                     db: Session) -> None:
    name = random_lower_string()
    year_id = create_random_year(db=db).id
    start_date = datetime.now().date()
    end_date = start_date + timedelta(days=90)
    current_year_term = randint(1, 4)
    has_electives = choice([True, False])
    data = {
        "name": name,
        "year_id": year_id,
        "start_date": start_date.isoformat(),
        "end_date": end_date.isoformat(),
        "current_year_term": current_year_term,
        "has_electives": has_electives,
    }
    r = client.post(f"{settings.API_V1_STR}/terms/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_term = r.json()
    fetched_term = crud.term.get_by_details(
        db=db,
        name=name,
        year_id=year_id,
        current_year_term=current_year_term,
        start_date=start_date,
        end_date=end_date,
    )
    assert fetched_term
    compare_api_and_db_query_results(api_result=created_term,
                                     db_dict=to_json(fetched_term))
    compare_api_and_db_query_results(data, created_term)
Example #5
0
def test_create_course(client: TestClient, superuser_token_headers: dict[str,
                                                                         str],
                       db: Session) -> None:
    name = random_lower_string()
    course_code = random_lower_string()[:20]
    elective_code = random_lower_string()[:20]
    term_id = create_random_term(db).id
    data = {
        "name": name,
        "course_code": course_code,
        "elective_code": elective_code,
        "term_id": term_id,
    }
    r = client.post(f"{settings.API_V1_STR}/courses/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_course = r.json()
    fetched_course = crud.course.get_by_details(
        db,
        name=name,
        course_code=course_code,
        term_id=term_id,
    )
    assert fetched_course
    compare_api_and_db_query_results(api_result=created_course,
                                     db_dict=to_json(fetched_course))
    compare_api_and_db_query_results(data, created_course)
Example #6
0
def test_get_timeslot_existing(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    timeslot = create_random_timeslot(db)
    r = client.get(f"{settings.API_V1_STR}/timeslots/{timeslot.id}", headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_timeslot = r.json()
    assert fetched_timeslot
    compare_api_and_db_query_results(api_result=fetched_timeslot, db_dict=to_json(timeslot))
Example #7
0
def test_read_user_self(client: TestClient, db: Session) -> None:
    user = create_random_user(db, type="student")
    r = client.get(
        f"{settings.API_V1_STR}/users/{user.id}",
        headers=authentication_token_from_email(client=client, email=user.email, db=db),
    )
    assert r.status_code == 200
    fetched_user = r.json()
    compare_api_and_db_query_results(api_result=fetched_user, db_dict=to_json(user))
Example #8
0
def test_get_student_me_normal_student(client: TestClient, db: Session) -> None:
    student = create_random_student(db)
    r = client.get(
        f"{settings.API_V1_STR}/students/me",
        headers=authentication_token_from_email(client=client, email=student.user.email, db=db),
    )
    assert r.status_code == 200
    fetched_student = r.json()
    assert fetched_student
    compare_api_and_db_query_results(api_result=fetched_student, db_dict=to_json(student))
Example #9
0
def test_read_student_by_id_superuser(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    student = create_random_student(db)
    r = client.get(
        f"{settings.API_V1_STR}/students/{student.user_id}",
        headers=superuser_token_headers,
    )
    assert r.status_code == 200
    fetched_student = r.json()
    assert fetched_student
    compare_api_and_db_query_results(api_result=fetched_student, db_dict=to_json(student))
Example #10
0
def test_get_division_existing(client: TestClient,
                               superuser_token_headers: dict[str, str],
                               db: Session) -> None:
    division = create_random_division(db)
    r = client.get(f"{settings.API_V1_STR}/divisions/{division.id}",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_division = r.json()
    assert fetched_division
    compare_api_and_db_query_results(fetched_division, to_json(division))
Example #11
0
def test_get_all_divisions(client: TestClient,
                           superuser_token_headers: dict[str, str],
                           db: Session) -> None:
    division = create_random_division(db)
    r = client.get(f"{settings.API_V1_STR}/divisions/",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    results = r.json()
    assert results
    compare_api_and_db_query_results(results[-1], to_json(division))
Example #12
0
def test_get_school_superuser(client: TestClient,
                              superuser_token_headers: dict[str, str],
                              db: Session) -> None:
    school = create_random_school(db)
    r = client.get(f"{settings.API_V1_STR}/schools/{school.id}",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_school = r.json()
    assert fetched_school
    compare_api_and_db_query_results(api_result=fetched_school,
                                     db_dict=to_json(school))
Example #13
0
def test_get_school_valid_student(client: TestClient, db: Session) -> None:
    school = create_random_school(db)
    admin_user_token_headers = authentication_token_from_email(
        client=client, db=db, email=random_email(), school_id=school.id)
    r = client.get(f"{settings.API_V1_STR}/schools/{school.id}",
                   headers=admin_user_token_headers)
    assert r.status_code == 200
    fetched_school = r.json()
    assert fetched_school
    compare_api_and_db_query_results(api_result=fetched_school,
                                     db_dict=to_json(school))
Example #14
0
def test_get_existing_user(client: TestClient, superuser_token_headers: dict, db: Session) -> None:
    user = create_random_user(db=db, type="superuser")
    assert user
    user_id = user.id
    r = client.get(
        f"{settings.API_V1_STR}/users/{user_id}",
        headers=superuser_token_headers,
    )
    assert r.status_code == 200
    api_user = r.json()
    compare_api_and_db_query_results(api_result=api_user, db_dict=to_json(user))
Example #15
0
def test_get_year_existing(client: TestClient,
                           superuser_token_headers: dict[str, str],
                           db: Session) -> None:
    year = create_random_year(db)
    r = client.get(f"{settings.API_V1_STR}/years/{year.id}",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_year = r.json()
    assert fetched_year
    compare_api_and_db_query_results(api_result=fetched_year,
                                     db_dict=to_json(year))
Example #16
0
def test_get_lecture_superuser(client: TestClient,
                               superuser_token_headers: dict[str, str],
                               db: Session) -> None:
    lecture = create_random_lecture(db)
    r = client.get(f"{settings.API_V1_STR}/lectures/{lecture.id}",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_lecture = r.json()
    assert fetched_lecture
    compare_api_and_db_query_results(api_result=fetched_lecture,
                                     db_dict=to_json(lecture))
Example #17
0
def test_get_course_existing(client: TestClient,
                             superuser_token_headers: dict[str, str],
                             db: Session) -> None:
    course = create_random_course(db)
    r = client.get(f"{settings.API_V1_STR}/courses/{course.id}",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_course = r.json()
    assert fetched_course
    compare_api_and_db_query_results(api_result=fetched_course,
                                     db_dict=to_json(course))
Example #18
0
def test_update_user_superuser(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    user = create_random_user(db, type="student")
    full_name = random_lower_string()
    data = {"full_name": full_name}
    r = client.put(
        f"{settings.API_V1_STR}/users/{user.id}",
        headers=superuser_token_headers,
        json=data,
    )
    assert r.status_code == 200
    updated_user = r.json()
    db.refresh(user)
    compare_api_and_db_query_results(api_result=updated_user, db_dict=to_json(user))
Example #19
0
def test_read_professor_divisions_by_id_superuser(
        client: TestClient, superuser_token_headers: dict[str, str],
        db: Session) -> None:
    professor = create_random_user(db, type="professor")
    division = create_random_division(db, professor_id=professor.id)
    r = client.get(
        f"{settings.API_V1_STR}/professors/{professor.id}/divisions",
        headers=superuser_token_headers,
    )
    assert r.status_code == 200
    divisions = r.json()
    compare_api_and_db_query_results(api_result=divisions[-1],
                                     db_dict=to_json(division))
Example #20
0
def test_create_user_new_email(client: TestClient, superuser_token_headers: dict, db: Session) -> None:
    username = random_email()
    password = random_password()
    data = {"email": username, "password": password, "type": "superuser"}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    assert r.status_code == 200
    created_user = r.json()
    user = crud.user.get_by_email(db, email=username)
    assert user
    compare_api_and_db_query_results(api_result=created_user, db_dict=to_json(user))
Example #21
0
def test_get_all_professors(client: TestClient,
                            superuser_token_headers: dict[str, str],
                            db: Session) -> None:
    school = create_random_school(db)
    school_professor = create_random_user(db=db,
                                          type="professor",
                                          school_id=school.id)
    r = client.get(f"{settings.API_V1_STR}/schools/{school.id}/professors",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    fetched_professors = r.json()
    assert fetched_professors
    compare_api_and_db_query_results(api_result=fetched_professors[-1],
                                     db_dict=to_json(school_professor))
Example #22
0
def test_get_lecture_admin(client: TestClient, db: Session) -> None:
    lecture = create_random_lecture(db)
    perms = AdminPermissions(0)
    perms["school"] = True
    admin = create_random_user(db, "admin", permissions=perms.permissions)
    admin_user_token_headers = authentication_token_from_email(
        client=client, db=db, email=admin.email, user_type="admin")
    r = client.get(f"{settings.API_V1_STR}/lectures/{lecture.id}",
                   headers=admin_user_token_headers)
    assert r.status_code == 200
    fetched_lecture = r.json()
    assert fetched_lecture
    compare_api_and_db_query_results(api_result=fetched_lecture,
                                     db_dict=to_json(lecture))
Example #23
0
def test_read_professor_divisions_by_id_normal_professor_fetch_self(
        client: TestClient, db: Session) -> None:
    professor = create_random_user(db, type="professor")
    division = create_random_division(db, professor_id=professor.id)
    r = client.get(
        f"{settings.API_V1_STR}/professors/{professor.id}/divisions",
        headers=authentication_token_from_email(client=client,
                                                email=professor.email,
                                                db=db),
    )
    assert r.status_code == 200
    divisions = r.json()
    compare_api_and_db_query_results(api_result=divisions[-1],
                                     db_dict=to_json(division))
Example #24
0
def test_update_division(client: TestClient,
                         superuser_token_headers: dict[str, str],
                         db: Session) -> None:
    division = create_random_division(db)
    assert division.professor_id
    professor_id = create_random_professor(db).user_id
    data = {"professor_id": professor_id}
    r = client.put(f"{settings.API_V1_STR}/divisions/{division.id}",
                   headers=superuser_token_headers,
                   json=data)
    fetched_division = r.json()
    db.refresh(division)
    assert fetched_division
    compare_api_and_db_query_results(fetched_division, to_json(division))
Example #25
0
def test_update_students_superuser(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    student = create_random_student(db)
    term_id = create_random_term(db).id
    data = {"term_id": term_id}
    r = client.put(
        f"{settings.API_V1_STR}/students/{student.user_id}",
        headers=superuser_token_headers,
        json=data,
    )
    assert r.status_code == 200
    updated_student = r.json()
    assert updated_student
    db.refresh(student)
    compare_api_and_db_query_results(api_result=updated_student, db_dict=to_json(student))
Example #26
0
def test_update_timeslot(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
    timeslot = create_random_timeslot(db)
    start_time = datetime.now().time()
    end_time = (datetime.now() + timedelta(hours=1)).time()
    school_id = create_random_school(db).id
    data = {
        "start_time": start_time.isoformat(),
        "end_time": end_time.isoformat(),
        "school_id": school_id,
    }
    r = client.put(f"{settings.API_V1_STR}/timeslots/{timeslot.id}", headers=superuser_token_headers, json=data)
    fetched_timeslot = r.json()
    db.refresh(timeslot)
    assert fetched_timeslot
    compare_api_and_db_query_results(api_result=fetched_timeslot, db_dict=to_json(timeslot))
Example #27
0
def test_update_course(client: TestClient, superuser_token_headers: dict[str,
                                                                         str],
                       db: Session) -> None:
    course = create_random_course(db)
    assert course.name
    name = random_lower_string()
    data = {"name": name}
    r = client.put(f"{settings.API_V1_STR}/courses/{course.id}",
                   headers=superuser_token_headers,
                   json=data)
    fetched_course = r.json()
    db.refresh(course)
    assert fetched_course
    compare_api_and_db_query_results(api_result=fetched_course,
                                     db_dict=to_json(course))
Example #28
0
def test_create_school(client: TestClient, superuser_token_headers: dict[str,
                                                                         str],
                       db: Session) -> None:
    name = random_lower_string()
    head = random_lower_string()
    data = {"name": name, "head": head}
    r = client.post(f"{settings.API_V1_STR}/schools/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_school = r.json()
    school = crud.school.get_by_name(db, name=name)
    assert school
    compare_api_and_db_query_results(api_result=created_school,
                                     db_dict=to_json(school))
Example #29
0
def test_update_lecture(client: TestClient, superuser_token_headers: dict[str,
                                                                          str],
                        db: Session) -> None:
    lecture = create_random_lecture(db)
    new_timeslot = create_random_timeslot(db)
    data = {"time_slot_id": new_timeslot.id}
    r = client.put(f"{settings.API_V1_STR}/lectures/{lecture.id}",
                   headers=superuser_token_headers,
                   json=data)
    assert r.status_code == 200
    fetched_lecture = r.json()
    db.refresh(lecture)
    assert fetched_lecture
    compare_api_and_db_query_results(api_result=fetched_lecture,
                                     db_dict=to_json(lecture))
Example #30
0
def test_get_term_students(client: TestClient,
                           superuser_token_headers: dict[str, str],
                           db: Session) -> None:
    term = create_random_term(db=db)
    student = create_random_student(db=db, term_id=term.id)
    db.refresh(term)
    assert term.students[-1] == student
    r = client.get(f"{settings.API_V1_STR}/terms/{term.id}/students",
                   headers=superuser_token_headers)
    assert r.status_code == 200
    students = r.json()
    assert students
    for api_obj, db_obj in zip(students, term.students):
        compare_api_and_db_query_results(api_result=api_obj,
                                         db_dict=to_json(db_obj))