Exemple #1
0
def test_course_set_assignment_visibility(db_test_client,
                                          a_files=[],
                                          t_files=[]):
    from application import db
    from .course_test import test_course_insert
    id, _ = test_course_insert(db_test_client)
    teacher = db.get_user("opettaja", "opettaja")

    visble_a_name = get_random_unicode(30)
    visble_reveal = pytz.utc.localize(
        datetime.datetime.utcnow()) - datetime.timedelta(hours=1)
    visble_deadline = random_datetime(start=visble_reveal)

    visble_a_id = db.insert_assignment(teacher.id, id, visble_a_name,
                                       visble_deadline, visble_reveal, a_files)

    a_name2 = get_random_unicode(30)
    reveal2 = pytz.utc.localize(
        datetime.datetime.utcnow()) + datetime.timedelta(hours=1)
    deadline2 = random_datetime(start=visble_reveal)

    a_id2 = db.insert_assignment(teacher.id, id, a_name2, deadline2, reveal2,
                                 a_files)

    points = random.randint(5, 20)
    t_desc = get_random_unicode(30)

    db.insert_task(teacher.id, visble_a_id, t_desc, points, t_files)
    points = random.randint(5, 20)
    t_desc = get_random_unicode(30)

    db.insert_task(teacher.id, a_id2, t_desc, points, t_files)
    cs = db.select_courses_teacher(teacher.id)
    assert len(cs) == 1
    c = cs[0]
    assert isinstance(c, Course)
    db.set_assignments(c, for_student=True)

    assert len(
        c.assignments) == 1, "can't see assignment that should be visible"
    assert isinstance(c.assignments[0], Assignment)
    assert c.assignments[0].id == visble_a_id, "assignment incorrect"
    assert c.assignments[0].name == visble_a_name, "assignment incorrect"
    assert c.assignments[0].reveal == visble_reveal, "assignment incorrect"
    assert c.assignments[0].deadline == visble_deadline, "assignment incorrect"

    cs = db.select_courses_teacher(teacher.id)
    assert len(cs) == 1
    c = cs[0]
    assert isinstance(c, Course)
    db.set_assignments(c, for_student=False)
    assert len(c.assignments) == 2
    first = Assignment(visble_a_id, visble_a_name,
                       visble_reveal.replace(tzinfo=None),
                       visble_deadline.replace(tzinfo=None), [])
    assert first in c.assignments

    second = Assignment(a_id2, a_name2, reveal2.replace(tzinfo=None),
                        deadline2.replace(tzinfo=None), [])
    assert second in c.assignments
Exemple #2
0
def test_simple_file_assignment_task(db_test_client):
    from application import db
    from .assignment_test import test_simple_assignment
    with generate_random_file() as temp, generate_random_file() as task_temp:
        file_name = get_random_unicode(10)
        task_file_name = get_random_unicode(10)
        
        werk_file = FileStorage(temp, file_name)
        werk_task_file = FileStorage(task_temp, task_file_name)


        test_simple_assignment(db_test_client)
        teach = db.get_user("opettaja", "opettaja")
        c = db.select_courses_teacher(teach.id)[0]
        name = get_random_unicode(10)
        reveal = pytz.utc.localize(datetime.datetime.now())
        deadline = random_datetime()


        a_id = db.insert_assignment(teach.id, c.id, name, deadline, reveal, [werk_file])
        
        task_name = get_random_unicode(10)
        points = random.randint(5,100)

        t_id = db.insert_task(teach.id, a_id, task_name, points, [werk_task_file])
        

        files = db.select_file_details(assignment_id=a_id)
        assert len(files)==1
        file = files[0]
        assert file.name == secure_filename(file_name)
        assert file.date.date() == datetime.date.today()
        
        temp.seek(0)
        bin_data = temp.read()
        db_bin_data, name=db.get_file(file.id)
        assert name == file.name

        assert len(bin_data) > 1000
        assert len(bin_data)==len(db_bin_data)
        assert type(bin_data)==type(db_bin_data), "Wrong types (can't compare) "+str(type(bin_data))+" vs "+str(type(db_bin_data))
        assert bin_data==db_bin_data

        task_files = db.select_file_details(task_id=t_id)

        assert len(task_files)==1
        file = task_files[0]
        task_temp.seek(0)
        real_task_bin_data = task_temp.read()
        db_bin_task_data, name = db.get_file(file.id)
        assert name == secure_filename(task_file_name)
        assert real_task_bin_data == db_bin_task_data
