def setUpGradings(self): self.passGrade = Grading(title='passed', means_passed=True) self.passGrade.save() self.failGrade = Grading(title='failed', means_passed=False) self.failGrade.save() self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme') self.passFailGrading.save() self.passFailGrading.gradings.add(self.passGrade) self.passFailGrading.gradings.add(self.failGrade) self.passFailGrading.save()
def handle(self, *args, **options): # create demo users users = {} for name in ['demo_student', 'demo_tutor', 'demo_owner']: user = User.objects.filter(username=name).delete() user = User.objects.create_user(username=name, email='*****@*****.**', password=name) UserProfile(user=user).save() users[name] = user # create demo grading passGrade = Grading(title='passed', means_passed=True) passGrade.save() failGrade = Grading(title='failed', means_passed=False) failGrade.save() passFailGrading = GradingScheme( title='Pass/Fail Grading Scheme (Demo)') passFailGrading.save() passFailGrading.gradings.add(passGrade) passFailGrading.gradings.add(failGrade) passFailGrading.save() # create demo course course = Course( title='Demo Course', active=True, owner=users['demo_owner'], max_authors=3, ) course.save() course.tutors.add(users['demo_tutor']) course.participants.add(users['demo_student'].profile) # create demo assignment today = timezone.now() last_week = today - datetime.timedelta(weeks=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) ass = Assignment(title='Demo Assignment 1', course=course, download='http://example.org/assignments1.pdf', gradingScheme=passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False) ass.save() # create demo submission Submission(assignment=ass, submitter=users['demo_student'], notes="This is a demo submission.", state=Submission.SUBMITTED_TESTED).save()
def create_pass_fail_grading(): pass_grade = Grading(title=uccrap + 'passed', means_passed=True) pass_grade.save() fail_grade = Grading(title=uccrap + 'failed', means_passed=False) fail_grade.save() pass_fail_grading = GradingScheme(title=uccrap + 'Pass/Fail') pass_fail_grading.save() pass_fail_grading.gradings.add(pass_grade) pass_fail_grading.gradings.add(fail_grade) pass_fail_grading.save() return pass_fail_grading
class SubmitTestCase(LiveServerTestCase): ''' A test case base class with several resources being prepared: Users: - self.admin - self.teacher - self.another_teacher - self.tutor - self.enrolled_students - self.not_enrolled_students - self.current_user (the one currently logged-in) No user is logged-in after setup. Courses: - self.course (by self.teacher) - self.anotherCourse (by self.another_teacher) - self.inactiveCourse - self.all_courses Assignments: - self.openAssignment (in self.course) - self.validatedAssignment (in self.course) - self.softDeadlinePassedAssignment (in self.course) - self.hardDeadlinePassedAssignment (in self.course) - self.unpublishedAssignment (in self.course) - self.allAssignments Gradings: - self.passGrade - self.failGrade - self.passFailGrading The class offers some convinience functions: - createTestMachine(self, test_host) - createSubmissionFile(self) - createTestedSubmissionFile(self, test_machine) - createValidatableSubmission(self, user) - createValidatedSubmission(self, user, test_host='127.0.0.1') - createSubmission(self, user, assignment) ''' current_user = None def setUp(self): super(SubmitTestCase, self).setUp() self.logger = logging.getLogger('OpenSubmit') self.loggerLevelOld = self.logger.level self.logger.setLevel(logging.DEBUG) self.setUpUsers() self.setUpCourses() self.setUpGradings() self.setUpAssignments() def tearDown(self): self.logger.setLevel(self.loggerLevelOld) def createUser(self, user_dict): args = dict(user_dict) args['password'] = make_password(args['password']) user_obj = User(**args) user_obj.save() user_profile = UserProfile(user=user_obj) user_profile.save() user_dict['user'] = user_obj user_dict['profile'] = user_profile user_struct = AnonStruct(user_dict) return user_struct def loginUser(self, user_struct): assert (self.c.login(username=user_struct.username, password=user_struct.password)) uid = self.c.session['_auth_user_id'] user_struct.user = User.objects.get(pk=uid) self.current_user = user_struct def setUpUsers(self): self.c = Client() self.admin_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': True } self.admin = self.createUser(self.admin_dict) self.teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.teacher = self.createUser(self.teacher_dict) self.another_teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.another_teacher = self.createUser(self.another_teacher_dict) self.tutor_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.tutor = self.createUser(self.tutor_dict) self.enrolled_students = list() for i in range(0, 5): enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_enrolled_student{}@django.localhost.local'.format( i), 'is_staff': False, 'is_superuser': False, 'first_name': 'Harold', 'last_name': 'Finch' } self.enrolled_students.append( self.createUser(enrolled_student_dict)) self.not_enrolled_students = list() for i in range(0, 5): not_enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_not_enrolled_student{}@django.localhost.local'. format(i), 'is_staff': False, 'is_superuser': False } self.not_enrolled_students.append( self.createUser(not_enrolled_student_dict)) def setUpCourses(self): self.all_courses = [] self.course = Course( title='Active test course', active=True, owner=self.teacher.user, max_authors=3, ) self.course.save() self.course.tutors.add(self.tutor.user) for student in self.enrolled_students: self.course.participants.add(student.profile) self.all_courses.append(self.course) self.anotherCourse = Course( title='Another active test course', active=True, owner=self.another_teacher.user, max_authors=1, ) self.anotherCourse.save() self.all_courses.append(self.anotherCourse) self.inactiveCourse = Course( title='Inactive test course', active=False, owner=self.another_teacher.user, max_authors=1, ) self.inactiveCourse.save() self.all_courses.append(self.inactiveCourse) def setUpGradings(self): self.passGrade = Grading(title='passed', means_passed=True) self.passGrade.save() self.failGrade = Grading(title='failed', means_passed=False) self.failGrade.save() self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme') self.passFailGrading.save() self.passFailGrading.gradings.add(self.passGrade) self.passFailGrading.gradings.add(self.failGrade) self.passFailGrading.save() def setUpAssignments(self): from django.core.files import File as DjangoFile today = timezone.now() last_week = today - datetime.timedelta(weeks=1) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) # List of all assignments being assigned to the "self.course" course self.allAssignments = [] self.openAssignment = Assignment( title='Open assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False) self.openAssignment.save() self.allAssignments.append(self.openAssignment) self.anotherAssignment = Assignment( title='Another open assignment', course=self.anotherCourse, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False) self.anotherAssignment.save() self.fileAssignment = Assignment( title='File assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=True) self.fileAssignment.save() self.allAssignments.append(self.fileAssignment) self.validatedAssignment = Assignment( title='Validated assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=True, validity_script_download=True, attachment_test_validity=DjangoFile( open(rootdir + '/opensubmit/tests/validators/working.zip')), attachment_test_full=DjangoFile( open(rootdir + '/opensubmit/tests/validators/working.zip'))) self.validatedAssignment.save() self.allAssignments.append(self.validatedAssignment) self.softDeadlinePassedAssignment = Assignment( title='Soft deadline passed assignment', course=self.course, download='http://example.org/assignments/2/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=tomorrow, has_attachment=False, ) self.softDeadlinePassedAssignment.save() self.allAssignments.append(self.softDeadlinePassedAssignment) self.hardDeadlinePassedAssignment = Assignment( title='Hard deadline passed assignment', course=self.course, download='http://example.org/assignments/3/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=yesterday, has_attachment=False, ) self.hardDeadlinePassedAssignment.save() self.allAssignments.append(self.hardDeadlinePassedAssignment) self.unpublishedAssignment = Assignment( title='Unpublished assignment', course=self.course, download='http://example.org/assignments/4/download', gradingScheme=self.passFailGrading, publish_at=tomorrow, soft_deadline=next_week, hard_deadline=next_week, has_attachment=False, ) self.unpublishedAssignment.save() self.allAssignments.append(self.unpublishedAssignment) def createTestMachine(self, test_host): ''' Create test machine entry. The configuration information is expected to be some JSON dictionary, since this is normally directly rendered in the machine details view. ''' self.machine = TestMachine(last_contact=datetime.datetime.now(), host=test_host, config=json.dumps( {'Operating system': 'Plan 9'})) self.machine.save() return self.machine def createSubmissionFile( self, relpath="/opensubmit/tests/submfiles/working_withsubdir.zip"): from django.core.files import File as DjangoFile fname = relpath[relpath.rfind(os.sep) + 1:] shutil.copyfile(rootdir + relpath, settings.MEDIA_ROOT + fname) sf = SubmissionFile( attachment=DjangoFile(open(rootdir + relpath), unicode(fname))) sf.save() return sf def createTestedSubmissionFile(self, test_machine): ''' Create finalized test result in the database. ''' sf = self.createSubmissionFile() result_compile = SubmissionTestResult( kind=SubmissionTestResult.COMPILE_TEST, result="Compilation ok.", machine=test_machine, submission_file=sf).save() result_validity = SubmissionTestResult( kind=SubmissionTestResult.VALIDITY_TEST, result="Validation ok.", machine=self.machine, perf_data="41;42;43", submission_file=sf).save() result_full = SubmissionTestResult(kind=SubmissionTestResult.FULL_TEST, result="Full test ok.", perf_data="77;88;99", machine=self.machine, submission_file=sf).save() return sf def createValidatableSubmission(self, user): ''' Create a submission that can be validated by executor. ''' sf = self.createSubmissionFile() sub = Submission(assignment=self.validatedAssignment, submitter=user.user, notes="This is a validatable submission.", state=Submission.TEST_COMPILE_PENDING, file_upload=sf) sub.save() return sub def createValidatedSubmission(self, user, test_host='127.0.0.1'): ''' Create a submission that already has test results in the database. ''' machine = self.createTestMachine(test_host) sf = self.createTestedSubmissionFile(machine) sub = Submission(assignment=self.validatedAssignment, submitter=user.user, notes="This is an already validated submission.", state=Submission.SUBMITTED_TESTED, file_upload=sf) sub.save() return sub def createSubmission(self, user, assignment): sub = Submission(assignment=assignment, submitter=user.user, notes="This is a submission.", state=Submission.SUBMITTED) sub.save() return sub
def handle(self, *args, **options): print("Adding demo data ...") # create demo users users = {} for name in ['demo_student', 'demo_cheater', 'demo_tutor', 'demo_owner']: user = User.objects.filter(username=name).delete() user = User.objects.create_user(username=name, email='*****@*****.**', password=name, first_name=name, last_name=name) users[name] = user # create demo grading Grading.objects.filter(title='passed (demo)').delete() passGrade = Grading(title='passed (demo)', means_passed=True) passGrade.save() Grading.objects.filter(title='failed (demo)').delete() failGrade = Grading(title='failed (demo)', means_passed=False) failGrade.save() GradingScheme.objects.filter( title='Pass/Fail Grading Scheme (Demo)').delete() passFailGrading = GradingScheme( title='Pass/Fail Grading Scheme (Demo)') passFailGrading.save() passFailGrading.gradings.add(passGrade) passFailGrading.gradings.add(failGrade) passFailGrading.save() # create demo course Course.objects.filter(title='Demo Course').delete() course = Course( title='Demo Course', active=True, owner=users['demo_owner'], homepage="http://www.open-submit.org" ) course.save() course.tutors.add(users['demo_tutor']) course.participants.add(users['demo_student'].profile) course.participants.add(users['demo_cheater'].profile) today = timezone.now() last_week = today - datetime.timedelta(weeks=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) # create demo assignment ass1 = Assignment( title='Demo A1: Graded group work with deadline', course=course, download='http://example.org/assignments1.pdf', gradingScheme=passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=True, max_authors=3 ) ass1.save() # create demo assignment without grading ass2 = Assignment( title='Demo A2: Non-graded group work with deadline', course=course, download='http://example.org/assignments2.pdf', gradingScheme=None, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=True, max_authors=3 ) ass2.save() # create demo assignment without deadlines ass3 = Assignment( title='Demo A3: Graded group work without deadline, only notes', course=course, download='http://example.org/assignments1.pdf', gradingScheme=passFailGrading, publish_at=last_week, soft_deadline=None, hard_deadline=None, has_attachment=False, max_authors=3 ) ass3.save() # create demo assignment without deadlines ass4 = Assignment( title='Demo A4: Graded group work with deadline being over', course=course, download='http://example.org/assignments1.pdf', gradingScheme=passFailGrading, publish_at=last_week, soft_deadline=last_week, hard_deadline=last_week, has_attachment=False, max_authors=3 ) ass4.save() # create demo submission Submission( assignment=ass1, submitter=users['demo_student'], notes="Demo submission for A1.", state=Submission.SUBMITTED_TESTED, file_upload=createSubmissionFile() ).save() # create cheater submission in course 1 Submission( assignment=ass1, submitter=users['demo_cheater'], notes="Demo duplicate submission for A1.", state=Submission.SUBMITTED_TESTED, file_upload=createSubmissionFile() ).save()
class SubmitTestCase(LiveServerTestCase): current_user = None def createUser(self, user_dict): user_obj = User.objects.create_user( username=user_dict['username'], password=user_dict['password'], email=user_dict['email'], ) if 'is_superuser' in user_dict: user_obj.is_superuser = user_dict['is_superuser'] if 'is_staff' in user_dict: user_obj.is_staff = user_dict['is_staff'] user_obj.save() user_profile = UserProfile(user=user_obj) user_profile.save() user_dict['user'] = user_obj user_dict['profile'] = user_profile user_struct = AnonStruct(user_dict) return user_struct def loginUser(self, user_struct): result = self.c.login(username=user_struct.username, password=user_struct.password) if result: self.current_user = user_struct return result def setUpUsers(self): self.c = Client() self.admin_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_superuser': True, 'is_staff': True, } self.admin = self.createUser(self.admin_dict) self.teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.teacher = self.createUser(self.teacher_dict) self.another_teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.another_teacher = self.createUser(self.another_teacher_dict) self.tutor_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.tutor = self.createUser(self.tutor_dict) self.enrolled_students = list() for i in range(0, 5): enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_enrolled_student{}@django.localhost.local'.format( i), } self.enrolled_students.append( self.createUser(enrolled_student_dict)) self.not_enrolled_students = list() for i in range(0, 5): not_enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_not_enrolled_student{}@django.localhost.local'. format(i), } self.not_enrolled_students.append( self.createUser(not_enrolled_student_dict)) def setUpCourses(self): self.course = Course( title='Active test course', active=True, owner=self.teacher.user, max_authors=3, ) self.course.save() self.course.tutors.add(self.tutor.user) for student in self.enrolled_students: self.course.participants.add(student.profile) self.anotherCourse = Course( title='Another active test course', active=True, owner=self.another_teacher.user, max_authors=1, ) self.anotherCourse.save() self.inactiveCourse = Course( title='Inactive test course', active=False, owner=self.another_teacher.user, max_authors=1, ) self.inactiveCourse.save() def setUpGradings(self): self.passGrade = Grading(title='passed', means_passed=True) self.passGrade.save() self.failGrade = Grading(title='failed', means_passed=False) self.failGrade.save() self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme') self.passFailGrading.save() self.passFailGrading.gradings.add(self.passGrade) self.passFailGrading.gradings.add(self.failGrade) self.passFailGrading.save() def setUpAssignments(self): today = timezone.now() last_week = today - datetime.timedelta(weeks=1) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) self.openAssignment = Assignment( title='Open assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False, ) self.openAssignment.save() self.softDeadlinePassedAssignment = Assignment( title='Soft deadline passed assignment', course=self.course, download='http://example.org/assignments/2/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=tomorrow, has_attachment=False, ) self.softDeadlinePassedAssignment.save() self.hardDeadlinePassedAssignment = Assignment( title='Hard deadline passed assignment', course=self.course, download='http://example.org/assignments/3/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=yesterday, has_attachment=False, ) self.hardDeadlinePassedAssignment.save() self.unpublishedAssignment = Assignment( title='Unpublished assignment', course=self.course, download='http://example.org/assignments/4/download', gradingScheme=self.passFailGrading, publish_at=tomorrow, soft_deadline=next_week, hard_deadline=next_week, has_attachment=False, ) self.unpublishedAssignment.save() def setUp(self): super(SubmitTestCase, self).setUp() self.logger = logging.getLogger('OpenSubmit') self.loggerLevelOld = self.logger.level self.logger.setLevel(logging.WARN) self.setUpUsers() self.setUpCourses() self.setUpGradings() self.setUpAssignments() def tearDown(self): self.logger.setLevel(self.loggerLevelOld) def createSubmission(self, user, assignment, authors=[]): sub = Submission( assignment=assignment, submitter=user.user, notes="This is a submission.", ) sub.save() if authors: [sub.authors.add(author) for author in authors] sub.save() return sub
class SubmitTestCase(TestCase): current_user = None def createUser(self, user_dict): args = dict(user_dict) args['password'] = make_password(args['password']) user_obj = User(**args) user_obj.save() user_profile = UserProfile(user=user_obj) user_profile.save() user_dict['user'] = user_obj user_dict['profile'] = user_profile user_struct = AnonStruct(user_dict) return user_struct def loginUser(self, user_struct): assert(self.c.login(username=user_struct.username, password=user_struct.password)) uid = self.c.session['_auth_user_id'] user_struct.user = User.objects.get(pk=uid) self.current_user = user_struct def setUpUsers(self): self.c = Client() self.admin_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': True } self.admin = self.createUser(self.admin_dict) self.teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.teacher = self.createUser(self.teacher_dict) self.another_teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.another_teacher = self.createUser(self.another_teacher_dict) self.tutor_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, 'is_superuser': False } self.tutor = self.createUser(self.tutor_dict) self.enrolled_students = list() for i in range(0, 5): enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_enrolled_student{}@django.localhost.local'.format(i), 'is_staff': False, 'is_superuser': False, 'first_name': 'Harold', 'last_name': 'Finch' } self.enrolled_students.append(self.createUser(enrolled_student_dict)) self.not_enrolled_students = list() for i in range(0, 5): not_enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_not_enrolled_student{}@django.localhost.local'.format(i), 'is_staff': False, 'is_superuser': False } self.not_enrolled_students.append(self.createUser(not_enrolled_student_dict)) def setUpCourses(self): self.all_courses = [] self.course = Course( title='Active test course', active=True, owner=self.teacher.user, max_authors=3, ) self.course.save() self.course.tutors.add(self.tutor.user) for student in self.enrolled_students: self.course.participants.add(student.profile) self.all_courses.append(self.course) self.anotherCourse = Course( title='Another active test course', active=True, owner=self.another_teacher.user, max_authors=1, ) self.anotherCourse.save() self.all_courses.append(self.anotherCourse) self.inactiveCourse = Course( title='Inactive test course', active=False, owner=self.another_teacher.user, max_authors=1, ) self.inactiveCourse.save() self.all_courses.append(self.inactiveCourse) def setUpGradings(self): self.passGrade = Grading(title='passed', means_passed=True) self.passGrade.save() self.failGrade = Grading(title='failed', means_passed=False) self.failGrade.save() self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme') self.passFailGrading.save() self.passFailGrading.gradings.add(self.passGrade) self.passFailGrading.gradings.add(self.failGrade) self.passFailGrading.save() def setUpAssignments(self): from django.core.files import File as DjangoFile today = timezone.now() last_week = today - datetime.timedelta(weeks=1) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) self.allAssignments = [] self.openAssignment = Assignment( title='Open assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False ) self.openAssignment.save() self.allAssignments.append(self.openAssignment) self.validatedAssignment = Assignment( title='Validated assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=True, validity_script_download=True, attachment_test_validity=DjangoFile(open(rootdir+'/opensubmit/tests/validators/working.zip')), attachment_test_full=DjangoFile(open(rootdir+'/opensubmit/tests/validators/working.zip')) ) self.validatedAssignment.save() self.allAssignments.append(self.validatedAssignment) self.softDeadlinePassedAssignment = Assignment( title='Soft deadline passed assignment', course=self.course, download='http://example.org/assignments/2/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=tomorrow, has_attachment=False, ) self.softDeadlinePassedAssignment.save() self.allAssignments.append(self.softDeadlinePassedAssignment) self.hardDeadlinePassedAssignment = Assignment( title='Hard deadline passed assignment', course=self.course, download='http://example.org/assignments/3/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=yesterday, has_attachment=False, ) self.hardDeadlinePassedAssignment.save() self.allAssignments.append(self.hardDeadlinePassedAssignment) self.unpublishedAssignment = Assignment( title='Unpublished assignment', course=self.course, download='http://example.org/assignments/4/download', gradingScheme=self.passFailGrading, publish_at=tomorrow, soft_deadline=next_week, hard_deadline=next_week, has_attachment=False, ) self.unpublishedAssignment.save() self.allAssignments.append(self.unpublishedAssignment) def setUp(self): super(SubmitTestCase, self).setUp() self.logger = logging.getLogger('OpenSubmit') self.loggerLevelOld = self.logger.level self.logger.setLevel(logging.WARN) self.setUpUsers() self.setUpCourses() self.setUpGradings() self.setUpAssignments() def tearDown(self): self.logger.setLevel(self.loggerLevelOld) def createTestMachine(self, test_host): ''' Create test machine entry. The configuration information is expected to be some JSON dictionary, since this is normally directly rendered in the machine details view. ''' self.machine = TestMachine( last_contact=datetime.datetime.now(), host = test_host, config=json.dumps({'Operating system':'Plan 9'})) self.machine.save() return self.machine def createSubmissionFile(self): from django.core.files import File as DjangoFile sf = SubmissionFile(attachment=DjangoFile(open(rootdir+"/opensubmit/tests/submfiles/working_withsubdir.zip"), unicode("working_withsubdir.zip"))) sf.save() return sf def createTestedSubmissionFile(self, test_machine): ''' Create finalized test result in the database. ''' sf = self.createSubmissionFile() result_compile = SubmissionTestResult( kind=SubmissionTestResult.COMPILE_TEST, result="Compilation ok.", machine=test_machine, submission_file=sf ).save() result_validity = SubmissionTestResult( kind=SubmissionTestResult.VALIDITY_TEST, result="Validation ok.", machine=self.machine, perf_data = "41;42;43", submission_file=sf).save() result_full = SubmissionTestResult( kind=SubmissionTestResult.FULL_TEST, result="Full test ok.", perf_data = "77;88;99", machine=self.machine, submission_file=sf).save() return sf def createValidatableSubmission(self, user): ''' Create a submission that can be validated by executor. ''' sf = self.createSubmissionFile() sub = Submission( assignment=self.validatedAssignment, submitter=user.user, notes="This is a validatable submission.", state=Submission.TEST_COMPILE_PENDING, file_upload=sf ) sub.save() return sub def createValidatedSubmission(self, user, test_host='127.0.0.1'): ''' Create a submission that already has test results in the database. ''' machine = self.createTestMachine(test_host) sf = self.createTestedSubmissionFile(machine) sub = Submission( assignment=self.validatedAssignment, submitter=user.user, notes="This is an already validated submission.", state=Submission.SUBMITTED_TESTED, file_upload=sf ) sub.save() return sub def createSubmission(self, user, assignment, authors=[]): sub = Submission( assignment=assignment, submitter=user.user, notes="This is a submission.", state=Submission.SUBMITTED ) sub.save() if authors: [sub.authors.add(author) for author in authors] sub.save() return sub
class SubmitTestCase(LiveServerTestCase): current_user = None def createUser(self, user_dict): user_obj = User.objects.create_user( username=user_dict['username'], password=user_dict['password'], email=user_dict['email'], ) if 'is_superuser' in user_dict: user_obj.is_superuser = user_dict['is_superuser'] if 'is_staff' in user_dict: user_obj.is_staff = user_dict['is_staff'] user_obj.save() user_profile = UserProfile(user=user_obj) user_profile.save() user_dict['user'] = user_obj user_dict['profile'] = user_profile user_struct = AnonStruct(user_dict) return user_struct def loginUser(self, user_struct): result = self.c.login(username=user_struct.username, password=user_struct.password) if result: self.current_user = user_struct return result def setUpUsers(self): self.c = Client() self.admin_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_superuser': True, 'is_staff': True, } self.admin = self.createUser(self.admin_dict) self.teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.teacher = self.createUser(self.teacher_dict) self.another_teacher_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.another_teacher = self.createUser(self.another_teacher_dict) self.tutor_dict = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'is_staff': True, } self.tutor = self.createUser(self.tutor_dict) self.enrolled_students = list() for i in range(0, 5): enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_enrolled_student{}@django.localhost.local'.format(i), } self.enrolled_students.append(self.createUser(enrolled_student_dict)) self.not_enrolled_students = list() for i in range(0, 5): not_enrolled_student_dict = { 'username': '******'.format(i), 'password': '******'.format(i), 'email': 'testrunner_not_enrolled_student{}@django.localhost.local'.format(i), } self.not_enrolled_students.append(self.createUser(not_enrolled_student_dict)) def setUpCourses(self): self.course = Course( title='Active test course', active=True, owner=self.teacher.user, max_authors=3, ) self.course.save() self.course.tutors.add(self.tutor.user) for student in self.enrolled_students: self.course.participants.add(student.profile) self.anotherCourse = Course( title='Another active test course', active=True, owner=self.another_teacher.user, max_authors=1, ) self.anotherCourse.save() self.inactiveCourse = Course( title='Inactive test course', active=False, owner=self.another_teacher.user, max_authors=1, ) self.inactiveCourse.save() def setUpGradings(self): self.passGrade = Grading(title='passed', means_passed=True) self.passGrade.save() self.failGrade = Grading(title='failed', means_passed=False) self.failGrade.save() self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme') self.passFailGrading.save() self.passFailGrading.gradings.add(self.passGrade) self.passFailGrading.gradings.add(self.failGrade) self.passFailGrading.save() def setUpAssignments(self): today = timezone.now() last_week = today - datetime.timedelta(weeks=1) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) next_week = today + datetime.timedelta(weeks=1) self.openAssignment = Assignment( title='Open assignment', course=self.course, download='http://example.org/assignments/1/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=tomorrow, hard_deadline=next_week, has_attachment=False, ) self.openAssignment.save() self.softDeadlinePassedAssignment = Assignment( title='Soft deadline passed assignment', course=self.course, download='http://example.org/assignments/2/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=tomorrow, has_attachment=False, ) self.softDeadlinePassedAssignment.save() self.hardDeadlinePassedAssignment = Assignment( title='Hard deadline passed assignment', course=self.course, download='http://example.org/assignments/3/download', gradingScheme=self.passFailGrading, publish_at=last_week, soft_deadline=yesterday, hard_deadline=yesterday, has_attachment=False, ) self.hardDeadlinePassedAssignment.save() self.unpublishedAssignment = Assignment( title='Unpublished assignment', course=self.course, download='http://example.org/assignments/4/download', gradingScheme=self.passFailGrading, publish_at=tomorrow, soft_deadline=next_week, hard_deadline=next_week, has_attachment=False, ) self.unpublishedAssignment.save() def setUp(self): super(SubmitTestCase, self).setUp() self.logger = logging.getLogger('OpenSubmit') self.loggerLevelOld = self.logger.level self.logger.setLevel(logging.WARN) self.setUpUsers() self.setUpCourses() self.setUpGradings() self.setUpAssignments() def tearDown(self): self.logger.setLevel(self.loggerLevelOld) def createSubmission(self, user, assignment, authors=[]): sub = Submission( assignment=assignment, submitter=user.user, notes="This is a submission.", ) sub.save() if authors: [sub.authors.add(author) for author in authors] sub.save() return sub