예제 #1
0
def test_remove_division_student(client: TestClient,
                                 superuser_token_headers: dict[str, str],
                                 db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    batch_number = randint(1, 5)
    for student in students:
        division.students.append({
            "student": student,
            "batch_number": batch_number
        })
    try:
        db.commit()
    except Exception as e:
        logging.error(e)
        db.rollback()
    r = client.delete(
        f"{settings.API_V1_STR}/divisions/{division.id}/students/{students[0].user_id}",
        headers=superuser_token_headers)
    assert r.status_code == 200
    updated_student = r.json()
    assert updated_student["user_id"] == students[0].user_id
    db.refresh(division)
    assert students[0] not in division.students
예제 #2
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))
예제 #3
0
def test_get_division_batch_students(client: TestClient,
                                     superuser_token_headers: dict[str, str],
                                     db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    batch_number = randint(1, 5)
    for student in students:
        division.students.append({
            "student": student,
            "batch_number": batch_number
        })
    try:
        db.commit()
    except exc.IntegrityError as e:
        logging.error(e.__str__())
        db.rollback()
    except Exception as e:
        logging.error(e.__str__())
        db.rollback()
    r = client.get(
        f"{settings.API_V1_STR}/divisions/{division.id}/students/{batch_number}",
        headers=superuser_token_headers)
    assert r.status_code == 200
    student_ids = [student.get("user_id") for student in r.json()]
    for student in students:
        assert student.user_id in student_ids
예제 #4
0
def test_add_division_students_no_student_object(client: TestClient,
                                                 superuser_token_headers: dict[
                                                     str, str],
                                                 db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    data = [student.user_id for student in students]
    student_no_object = create_random_student(
        db, school_id=course.term.year.school_id, term_id=course.term_id)
    crud.student.remove(db, id=student_no_object.user_id)
    data.append(student_no_object.user_id)
    r = client.post(f"{settings.API_V1_STR}/divisions/{division.id}/students",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 207
    response = r.json()
    assert response.get("errors").get(
        "no student object")[0] == student_no_object.user_id
    for student in students:
        assert student.user_id in response.get("success")
예제 #5
0
def test_get_division_students_professor(client: TestClient,
                                         db: Session) -> None:
    course = create_random_course(db)
    professor = create_random_user(db,
                                   type="professor",
                                   school_id=course.term.year.school_id)
    division = create_random_division(db,
                                      course_id=course.id,
                                      professor_id=professor.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    batch_number = randint(1, 5)
    for student in students:
        division.students.append({
            "student": student,
            "batch_number": batch_number
        })
    try:
        db.commit()
    except exc.IntegrityError as e:
        logging.error(e.__str__())
        db.rollback()
    except Exception as e:
        logging.error(e.__str__())
        db.rollback()
    r = client.get(
        f"{settings.API_V1_STR}/divisions/{division.id}/students",
        headers=authentication_token_from_email(client=client,
                                                db=db,
                                                email=professor.email),
    )
    assert r.status_code == 200
    assert r.json()
    fetched_students = [student.get("user_id") for student in r.json()]
    for student in students:
        assert student.user_id in fetched_students
예제 #6
0
def test_get_division_batch_students_admin_with_perms(client: TestClient,
                                                      db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    batch_number = randint(1, 5)
    for student in students:
        division.students.append({
            "student": student,
            "batch_number": batch_number
        })
    try:
        db.commit()
    except exc.IntegrityError as e:
        logging.error(e.__str__())
        db.rollback()
    except Exception as e:
        logging.error(e.__str__())
        db.rollback()
    perms = AdminPermissions(0)
    perms["course"] = True
    admin = create_random_user(db, type="admin", permissions=perms.permissions)
    r = client.get(
        f"{settings.API_V1_STR}/divisions/{division.id}/students/{batch_number}",
        headers=authentication_token_from_email(client=client,
                                                db=db,
                                                email=admin.email),
    )
    assert r.status_code == 200
    student_ids = [student.get("user_id") for student in r.json()]
    for student in students:
        assert student.user_id in student_ids
예제 #7
0
def test_add_division_students(client: TestClient,
                               superuser_token_headers: dict[str, str],
                               db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    students = [
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
        create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id),
    ]
    data = [student.user_id for student in students]
    r = client.post(f"{settings.API_V1_STR}/divisions/{division.id}/students",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 207
    assert r.json()
    fetched_students = [student_id for student_id in r.json()["success"]]
    for student in students:
        assert student.user_id in fetched_students
예제 #8
0
def test_add_division_students_duplicate_students(
        client: TestClient, superuser_token_headers: dict[str, str],
        db: Session) -> None:
    course = create_random_course(db)
    division = create_random_division(db, course_id=course.id)
    s = create_random_student(db,
                              school_id=course.term.year.school_id,
                              term_id=course.term_id)
    students = [s, s]
    data = [student.user_id for student in students]
    r = client.post(f"{settings.API_V1_STR}/divisions/{division.id}/students",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 207
    assert r.json()
    db.refresh(division)
    success = r.json().get("success")
    assert success
    assert s.user_id in [student_id for student_id in success]
    errors = r.json().get("errors")
    assert errors.get("student already in division")
    assert s.user_id in [
        student_id for student_id in errors.get("student already in division")
    ]