Exemple #3
0
def test_large_assignment_with_files_insert_with_access_rights(db_test_client):
    from application import db

    teachers = insert_users(db, 8, roles=["TEACHER"])
    
    assert len(teachers)>=8

    students = insert_users(db, 50, roles=["USER"])
    assert len(students) > 49


    all_courses = []
    for t in teachers:
        insert_courses(db, t.id, 6)
        t_c = db.select_courses_teacher(t.id)
        assert len(t_c) ==6
        all_courses+=t_c
    assert len(all_courses)
    student_enlists = {}
    for s in students:
        student_enlists[s.id] = []
        courses_to_enlist =random.sample(all_courses, random.randint(4,8))
        for c in courses_to_enlist:
            db.enlist_student(c.code, s.id)
            student_enlists[s.id].append(c)
    assert student_enlists
    file_storage = FileMultiDict()
    not_hidden_file_storage = FileMultiDict()
    assert len(teachers) == 8
    for t in teachers:
        courses = db.select_courses_teacher(t.id)
        assert len(courses) == 6
        for c in courses:
            for _ in range(5):                
                hidden = bool(random.randint(0,1))
                a_name, a_reveal, a_deadline = random_assignment(c.id, t.id, hidden=hidden)
                
                werk_files = []
                
                for _ in range(random.randint(2,4)):
                    file = generate_random_file(length=10)
                    file_name = secure_filename(get_random_unicode(20))
                    werk_file = FileStorage(file, file_name)
                    werk_files.append(werk_file)
                    


                a_id = db.insert_assignment(t.id, c.id, a_name, a_deadline, a_reveal,werk_files)
                with db.engine.connect() as conn:
                    sql = select([db.assignment]).where(db.assignment.c.id == a_id)
                    rs = conn.execute(sql)
                    row = rs.first()
                    assert row is not None
                    assert row[db.assignment.c.id] == a_id
                    assert row[db.assignment.c.name] == a_name
                    


                for file in werk_files:
                    file_storage.add_file("a"+str(a_id), file, file.filename)
                    if not hidden:
                        not_hidden_file_storage.add_file("a"+str(a_id), file, file.filename)

                    file.close()
                assert werk_files[0].filename
                assert len(file_storage.getlist("a"+str(a_id))) >=2
                assert file_storage.getlist("a"+str(a_id))[0].filename == werk_files[0].filename

                for __ in range(random.randint(3,5)):
                    werk_files = []
                    n = random.randint(1,3)
                    for _ in range(n):
                        file = generate_random_file(length=10)
                        file_name = secure_filename(get_random_unicode(20))
                        werk_file = FileStorage(file, file_name)
                        werk_files.append(werk_file)

                    task = random_task(a_id, werk_files)
                    task.id = db.insert_task(t.id, a_id, task.description, task.points, werk_files)
                    
                    for file in werk_files:
                        file_storage.add_file("t"+str(task.id), file, file.filename)
                        if not hidden:
                            not_hidden_file_storage.add_file("t"+str(task.id), file, file.filename)

                        file.close()

                    assert len(file_storage.getlist("t"+str(task.id))) == n
    
    for t in teachers:
        courses = db.select_courses_teacher(t.id)
        for c in courses:
            db.set_assignments(c, for_student=False)
            for a in c.assignments:
                assert isinstance(a, Assignment)
                assert not a.files
                a.files = db.select_file_details(assignment_id=a.id)

                correct_files = file_storage.getlist("a"+str(a.id))
                assert len(correct_files)>=2
                correct_file_names = [file.filename for file in correct_files]
                db_filenames = [file.name for file in a.files]
                case = unittest.TestCase()
                case.assertCountEqual(correct_file_names, db_filenames), "incorrect filenames in db filename for assign "+str(a.id) 
                for file in a.files:
                        db_files = db.select_file_details(file_id =file.id)
                        assert len(db_files)==1
                        assert db_files[0] == file
                for t in a.tasks:
                    assert isinstance(t, Task)
                    t_id = t.id
                    correct_files = file_storage.getlist("t"+str(t_id))
                    t.files = db.select_file_details(task_id=t_id)
                    assert len(correct_files)>=1
                    correct_file_names = [file.filename for file in correct_files]
                    db_filenames = [file.name for file in t.files]
                    case.assertCountEqual(correct_file_names, db_filenames), "incorrect filenames in db filename for task "+str(t.id)

                    for file in t.files:
                        db_files = db.select_file_details(file_id = file.id)
                        assert len(db_files)==1
                        assert db_files[0] == file

    assert len(students) > 30
    for s in students:
        courses = student_enlists[s.id]
        db_courses = db.select_courses_student(s.id)
        case = unittest.TestCase()
        case.assertCountEqual(courses, db_courses)
        assert db_courses
        for c in db_courses:
            db.set_assignments(c, for_student=True)
            for a in c.assignments:
                assert isinstance(a, Assignment)
                assert a.reveal <= pytz.utc.localize(datetime.datetime.utcnow())
                a.files=db.select_file_details(assignment_id=a.id)
                correct_files = not_hidden_file_storage.getlist("a"+str(a.id))
                
                correct_file_names = [file.filename for file in correct_files]
                db_filenames = [file.name for file in a.files]
                case = unittest.TestCase()
                case.assertCountEqual(correct_file_names, db_filenames) 
