diff options
Diffstat (limited to 'yaksh/test_models.py')
-rw-r--r-- | yaksh/test_models.py | 241 |
1 files changed, 208 insertions, 33 deletions
diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 14d5197..a60a1d6 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,10 +1,11 @@ import unittest from django.contrib.auth.models import Group +from django.core.files.uploadedfile import SimpleUploadedFile from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ StdIOBasedTestCase, FileUpload, McqTestCase, AssignmentUpload,\ LearningModule, LearningUnit, Lesson, LessonFile, CourseStatus, \ - TestCaseOrder, create_group + create_group, legend_display_types from yaksh.code_server import ( ServerPool, get_result as get_result_from_code_server ) @@ -21,11 +22,12 @@ import os import shutil import tempfile from threading import Thread +from collections import defaultdict from yaksh import settings def setUpModule(): - mod_group = Group.objects.create(name='moderator') + Group.objects.create(name='moderator') # create user profile user = User.objects.create_user(username='creator', @@ -125,6 +127,7 @@ class GlobalMethodsTestCases(unittest.TestCase): Group.objects.get(name='moderator') ) + ############################################################################### class LessonTestCases(unittest.TestCase): def setUp(self): @@ -143,14 +146,52 @@ class LearningModuleTestCases(unittest.TestCase): self.learning_module_two = LearningModule.objects.get(name='LM2') self.creator = User.objects.get(username='creator') self.student = User.objects.get(username='course_user') - self.learning_unit_one = LearningUnit.objects.get(order=1) - self.learning_unit_two = LearningUnit.objects.get(order=2) + self.learning_unit_one = LearningUnit.objects.get(id=1) + self.learning_unit_two = LearningUnit.objects.get(id=2) self.quiz = Quiz.objects.get(description='demo quiz 1') self.lesson = Lesson.objects.get(name='L1') self.course = Course.objects.get(name='Python Course') self.course_status = CourseStatus.objects.get( course=self.course, user=self.student) + self.prereq_course = Course.objects.create( + name="Prerequisite Course", + enrollment="Enroll Request", creator=self.creator + ) + + self.prereq_learning_module = LearningModule.objects.create( + name='LM3', description='module one', creator=self.creator + ) + self.test_learning_module = LearningModule.objects.create( + name='LM4', description='module two', + creator=self.creator, order=1 + ) + course_status = CourseStatus.objects.create( + course=self.prereq_course, user=self.student + ) + lesson = Lesson.objects.create( + name='P1', description='Video Lesson', + creator=self.creator + ) + learning_unit_lesson = LearningUnit.objects.create( + order=2, + lesson=lesson, + type='lesson' + ) + learning_unit_quiz = LearningUnit.objects.create( + order=1, + quiz=self.quiz, + type='quiz' + ) + self.prereq_learning_module.learning_unit.add(learning_unit_quiz) + self.prereq_learning_module.learning_unit.add(learning_unit_lesson) + self.prereq_learning_module.save() + self.prereq_course.learning_module.add(self.prereq_learning_module) + self.prereq_course.learning_module.add(self.test_learning_module) + self.prereq_course.students.add(self.student) + self.prereq_course.save() + + def tearDown(self): # Remove unit from course status completed units self.course_status.completed_units.remove(self.learning_unit_one) @@ -162,6 +203,13 @@ class LearningModuleTestCases(unittest.TestCase): self.assertTrue(self.learning_module.check_prerequisite) self.assertEqual(self.learning_module.order, 0) + def test_prerequisite_passes(self): + self.assertFalse( + self.test_learning_module.is_prerequisite_passed( + self.student, self.prereq_course + ) + ) + def test_get_quiz_units(self): # Given quizzes = [self.quiz] @@ -274,8 +322,12 @@ class LearningUnitTestCases(unittest.TestCase): def test_learning_unit(self): self.assertEqual(self.learning_unit_one.type, 'lesson') self.assertEqual(self.learning_unit_two.type, 'quiz') - self.assertEqual(self.learning_unit_one.lesson, self.lesson) - self.assertEqual(self.learning_unit_two.quiz, self.quiz) + self.assertEqual( + self.learning_unit_one.get_lesson_or_quiz(), self.lesson + ) + self.assertEqual( + self.learning_unit_two.get_lesson_or_quiz(), self.quiz + ) self.assertIsNone(self.learning_unit_one.quiz) self.assertIsNone(self.learning_unit_two.lesson) self.assertTrue(self.learning_unit_one.check_prerequisite) @@ -284,19 +336,51 @@ class LearningUnitTestCases(unittest.TestCase): class ProfileTestCases(unittest.TestCase): def setUp(self): - self.user1 = User.objects.get(username='creator') - self.profile = Profile.objects.get(user=self.user1) - self.user2 = User.objects.get(username='demo_user3') + self.creator = User.objects.get(username='creator') + self.profile = Profile.objects.get(user=self.creator) + self.teacher = User.objects.create_user( + username='teacher_profile', + password='teacher_profile', + email='teacher_profile@test.com') + Profile.objects.create( + user=self.teacher, roll_number=123, institute='IIT', + is_moderator=True, department='Chemical', position='Teacher' + ) + self.course = Course.objects.create( + name="Course For ProfileTestCase", + enrollment="Open Course", + creator=self.creator, + start_enroll_time=datetime( + 2015, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc + ), + end_enroll_time=datetime( + 2015, 11, 9, 10, 8, 15, 0, + tzinfo=pytz.utc + ), + ) + self.course.add_teachers(self.teacher) def test_user_profile(self): """ Test user profile""" - self.assertEqual(self.user1.username, 'creator') + self.assertEqual(self.creator.username, 'creator') self.assertEqual(self.profile.user.username, 'creator') self.assertEqual(int(self.profile.roll_number), 1) self.assertEqual(self.profile.institute, 'IIT') self.assertEqual(self.profile.department, 'Chemical') self.assertEqual(self.profile.position, 'Student') + def test_profile_is_moderator_removes_teacher(self): + teacher_profile = self.teacher.profile + teacher_profile.is_moderator = False + teacher_profile.save() + self.assertNotIn(self.teacher, self.course.teachers.all()) + + def tearDown(self): + self.teacher.profile.delete() + self.teacher.delete() + self.course.delete() + ############################################################################### class QuestionTestCases(unittest.TestCase): @@ -509,6 +593,46 @@ class QuestionTestCases(unittest.TestCase): ) self.assertEqual(msg, "Unable to parse test case data") + def test_get_test_case_options(self): + """ + Test if test case options are selected based on + question type and language + """ + + # Given + question_types = [ + "mcq", "integer", "float", "string", "arrange", "upload" + ] + que_list = [] + for i, q_type in enumerate(question_types, 1): + que_list.append(Question.objects.create( + summary='Python Question {0}'.format(i), language='python', + type=q_type, active=True, + description='{0} Question'.format(q_type), + points=1.0, user=self.user1 + )) + + # When + expected_tc_options = [ + ('standardtestcase', 'Standard TestCase'), + ('stdiobasedtestcase', 'StdIO TestCase'), + ('hooktestcase', 'Hook TestCase') + ] + other_tc_options = [ + ('mcqtestcase', 'Mcq TestCase'), + ('integertestcase', 'Integer TestCase'), + ('floattestcase', 'Float TestCase'), + ('stringtestcase', 'String TestCase'), + ('arrangetestcase', 'Arrange TestCase'), + ('hooktestcase', 'Hook TestCase') + ] + + # Then + obtained_tc_options = self.question2.get_test_case_options() + self.assertEqual(expected_tc_options, obtained_tc_options) + for que, tc_option in zip(que_list, other_tc_options): + self.assertEqual(que.get_test_case_options()[0], tc_option) + ############################################################################### class QuizTestCases(unittest.TestCase): @@ -957,7 +1081,6 @@ class AnswerPaperTestCases(unittest.TestCase): quiz=self.quiz2, total_marks=3 ) self.qtn_paper_with_single_question.save() - all_questions = Question.objects.filter(user=self.user).order_by("id") self.questions = all_questions[0:3] self.start_time = timezone.now() @@ -990,6 +1113,7 @@ class AnswerPaperTestCases(unittest.TestCase): ) self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() + # answers for the Answer Paper self.answer_right = Answer( question=self.question1, @@ -1104,6 +1228,33 @@ class AnswerPaperTestCases(unittest.TestCase): self.user2_answerpaper2 = self.question_paper.make_answerpaper( self.user2, self.ip, 1, self.course.id ) + self.questions_list = Question.objects.filter( + summary__in=summary_list[0:5]) + # create question_paper3 + self.question_paper3 = QuestionPaper( + quiz=self.quiz2, total_marks=3, shuffle_questions=True) + self.question_paper3.save() + question_list_with_only_one_category = [ + question for question in self.questions_list + if question.type == 'code'] + self.question_paper3.fixed_questions.add( + *question_list_with_only_one_category + ) + # create anspaper for user1 with questions of only one category + self.user1_answerpaper2 = self.question_paper3.make_answerpaper( + self.user, self.ip, 1, self.course.id + ) + # create question_paper4 + self.question_paper4 = QuestionPaper( + quiz=self.quiz, total_marks=0, shuffle_questions=True + ) + self.question_paper4.save() + + # create anspaper for user1 with no questions + self.user1_answerpaper3 = self.question_paper4.make_answerpaper( + self.user, self.ip, 1, self.course.id + ) + settings.code_evaluators['python']['standardtestcase'] = \ "yaksh.python_assertion_evaluator.PythonAssertionEvaluator" self.SERVER_POOL_PORT = 4000 @@ -1285,21 +1436,6 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) - def test_testcase_order(self): - testcase_ids = ",".join([str(ids) for ids in - self.question2.get_test_cases() - ]) - testcase_order = TestCaseOrder.objects.create( - answer_paper=self.answerpaper, - question=self.question2, - order=testcase_ids) - with self.assertRaises(IntegrityError): - TestCaseOrder.objects.create(answer_paper=self.answerpaper, - question=self.question2, - order=testcase_ids - ) - testcase_order.delete() - def test_validate_and_regrade_mcq_correct_answer(self): # Given mcq_answer = str(self.mcq_based_testcase.id) @@ -1567,6 +1703,24 @@ class AnswerPaperTestCases(unittest.TestCase): course=self.answerpaper.course ) + def test_get_categorized_question_indices_with_multiple_categories(self): + question_indices = {'Programming': [1], 'Objective Type': [2, 3]} + categorized_question_indices = \ + self.answerpaper.get_categorized_question_indices() + self.assertDictEqual(question_indices, categorized_question_indices) + + def test_get_categorized_question_indices_for_only_one_category(self): + question_indices = {'Programming': [1, 2, 3]} + categorized_question_indices = \ + self.user1_answerpaper2.get_categorized_question_indices() + self.assertDictEqual(question_indices, categorized_question_indices) + + def test_get_categorized_question_indices_for_no_questions(self): + question_indices = {} + categorized_question_indices = \ + self.user1_answerpaper3.get_categorized_question_indices() + self.assertDictEqual(question_indices, categorized_question_indices) + ############################################################################### class CourseTestCases(unittest.TestCase): @@ -1918,7 +2072,7 @@ class AssignmentUploadTestCases(unittest.TestCase): self.user2.last_name = "user3" self.user2.save() self.quiz = Quiz.objects.get(description="demo quiz 1") - + self.course = Course.objects.get(name="Python Course") self.questionpaper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=0.0, shuffle_questions=True ) @@ -1934,17 +2088,19 @@ class AssignmentUploadTestCases(unittest.TestCase): file_path2 = os.path.join(tempfile.gettempdir(), "upload2.txt") self.assignment1 = AssignmentUpload.objects.create( user=self.user1, assignmentQuestion=self.question, - assignmentFile=file_path1, question_paper=self.questionpaper + assignmentFile=file_path1, question_paper=self.questionpaper, + course=self.course ) self.assignment2 = AssignmentUpload.objects.create( user=self.user2, assignmentQuestion=self.question, - assignmentFile=file_path2, question_paper=self.questionpaper + assignmentFile=file_path2, question_paper=self.questionpaper, + course=self.course ) def test_get_assignments_for_user_files(self): assignment_files, file_name = AssignmentUpload.objects.get_assignments( self.questionpaper, self.question.id, - self.user1.id + self.user1.id, self.course.id ) self.assertIn("upload1.txt", assignment_files[0].assignmentFile.name) self.assertEqual(assignment_files[0].user, self.user1) @@ -1954,15 +2110,15 @@ class AssignmentUploadTestCases(unittest.TestCase): def test_get_assignments_for_quiz_files(self): assignment_files, file_name = AssignmentUpload.objects.get_assignments( - self.questionpaper - ) + self.questionpaper, course_id=self.course.id + ) files = [os.path.basename(file.assignmentFile.name) for file in assignment_files] question_papers = [file.question_paper for file in assignment_files] self.assertIn("upload1.txt", files) self.assertIn("upload2.txt", files) self.assertEqual(question_papers[0].quiz, self.questionpaper.quiz) - actual_file_name = self.quiz.description.replace(" ", "_") + actual_file_name = self.course.name.replace(" ", "_") file_name = file_name.replace(" ", "_") self.assertIn(actual_file_name, file_name) @@ -2064,3 +2220,22 @@ class CourseStatusTestCases(unittest.TestCase): # Test get course grade after completion self.assertEqual(self.course.get_grade(self.answerpaper1.user), 'B') + + +class FileUploadTestCases(unittest.TestCase): + def setUp(self): + self.question = Question.objects.get(summary='Q1') + self.filename = "uploadtest.txt" + self.uploaded_file = SimpleUploadedFile(self.filename, b'Test File') + self.file_upload = FileUpload.objects.create( + file=self.uploaded_file, + question=self.question + ) + + def test_get_file_name(self): + self.assertEqual(self.file_upload.get_filename(), self.filename) + + def tearDown(self): + if os.path.isfile(self.file_upload.file.path): + os.remove(self.file_upload.file.path) + self.file_upload.delete() |