diff options
Diffstat (limited to 'yaksh/test_models.py')
-rw-r--r-- | yaksh/test_models.py | 159 |
1 files changed, 151 insertions, 8 deletions
diff --git a/yaksh/test_models.py b/yaksh/test_models.py index c9cb953..4d91b27 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, \ - create_group + create_group, legend_display_types from yaksh.code_server import ( ServerPool, get_result as get_result_from_code_server ) @@ -21,6 +22,7 @@ import os import shutil import tempfile from threading import Thread +from collections import defaultdict from yaksh import settings @@ -144,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) @@ -163,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] @@ -285,19 +332,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): @@ -958,7 +1037,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() @@ -991,6 +1069,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, @@ -1105,6 +1184,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 @@ -1553,6 +1659,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): @@ -2052,3 +2176,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() |