Exemple #4
0
def new_assignment(course_id):
    if current_user.role == "USER":
        app.logger.info("Student attempted to insert assignment")
        return redirect(url_for("course", course_id=course_id))

    if request.method == "GET":
        return render_template("/teacher/assignment/new.html",
                               id=course_id,
                               form=AssignmentForm())

    app.logger.info("attempting assignemnt add")
    form = AssignmentForm(request.form)
    if request.form.get("more") is not None:
        form.tasks.append_entry()
        app.logger.info("returning more tasks")
        return render_template("/teacher/assignment/new.html",
                               id=course_id,
                               form=form)
    if form.reveal.data is not None and form.deadline.data is not None and form.deadline.data < form.reveal.data:
        app.logger.info("date validations failed")
        return render_template(
            "/teacher/assignment/new.html",
            id=course_id,
            form=form,
            reveal_error=
            "Deadline ei voi olla ennen kuin tehtävä näkyy opiskelijoille!")
    files = request.files.getlist("files")
    app.logger.info("Adding assignment")
    for file in files:
        if not check_file(file):
            app.logger.info("File max size reached")
            return render_template(
                "/teacher/assignment/new.html",
                id=course_id,
                form=form,
                reveal_error=
                "Ainakin yhden lataamasi tiedoston koko oli liian suuri (max 50 Mb)"
            )

    deadline = None
    if form.deadline.data is not None:
        deadline = pytz.timezone("Europe/Helsinki").localize(
            form.deadline.data)

    reveal = None
    if form.reveal.data is not None:
        reveal = pytz.timezone("Europe/Helsinki").localize(form.reveal.data)

    i = 0
    for task in form.tasks.data:
        task_files = request.files.getlist("tasks-" + str(i) + "-task_files")
        for file in task_files:
            if not check_file(file):
                app.logger.info("File max size reached")
                return render_template(
                    "/teacher/assignment/new.html",
                    id=course_id,
                    form=form,
                    reveal_error=
                    "Ainakin yhden lataamasi tiedoston koko oli liian suuri (max 50 Mb)"
                )
        i += 1
    app.logger.info("attempting insert")
    assig_id = db.insert_assignment(current_user.get_id(), course_id,
                                    form.name.data, deadline, reveal, files)
    app.logger.info("insert successfull")
    i = 0
    for task in form.tasks.data:

        files = request.files.getlist("tasks-" + str(i) + "-task_files")

        app.logger.info("inserting tasks")
        db.insert_task(current_user.get_id(), i + 1, assig_id,
                       task.get("brief"), task.get("points"), files)
        i += 1
    app.logger.info("Everthing inserted! Assignment inserted")
    return redirect(url_for("index"))
