diff options
Diffstat (limited to 'yaksh/test_models.py')
-rw-r--r-- | yaksh/test_models.py | 233 |
1 files changed, 159 insertions, 74 deletions
diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 37baf6e..fe0d3b5 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,11 +1,12 @@ import unittest from django.contrib.auth.models import Group +from django.contrib.contenttypes.models import ContentType 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, legend_display_types, Post, Comment + create_group, legend_display_types, Post, Comment, MicroManager from yaksh.code_server import ( ServerPool, get_result as get_result_from_code_server ) @@ -27,7 +28,7 @@ from yaksh import settings def setUpModule(): - Group.objects.create(name='moderator') + Group.objects.get_or_create(name='moderator') # create user profile user = User.objects.create_user(username='creator', @@ -103,7 +104,8 @@ def setUpModule(): course.save() LessonFile.objects.create(lesson=lesson) CourseStatus.objects.create(course=course, user=course_user) - + MicroManager.objects.create(manager=user, course=course, quiz=quiz, + student=course_user) def tearDownModule(): User.objects.all().delete() @@ -116,6 +118,7 @@ def tearDownModule(): LearningUnit.objects.all().delete() LearningModule.objects.all().delete() AnswerPaper.objects.all().delete() + MicroManager.objects.all().delete() Group.objects.all().delete() @@ -129,6 +132,141 @@ class GlobalMethodsTestCases(unittest.TestCase): ############################################################################### +class MicroManagerTestCase(unittest.TestCase): + def setUp(self): + self.micromanager = MicroManager.objects.first() + self.course = self.micromanager.course + quiz = self.micromanager.quiz + self.questionpaper = QuestionPaper.objects.create(quiz=quiz) + question = Question.objects.get(summary='Q1') + self.questionpaper.fixed_questions.add(question) + self.questionpaper.update_total_marks() + self.student = User.objects.get(username='course_user') + + def tearDown(self): + self.questionpaper.delete() + + def test_micromanager(self): + # Given + user = User.objects.get(username='creator') + course = Course.objects.get(name='Python Course', creator=user) + quiz = Quiz.objects.get(description='demo quiz 1') + student = User.objects.get(username='course_user') + + # When + micromanager = MicroManager.objects.first() + + # Then + self.assertIsNotNone(micromanager) + self.assertEqual(micromanager.manager, user) + self.assertEqual(micromanager.student, student) + self.assertEqual(micromanager.course, course) + self.assertEqual(micromanager.quiz, quiz) + self.assertFalse(micromanager.special_attempt) + self.assertEqual(micromanager.attempts_permitted, 0) + self.assertEqual(micromanager.attempts_utilised, 0) + self.assertEqual(micromanager.wait_time, 0) + self.assertEqual(micromanager.attempt_valid_for, 90) + self.assertEqual(user.micromanaging.first(), micromanager) + self.assertEqual(student.micromanaged.first(), micromanager) + + def test_set_wait_time(self): + # Given + micromanager = self.micromanager + + # When + micromanager.set_wait_time(days=2) + + # Then + self.assertEqual(micromanager.wait_time, 2) + + def self_increment_attempts_permitted(self): + # Given + micromanager = self.micromanager + + # When + micromanager.increment_attempts_permitted() + + # Then + self.assertEqual(micromanager.attempts_permitted, 1) + + def test_update_permitted_time(self): + # Given + micromanager = self.micromanager + permit_time = timezone.now() + + # When + micromanager.update_permitted_time(permit_time) + + # Then + self.assertEqual(micromanager.permitted_time, permit_time) + + def test_has_student_attempts_exhausted(self): + # Given + micromanager = self.micromanager + + # Then + self.assertFalse(micromanager.has_student_attempts_exhausted()) + + def test_has_quiz_time_exhausted(self): + # Given + micromanager = self.micromanager + + # Then + self.assertFalse(micromanager.has_quiz_time_exhausted()) + + def test_is_special_attempt_required(self): + # Given + micromanager = self.micromanager + attempt = 1 + ip = '127.0.0.1' + + # Then + self.assertFalse(micromanager.is_special_attempt_required()) + + # When + answerpaper = self.questionpaper.make_answerpaper(self.student, ip, + attempt, + self.course.id) + answerpaper.update_marks(state='completed') + + # Then + self.assertTrue(micromanager.is_special_attempt_required()) + + answerpaper.delete() + + def test_allow_special_attempt(self): + # Given + micromanager = self.micromanager + + # When + micromanager.allow_special_attempt() + + # Then + self.assertFalse(micromanager.special_attempt) + + def test_has_special_attempt(self): + # Given + micromanager = self.micromanager + + # Then + self.assertFalse(micromanager.has_special_attempt()) + + def test_is_attempt_time_valid(self): + # Given + micromanager = self.micromanager + + # Then + self.assertTrue(micromanager.is_attempt_time_valid()) + + def test_can_student_attempt(self): + # Given + micromanager = self.micromanager + + # Then + self.assertFalse(micromanager.can_student_attempt()) + + class LessonTestCases(unittest.TestCase): def setUp(self): self.lesson = Lesson.objects.get(name='L1') @@ -199,7 +337,7 @@ class LearningModuleTestCases(unittest.TestCase): def test_learning_module(self): self.assertEqual(self.learning_module.description, 'module one') self.assertEqual(self.learning_module.creator, self.creator) - self.assertTrue(self.learning_module.check_prerequisite) + self.assertFalse(self.learning_module.check_prerequisite) self.assertEqual(self.learning_module.order, 0) def test_prerequisite_passes(self): @@ -234,16 +372,16 @@ class LearningModuleTestCases(unittest.TestCase): self.assertEqual(module_quiz_lesson, quiz_lessons) def test_toggle_check_prerequisite(self): - self.assertTrue(self.learning_module.check_prerequisite) + self.assertFalse(self.learning_module.check_prerequisite) # When self.learning_module.toggle_check_prerequisite() # Then - self.assertFalse(self.learning_module.check_prerequisite) + self.assertTrue(self.learning_module.check_prerequisite) # When self.learning_module.toggle_check_prerequisite() # Then - self.assertTrue(self.learning_module.check_prerequisite) + self.assertFalse(self.learning_module.check_prerequisite) def test_get_next_unit(self): # Given @@ -329,8 +467,8 @@ class LearningUnitTestCases(unittest.TestCase): ) self.assertIsNone(self.learning_unit_one.quiz) self.assertIsNone(self.learning_unit_two.lesson) - self.assertTrue(self.learning_unit_one.check_prerequisite) - self.assertTrue(self.learning_unit_two.check_prerequisite) + self.assertFalse(self.learning_unit_one.check_prerequisite) + self.assertFalse(self.learning_unit_two.check_prerequisite) class ProfileTestCases(unittest.TestCase): @@ -842,7 +980,11 @@ class QuestionPaperTestCases(unittest.TestCase): total_marks=0.0, shuffle_questions=True ) - + self.question_paper_with_time_between_attempts.fixed_question_order = \ + "{0}, {1}".format(self.questions[3].id, self.questions[5].id) + self.question_paper_with_time_between_attempts.fixed_questions.add( + self.questions[3], self.questions[5] + ) self.question_paper.fixed_question_order = "{0}, {1}".format( self.questions[3].id, self.questions[5].id ) @@ -853,7 +995,7 @@ class QuestionPaperTestCases(unittest.TestCase): # create two QuestionSet for random questions # QuestionSet 1 self.question_set_1 = QuestionSet.objects.create( - marks=2, num_questions=2 + marks=1, num_questions=2 ) # add pool of questions for random sampling @@ -866,7 +1008,7 @@ class QuestionPaperTestCases(unittest.TestCase): # QuestionSet 2 self.question_set_2 = QuestionSet.objects.create( - marks=3, num_questions=3 + marks=1, num_questions=3 ) # add pool of questions @@ -933,7 +1075,7 @@ class QuestionPaperTestCases(unittest.TestCase): """ Test update_total_marks() method of Question Paper""" self.assertEqual(self.question_paper.total_marks, 0) self.question_paper.update_total_marks() - self.assertEqual(self.question_paper.total_marks, 15) + self.assertEqual(self.question_paper.total_marks, 7.0) def test_get_random_questions(self): """ Test get_random_questions() method of Question Paper""" @@ -1030,7 +1172,7 @@ class QuestionPaperTestCases(unittest.TestCase): qu_list = [str(self.questions_list[0]), str(self.questions_list[1])] trial_paper = \ QuestionPaper.objects.create_trial_paper_to_test_quiz( - self.trial_quiz, self.quiz.id + self.trial_quiz, self.quiz_with_time_between_attempts.id ) trial_paper.random_questions.add(self.question_set_1) trial_paper.random_questions.add(self.question_set_2) @@ -1646,18 +1788,7 @@ class AnswerPaperTestCases(unittest.TestCase): """ Test get_question_answer() method of Answer Paper""" questions = self.answerpaper.questions.all() answered = self.answerpaper.get_question_answers() - for question in questions: - answers_saved = Answer.objects.filter(question=question) - error_list = [json.loads(ans.error) for ans in answers_saved] - if answers_saved: - self.assertEqual(len(answered[question]), len(answers_saved)) - ans = [] - err = [] - for val in answered[question]: - ans.append(val.get('answer')) - err.append(val.get('error_list')) - self.assertEqual(set(ans), set(answers_saved)) - self.assertEqual(error_list, err) + self.assertEqual(list(questions), list(answered.keys())) def test_is_answer_correct(self): self.assertTrue(self.answerpaper.is_answer_correct(self.questions[0])) @@ -2288,9 +2419,10 @@ class PostModelTestCases(unittest.TestCase): enrollment='Enroll Request', creator=self.user3 ) + course_ct = ContentType.objects.get_for_model(self.course) self.post1 = Post.objects.create( title='Post 1', - course=self.course, + target_ct=course_ct, target_id=self.course.id, creator=self.user1, description='Post 1 description' ) @@ -2313,56 +2445,9 @@ class PostModelTestCases(unittest.TestCase): count = self.post1.get_comments_count() self.assertEquals(count, 2) - def test__str__(self): - self.assertEquals(str(self.post1.title), self.post1.title) - def tearDown(self): self.user1.delete() self.user2.delete() self.user3.delete() self.course.delete() self.post1.delete() - - -class CommentModelTestCases(unittest.TestCase): - def setUp(self): - self.user1 = User.objects.create( - username='bart', - password='bart', - email='bart@test.com' - ) - Profile.objects.create( - user=self.user1, - roll_number=1, - institute='IIT', - department='Chemical', - position='Student' - ) - self.course = Course.objects.create( - name='Python Course', - enrollment='Enroll Request', - creator=self.user1 - ) - self.post1 = Post.objects.create( - title='Post 1', - course=self.course, - creator=self.user1, - description='Post 1 description' - ) - self.comment1 = Comment.objects.create( - post_field=self.post1, - creator=self.user1, - description='Post 1 comment 1' - ) - - def test__str__(self): - self.assertEquals( - str(self.comment1.post_field.title), - self.comment1.post_field.title - ) - - def tearDown(self): - self.user1.delete() - self.course.delete() - self.post1.delete() - self.comment1.delete() |