diff options
Diffstat (limited to 'yaksh/tests.py')
-rw-r--r-- | yaksh/tests.py | 111 |
1 files changed, 104 insertions, 7 deletions
diff --git a/yaksh/tests.py b/yaksh/tests.py index f2083dd..441f0f6 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -4,8 +4,11 @@ from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ StdoutBasedTestCase import json from datetime import datetime, timedelta +from django.utils import timezone +import pytz from django.contrib.auth.models import Group + def setUpModule(): # create user profile user = User.objects.create_user(username='demo_user', @@ -31,16 +34,20 @@ def setUpModule(): Question.objects.create(summary='Q%d' % (i), points=1, type='code', user=user) # create a quiz - quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), - end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0), + quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), + end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), duration=30, active=True, attempts_allowed=1, time_between_attempts=0, description='demo quiz', pass_criteria=0, language='Python', prerequisite=None, course=course) - Quiz.objects.create(start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0), - end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), + Quiz.objects.create(start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), + end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), duration=30, active=False, attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, @@ -150,11 +157,15 @@ class QuestionTestCases(unittest.TestCase): self.assertTrue(question_data.active) self.assertEqual(question_data.snippet, 'def fact()') + ############################################################################### class QuizTestCases(unittest.TestCase): def setUp(self): + self.creator = User.objects.get(pk=1) + self.teacher = User.objects.get(pk=2) self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) + self.trial_course = Course.objects.create_trial_course(self.creator) def test_quiz(self): """ Test Quiz""" @@ -182,6 +193,50 @@ class QuizTestCases(unittest.TestCase): for quiz in quizzes: self.assertTrue(quiz.active) + def test_create_trial_quiz(self): + """Test to check if trial quiz is created""" + trial_quiz = Quiz.objects.create_trial_quiz(self.trial_course, + self.creator + ) + self.assertEqual(trial_quiz.course, self.trial_course) + self.assertEqual(trial_quiz.duration, 1000) + self.assertEqual(trial_quiz.description, "trial_questions") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.time_between_attempts, 0) + + def test_create_trial_from_quiz_godmode(self): + """Test to check if a copy of original quiz is created in godmode""" + trial_quiz = Quiz.objects.create_trial_from_quiz(self.quiz1.id, + self.creator, + True + ) + self.assertEqual(trial_quiz.description, "Trial_orig_id_1_godmode") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.duration, 1000) + self.assertTrue(trial_quiz.active) + self.assertEqual(trial_quiz.end_date_time, + datetime(2199, 1, 1, 0, 0, 0, 0) + ) + self.assertEqual(trial_quiz.time_between_attempts, 0) + + def test_create_trial_from_quiz_usermode(self): + """Test to check if a copy of original quiz is created in usermode""" + trial_quiz = Quiz.objects.create_trial_from_quiz(self.quiz2.id, + self.creator, + False + ) + self.assertEqual(trial_quiz.description, "Trial_orig_id_2_usermode") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.duration, self.quiz2.duration) + self.assertEqual(trial_quiz.active, self.quiz2.active) + self.assertEqual(trial_quiz.start_date_time, + self.quiz2.start_date_time + ) + self.assertEqual(trial_quiz.end_date_time, + self.quiz2.end_date_time + ) + self.assertEqual(trial_quiz.time_between_attempts, 0) + ############################################################################### class QuestionPaperTestCases(unittest.TestCase): @@ -239,6 +294,11 @@ class QuestionPaperTestCases(unittest.TestCase): user=self.user ) + # For Trial case + self.questions_list = [self.questions[3].id, self.questions[5].id] + trial_course = Course.objects.create_trial_course(self.user) + trial_quiz = Quiz.objects.create_trial_quiz(trial_course, self.user) + def test_questionpaper(self): """ Test question paper""" self.assertEqual(self.question_paper.quiz.description, 'demo quiz') @@ -291,6 +351,30 @@ class QuestionPaperTestCases(unittest.TestCase): # test can_attempt_now(self): self.assertFalse(self.question_paper.can_attempt_now(self.user)) + def test_create_trial_paper_to_test_quiz(self): + trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\ + (trial_quiz, + self.question_paper.id + ) + self.assertEqual(trial_paper.quiz, trial_quiz) + self.assertEqual(trial_paper.fixed_questions.all(), + self.question_paper.fixed_questions.all() + ) + self.assertEqual(trial_paper.random_questions.all(), + self.question_paper.random_questions.all() + ) + + def test_create_trial_paper_to_test_questions(self): + trial_paper = QuestionPaper.objects.\ + create_trial_paper_to_test_questions( + trial_quiz, self.questions_list + ) + self.assertEqual(trial_paper.quiz, trial_quiz) + self.assertEqual(self.questions_list, + self.question_paper.fixed_questions + .values_list("id", flat=True) + ) + ############################################################################### class AnswerPaperTestCases(unittest.TestCase): @@ -303,7 +387,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1,2,3]) - self.start_time = datetime.now() + self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) # create answerpaper @@ -386,7 +470,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertFalse(self.answerpaper.is_attempt_inprogress()) def test_set_end_time(self): - current_time = datetime.now() + current_time = timezone.now() self.answerpaper.set_end_time(current_time) self.assertEqual(self.answerpaper.end_time,current_time) @@ -427,6 +511,7 @@ class CourseTestCases(unittest.TestCase): self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) + def test_is_creator(self): """ Test is_creator method of Course""" self.assertTrue(self.course.is_creator(self.creator)) @@ -499,6 +584,18 @@ class CourseTestCases(unittest.TestCase): result = self.course.is_teacher(self.student2) self.assertTrue(result) + def test_create_trial_course(self): + """Test to check if trial course is created""" + # Test for manager method create_trial_course + trial_course = Course.objects.create_trial_course(self.creator) + self.assertEqual(trial_course.name, "trial_course") + self.assertEqual(trial_course.enrollment, "open") + self.assertTrue(trial_course.active) + self.assertEqual(trial_course.students.get(user=self.creator.id), + self.creator + ) + self.assertTrue(trial_course.is_trial) + ############################################################################### class TestCaseTestCases(unittest.TestCase): @@ -561,4 +658,4 @@ class TestCaseTestCases(unittest.TestCase): result = self.question1.consolidate_answer_data( user_answer="demo_answer" ) - self.assertEqual(result, self.answer_data_json)
\ No newline at end of file + self.assertEqual(result, self.answer_data_json) |