def test_large_answer_insert_and_update(db_test_client):
    from application import db
    visible_reveal = pytz.utc.localize(
        datetime.datetime.utcnow()) - datetime.timedelta(minutes=1)
    teacher = insert_users(db, 1, roles=["TEACHER"])[0]
    course_id, _ = db.insert_course(
        Course("something", "somthing", visible_reveal, visible_reveal),
        teacher.id)
    f = generate_random_file()
    file_name = secure_filename(get_random_unicode(30))
    werk_file = FileStorage(f, file_name)
    assignment_id = db.insert_assignment(teacher.id, course_id, "somthing",
                                         random_datetime(), visible_reveal,
                                         [werk_file])
    werk_file.close()
    all_answers = []
    visible_answers = []
    visible_tasks = []
    correct_files = FileMultiDict()
    for _ in range(100):
        task_id = db.insert_task(teacher.id, assignment_id,
                                 get_random_unicode(20), 20, [])
        hidden = random.randint(0, 1)
        if not hidden:
            visible_tasks.append(task_id)
            reveal = visible_reveal
        else:
            reveal = random_datetime()
        files = []
        for i in range(3):
            f = generate_random_file(length=10)
            file_name = str(task_id) + "t" + str(i)
            werk_file = FileStorage(f, file_name)
            files.append(werk_file)
            correct_files.add_file(task_id, werk_file, werk_file.filename)
        desc = get_random_unicode(30)
        id = db.update_answer(teacher.id, task_id, files, desc, reveal=reveal)

        answer = Answer(id, desc, reveal.replace(tzinfo=None),
                        db.select_file_details(answer_id=id))
        all_answers.append(answer)
        if not hidden:
            visible_answers.append(answer)

    for f in files:
        f.close()
    assignment = db.select_assignment(assignment_id, for_student=False)
    all_db_answers = []
    case = unittest.TestCase()
    assert len(assignment.tasks) == 100
    for t in assignment.tasks:
        assert isinstance(t, Task)
        db.set_task_answer(t, for_student=False)
        assert t.answer is not None
        a = t.answer
        assert isinstance(a, Answer)
        assert len(a.files) == 3
        correct_filenames = [
            file.filename for file in correct_files.getlist(t.id)
        ]
        assert len(correct_filenames) == 3
        db_filenames = [file.name for file in a.files]

        case.assertCountEqual(db_filenames, correct_filenames)
        all_db_answers.append(a)

    assert len(all_answers) == len(all_db_answers)
    case.maxDiff = None
    case.assertCountEqual(all_answers, all_db_answers)
    assignment = db.select_assignment(assignment_id, for_student=True)
    assert len(assignment.tasks) == 100

    db_visibles = []
    for t in assignment.tasks:
        assert isinstance(t, Task)
        db.set_task_answer(t, for_student=True)
        if t.id not in visible_tasks:
            assert t.answer is None
            continue
        assert t.answer is not None
        db_visibles.append(t.answer)
        a = t.answer
        assert isinstance(a, Answer)
        assert len(a.files) == 3
        correct_filenames = [
            file.filename for file in correct_files.getlist(t.id)
        ]
        assert len(correct_filenames) == 3
        db_filenames = [file.name for file in a.files]

        case.assertCountEqual(db_filenames, correct_filenames)

    case.assertCountEqual(db_visibles, visible_answers)
