def test_course_insert(db_test_client): from application import db from .user_test import test_user_insert test_user_insert(db_test_client) student = db.get_user_by_id(1) teacher = db.get_user_by_id(2) assert student is not None assert teacher is not None name = "'öäasöä1ÅÄÖÅÄÅÖÄÅÖö23å231äl23ölasäösä" desc = "äöääöäpläplpä21äl.masalöas" c = Course(name, desc, datetime.date.today()) id, code = db.insert_course(c, teacher.id) assert id > 0 assert code is not None with db.engine.connect() as conn: sql = select([db.course]).where(db.course.c.teacher_id == teacher.id) rs = conn.execute(sql) row = rs.first() assert row[db.course.c.id] == id assert row[db.course.c.description] == desc assert row[db.course.c.name] == name return id, code
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
def test_select_file_details_multiple(db_test_client): from application import db db.insert_user("turha", "turha", "turha", "turha", role="TEACHER") user_id = db.get_user("turha","turha").id case = unittest.TestCase() case.maxDiff = None c_id, _ =db.insert_course(Course("something","somthing", datetime.datetime.now()),user_id) for _ in range(20): id_dic = create_dummy(db, user_id=user_id, course=c_id) correct_files= [] for i in range(20): rand_bytes= os.urandom(20) name = "task"+str(id_dic["task_id"])+str(i) id = file_insert_helper(db,binary_file=rand_bytes, task_id=id_dic["task_id"], name=name, user_id=user_id) correct_files.append(File(id, name, datetime.datetime.now())) files = db.select_file_details(task_id=id_dic["task_id"]) assert len(files) == 20 case.assertCountEqual(files, correct_files) correct_bytes, _ = db.get_file(id) assert rand_bytes == correct_bytes rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,submit_id=id_dic["submit_id"], name="submit_id"+str(id_dic["submit_id"]), user_id=user_id) sub_id = id_dic["submit_id"] files = db.select_file_details(submit_id=sub_id) assert len(files) == 1 assert files[0].name == "submit_id"+str(id_dic["submit_id"]) assert files[0].id == id correct_bytes, _ = db.get_file(id) assert rand_bytes == correct_bytes rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,assignment_id=id_dic["assignment_id"], name="assignment_id"+str(id_dic["task_id"]), user_id=user_id) files = db.select_file_details(assignment_id=id_dic["assignment_id"]) assert len(files) == 1 assert files[0].name == "assignment_id"+str(id_dic["assignment_id"]) assert files[0].id == id correct_bytes, _ = db.get_file(id) assert rand_bytes == correct_bytes rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,answer_id=id_dic["answer_id"], name="answer_id"+str(id_dic["task_id"]), user_id=user_id) files = db.select_file_details(answer_id=id_dic["answer_id"]) assert len(files) == 1 assert files[0].name == "answer_id"+str(id_dic["answer_id"]) assert files[0].id == id correct_bytes, _ = db.get_file(id) assert rand_bytes == correct_bytes
def test_invalid_signup(db_test_client): from application import db description = get_random_unicode(100) 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] id, code = db.insert_course(Course("something", "something",reveal), teacher.id) db.enlist_student(code, student.id) with pytest.raises(IntegrityError): db.enlist_student(code, student.id) with pytest.raises(ValueError): db.enlist_student("something", student.id)
def test_select_file_details_single(db_test_client): from application import db db.insert_user("turha", "turha", "turha", "turha", role="TEACHER") user_id = db.get_user("turha","turha").id c_id, _ =db.insert_course(Course("something","somthing", datetime.datetime.now()),user_id) for _ in range(20): id_dic = create_dummy(db, user_id=user_id, course=c_id) rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes, task_id=id_dic["task_id"], name="task"+str(id_dic["task_id"]), user_id=user_id) files = db.select_file_details(task_id=id_dic["task_id"]) assert len(files) == 1 assert files[0].name == "task"+str(id_dic["task_id"]) assert files[0].id == id assert rand_bytes, _ == db.get_file(id) rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,submit_id=id_dic["submit_id"], name="submit_id"+str(id_dic["submit_id"]), user_id=user_id) sub_id = id_dic["submit_id"] files = db.select_file_details(submit_id=sub_id) assert len(files) == 1 assert files[0].name == "submit_id"+str(id_dic["submit_id"]) assert files[0].id == id assert rand_bytes, _ == db.get_file(id) rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,assignment_id=id_dic["assignment_id"], name="assignment_id"+str(id_dic["task_id"]), user_id=user_id) files = db.select_file_details(assignment_id=id_dic["assignment_id"]) assert len(files) == 1 assert files[0].name == "assignment_id"+str(id_dic["assignment_id"]) assert files[0].id == id db_bytes, name = db.get_file(id) assert rand_bytes == db_bytes rand_bytes= os.urandom(20) id = file_insert_helper(db,binary_file=rand_bytes,answer_id=id_dic["answer_id"], name="answer_id"+str(id_dic["task_id"]), user_id=user_id) files = db.select_file_details(answer_id=id_dic["answer_id"]) assert len(files) == 1 assert files[0].name == "answer_id"+str(id_dic["answer_id"]) assert files[0].id == id assert rand_bytes, _ == db.get_file(id)
def new_course(): if current_user.role != "TEACHER": app.logger.info("non-teacher attempted insert") return redirect(url_for("index")) if request.method == "GET": return render_template("/teacher/new_course.html", form = CourseForm()) form = CourseForm(request.form) if not form.validate(): app.logger.info("form validation failed") render_template("/teacher/new_course.html", form = form) date = form.end_date.data if not date: date = datetime.date.today() id = db.insert_course(Course(form.name.data, form.description.data, form.end_date.data, time_zone="Europe/Helsinki"), current_user.get_id()) return redirect(url_for("courses"))
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)
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