示例#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
示例#2
0
def test_large_course_insert(db_test_client):
    from application import db
    teachers = []
    for _ in range(random.randint(18,23)):
        username = get_random_unicode(35)
        password = get_random_unicode(30)
        first = get_random_unicode(15)
        last = get_random_unicode(25)

        db.insert_user(username, password, first, last, role="TEACHER")
        acc = db.get_user(username, password)
        assert acc.name == username
        assert acc.role == "TEACHER"
        teachers.append(acc)
    teach_courses = {}
    for t in teachers:
        teach_courses[t.id]=[]
        for _ in range(random.randint(12,20)):
            course = Course(get_random_unicode(20), get_random_unicode(40), random_datetime())
            id, code = db.insert_course(course, t.id)
            course.id = id
            course.code = code
            teach_courses[t.id].append(course)
    random.shuffle(teachers)
    for t in teachers:
        db_courses = db.select_courses_teacher(t.id)
        assert len(db_courses)>=12
        case = unittest.TestCase()
        case.assertCountEqual(db_courses, teach_courses[t.id])
    return teachers
示例#3
0
def courses():
    if current_user.role == "TEACHER":
        courses = db.select_courses_teacher(current_user.get_id())
        set_course_counts(courses, db.count_students(current_user.get_id()))
        return render_template("/teacher/teacher_list.html", courses=courses)
    else:
        courses = db.select_courses_student(current_user.get_id())
        return render_template("/student/student_list.html", courses=courses)
示例#4
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
示例#5
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) 
示例#6
0
def test_large_course_signup(db_test_client):
    from application import db
    from .user_test import test_weird_chars_large_set
    ids = test_weird_chars_large_set(db_test_client, random_roles=False)
    
    
    now = datetime.datetime.now()
    courses = []
    teacher_courses = {}
    accs = []
    for id in ids:
        acc = db.get_user_by_id(id)
        assert acc.id == id
        assert acc.role == "TEACHER" or acc.role=="USER"

        accs.append(acc)
        if acc.role == "TEACHER":
            teacher_courses[id] = []
            for _ in range(random.randint(14,16)):
                
                name = get_random_unicode(10)
                description = get_random_unicode(20)
                random_time = now + datetime.timedelta(days=random.randint(0, 30))
                c = Course(name, description, random_time, teacher_id=id)
                course_id, code = db.insert_course(c, id)
                c.id = course_id
                c.code = code
                assert c.teacher_id is not None
                courses.append(c)
                teacher_courses[id].append(c)
    student_enlists = {}
    for acc in accs:
        if acc.role == "USER":
            student_enlists[acc.id]=[]
            enlists = random.sample(courses, k=random.randint( len(courses)//2, len(courses)-1 ))
            for c in enlists:
                student_enlists[acc.id].append(c)
                db.enlist_student(c.code, acc.id)

    for acc in accs:
        if acc.role =="TEACHER":
            courses = db.select_courses_teacher(acc.id)
            real = teacher_courses[acc.id]
            assert len(real) == len(courses)
            assert len(real) > 10
            case = unittest.TestCase()
            case.assertCountEqual(courses, real)
            for c in courses:
                assert c.teacher_id == acc.id
                assert c in real, str(c)+" not found in"+", ".join(str(a) for a in real)
            
            for c in real:
                assert c in courses, str(c)+" not found in"+", ".join(str(c) for c in real)
        else:
            courses = db.select_courses_student(acc.id)
            real = student_enlists[acc.id]

            assert len(real) == len(courses)
            assert len(real) > 6
            case = unittest.TestCase()
            case.assertCountEqual(courses, real)
            for c in courses:
                assert c.teacher_id is not None
                assert c in real, str(c)+"not found in"+", ".join(str(c) for c in real)
            for c in real:
                assert c in courses, str(c)+"not found in"+", ".join(str(c) for c in real)
示例#7
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