def test_simple_answer(db_test_client):
    from application import db

    reveal = pytz.utc.localize(
        datetime.datetime.utcnow()) - datetime.timedelta(minutes=1)
    teacher = insert_users(db, 1, roles=["TEACHER"])[0]
    student = insert_users(db, 1, roles=["USER"])[0]
    c_id, code = db.insert_course(Course("something", "something", reveal),
                                  teacher.id)
    db.enlist_student(code, student.id)
    f = generate_random_file()
    file_name = secure_filename(get_random_unicode(30))
    werk_file = FileStorage(f, file_name)
    a_id = db.insert_assignment(teacher.id, c_id, "ksakas", reveal, reveal,
                                [werk_file])
    t_id = db.insert_task(teacher.id, a_id, "something", 3, [werk_file])
    werk_file.close()
    files = []
    correct_files = FileMultiDict()
    for _ in range(5):
        f = generate_random_file()
        file_name = secure_filename(get_random_unicode(30))
        werk_file = FileStorage(f, file_name)
        files.append(werk_file)
        correct_files.add_file(file_name, werk_file)

    description = get_random_unicode(100)
    db.update_answer(teacher.id,
                     t_id,
                     files,
                     description,
                     reveal=random_datetime())

    assignment = db.select_assignment(a_id, task_id=t_id)

    assert assignment.name == "ksakas"

    assert len(assignment.tasks) == 1
    t = assignment.tasks[0]
    assert isinstance(t, Task)
    db.set_task_answer(assignment.tasks[0], for_student=False)

    assert t.answer is not None
    assert isinstance(t.answer, Answer)
    assert t.answer.description == description

    assert len(t.answer.files) == 5

    for db_f in t.answer.files:
        assert isinstance(db_f, File)
        c_f = correct_files.get(db_f.name)

        bin_file, name = db.get_file(db_f.id)
        assert name == db_f.name, "shouldn't fail... " + str(
            type(name)) + "  " + str(type(db_f.name))
        c_f.seek(0)
        assert bin_file == c_f.read()
        c_f.close()

    db.set_task_answer(t, for_student=True)
    assert t.answer is None
Exemple #7
0
def test_simple_assignment(db_test_client, a_files=[], t_files=[]):
    from application import db
    from .course_test import test_course_insert
    id, code = test_course_insert(db_test_client)
    student = db.get_user("oppilas", "oppilas")
    teacher = db.get_user("opettaja", "opettaja")

    assert student is not None
    assert teacher is not None

    db.enlist_student(code, student.id)

    visible_a_name = get_random_unicode(30)
    reveal = random_datetime()
    deadline = random_datetime(start=reveal)

    a_id = db.insert_assignment(teacher.id, id, visible_a_name, deadline,
                                reveal, a_files)

    points = random.randint(5, 20)
    t_desc = get_random_unicode(30)

    t_id = db.insert_task(teacher.id, a_id, t_desc, points, t_files)

    with db.engine.connect() as conn:
        sql = select([db.assignment]).where(db.assignment.c.id == a_id)
        rs = conn.execute(sql)
        row = rs.first()
        assert row is not None
        assert row[db.assignment.c.id] == a_id
        assert row[db.assignment.c.name] == visible_a_name
        assert pytz.utc.localize(row[db.assignment.c.deadline]) == deadline
        assert pytz.utc.localize(row[db.assignment.c.reveal]) == reveal

        sql = select([db.task]).where(db.task.c.id == t_id)
        rs = conn.execute(sql)
        row = rs.first()

        assert row is not None
        assert row[db.task.c.id] == t_id
        assert row[db.task.c.description] == t_desc
        assert row[db.task.c.points] == points
        assert row[db.task.c.assignment_id] == a_id

        null = db.select_assignment(28128218)
        assert null is None

        a = db.select_assignment(a_id)
        assert isinstance(a, Assignment)
        assert len(a.tasks) == 1

        assert a.id == a_id
        assert a.name == visible_a_name
        assert a.deadline == deadline
        assert a.reveal == reveal

        t = a.tasks[0]

        assert isinstance(t, Task)
        assert t.id == t_id
        assert t.description == t_desc
        assert t.points == points
        assert t.assignment_id == a_id

        null = db.select_assignment(a_id, for_student=True)
        assert null is None
Exemple #8
0
def test_large_assignment_timezone_helsinki(db_test_client, files=[]):
    from application import db
    db.insert_user("opettaja1", "opettaja", "Who", "Cares", role="TEACHER")
    db.insert_user("opettaja2", "opettaja", "Who", "Cares", role="TEACHER")
    db.insert_user("opettaja3", "opettaja", "Who", "Cares", role="TEACHER")
    db.insert_user("opettaja4", "opettaja", "Who", "Cares", role="TEACHER")

    teacher1 = db.get_user("opettaja1", "opettaja")
    teacher2 = db.get_user("opettaja2", "opettaja")
    teacher3 = db.get_user("opettaja3", "opettaja")
    teacher4 = db.get_user("opettaja4", "opettaja")
    teachers = [teacher1, teacher2, teacher3, teacher4]

    assert teacher1 is not None
    assert teacher2 is not None
    assert teacher3 is not None
    assert teacher4 is not None

    teacher_assigments = []
    for teacher in teachers:
        all_assignments = {}
        visible_assignments = {}
        course_ids = insert_random_courses(teacher.id, db)
        assert len(course_ids) > 0
        for _ in range(random.randint(12, 21)):  #insert assignments
            name = get_random_unicode(15)
            course_id = random.choice(course_ids)
            if random.randint(0, 1) or not visible_assignments.get(course_id):
                visible = True
                reveal = pytz.timezone("Europe/Helsinki").localize(
                    datetime.datetime.now()) - datetime.timedelta(minutes=1)
            else:
                visible = False
                reveal = pytz.timezone("Europe/Helsinki").localize(
                    datetime.datetime.now()) + datetime.timedelta(
                        hours=random.randint(3, 5), minutes=1)
            deadline = random_datetime(start=reveal,
                                       time_zone="Europe/Helsinki")

            assig_id = db.insert_assignment(teacher1.id, course_id, name,
                                            deadline, reveal, files)
            tasks = []
            for _ in range(random.randint(2, 15)):
                task = random_task(assig_id, files)
                task_id = db.insert_task(teacher.id, assig_id,
                                         task.description, task.points, files)
                task.id = task_id
                tasks.append(task)
            result_dict = {
                "teacher_id": teacher.id,
                "id": assig_id,
                "reveal": reveal,
                "deadline": deadline,
                "course_id": course_id,
                "name": name,
                "files": files,
                "tasks": tasks
            }
            if not all_assignments.get(course_id):
                all_assignments[course_id] = [result_dict]
            else:
                all_assignments[course_id].append(result_dict)
            if visible:
                if not visible_assignments.get(course_id):
                    visible_assignments[course_id] = [result_dict]
                else:
                    visible_assignments[course_id].append(result_dict)

        temp = (teacher.id, all_assignments, visible_assignments)
        teacher_assigments.append(temp)

    for teacher_id, all_assignments, visible_assignments in teacher_assigments:
        courses = db.select_courses_teacher(teacher_id)
        assert len(courses)
        course_ids = [c.id for c in courses]
        for key in all_assignments.keys():
            assert key in course_ids
        for course in courses:
            correct_assignments = all_assignments.get(course.id, [])
            correct_visible_assignments = visible_assignments.get(course.id)

            db.set_assignments(course, for_student=False)
            course.set_timezones("Europe/Helsinki")
            assert len(course.assignments) == len(
                correct_assignments), "wrong number of assignments"

            for a in course.assignments:
                assert isinstance(a, Assignment)
                assignment_dic = get_correct_assignment(
                    a.id, correct_assignments)
                assert assignment_dic, "\n".join(
                    format_error(a, correct_assignments))
                assert assignment_dic["id"] == a.id
                assert assignment_dic["teacher_id"] == teacher_id

                assert assignment_dic["course_id"] == course.id
                assert a.deadline == assignment_dic["deadline"]
                assert a.reveal == assignment_dic["reveal"]
                case = unittest.TestCase()
                case.assertCountEqual(assignment_dic["tasks"], a.tasks)

            db.set_assignments(course, for_student=True)
            for a in course.assignments:
                assert isinstance(a, Assignment)
                assignment_dic = get_correct_assignment(
                    a.id, correct_visible_assignments)
                assert assignment_dic, "\n".join(
                    format_error(a, correct_visible_assignments))
                assert assignment_dic["id"] == a.id
                assert assignment_dic["teacher_id"] == teacher_id

                assert assignment_dic["course_id"] == course.id
                assert a.deadline == assignment_dic["deadline"]
                assert a.reveal == assignment_dic["reveal"]
                case = unittest.TestCase()
                case.assertCountEqual(assignment_dic["tasks"], a.tasks)
    return teachers