From 211c12eb1c2002945e228e0f33cd45d494df74ff Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 2 Mar 2017 20:33:34 +0530 Subject: removed pk/id based querying in test cases --- yaksh/test_models.py | 110 ++++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 53 deletions(-) (limited to 'yaksh/test_models.py') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 91d8806..ada5a65 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -44,7 +44,7 @@ def setUpModule(): tzinfo=pytz.utc), duration=30, active=True, attempts_allowed=1, time_between_attempts=0, - description='demo quiz', pass_criteria=0, + description='demo quiz 1', pass_criteria=0, language='Python', prerequisite=None, course=course, instructions="Demo Instructions") @@ -54,7 +54,7 @@ def setUpModule(): tzinfo=pytz.utc), duration=30, active=False, attempts_allowed=-1, time_between_attempts=0, - description='demo quiz', pass_criteria=40, + description='demo quiz 2', pass_criteria=40, language='Python', prerequisite=quiz, course=course, instructions="Demo Instructions") @@ -65,6 +65,8 @@ def tearDownModule(): User.objects.all().delete() Question.objects.all().delete() Quiz.objects.all().delete() + Course.objects.all().delete() + QuestionPaper.objects.all().delete() que_id_list = ["25", "22", "24", "27"] for que_id in que_id_list: @@ -75,9 +77,9 @@ def tearDownModule(): ############################################################################### class ProfileTestCases(unittest.TestCase): def setUp(self): - self.user1 = User.objects.get(pk=1) - self.profile = Profile.objects.get(pk=1) - self.user2 = User.objects.get(pk=3) + self.user1 = User.objects.get(username="demo_user") + self.profile = Profile.objects.get(user=self.user1) + self.user2 = User.objects.get(username='demo_user3') def test_user_profile(self): """ Test user profile""" @@ -92,9 +94,9 @@ class ProfileTestCases(unittest.TestCase): class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details - self.user1 = User.objects.get(pk=1) - self.user2 = User.objects.get(pk=2) - self.question1 = Question(summary='Demo question', + self.user1 = User.objects.get(username="demo_user") + self.user2 = User.objects.get(username="demo_user2") + self.question1 = Question.objects.create(summary='Demo Python 1', language='Python', type='Code', active=True, @@ -103,9 +105,8 @@ class QuestionTestCases(unittest.TestCase): snippet='def myfunc()', user=self.user1 ) - self.question1.save() - self.question2 = Question(summary='Demo Json', + self.question2 = Question.objects.create(summary='Demo Json', language='python', type='code', active=True, @@ -114,7 +115,6 @@ class QuestionTestCases(unittest.TestCase): snippet='def fact()', user=self.user2 ) - self.question2.save() # create a temp directory and add files for loading questions test file_path = "/tmp/test.txt" @@ -161,7 +161,7 @@ class QuestionTestCases(unittest.TestCase): def test_question(self): """ Test question """ - self.assertEqual(self.question1.summary, 'Demo question') + self.assertEqual(self.question1.summary, 'Demo Python 1') self.assertEqual(self.question1.language, 'Python') self.assertEqual(self.question1.type, 'Code') self.assertEqual(self.question1.description, 'Write a function') @@ -203,8 +203,8 @@ class QuestionTestCases(unittest.TestCase): """ Test load questions into database from json """ question = Question() result = question.load_questions(self.json_questions_data, self.user1) - question_data = Question.objects.get(pk=25) - file = FileUpload.objects.get(question=25) + question_data = Question.objects.get(summary="Json Demo") + file = FileUpload.objects.get(question=question_data) test_case = question_data.get_test_cases() self.assertEqual(question_data.summary, 'Json Demo') self.assertEqual(question_data.language, 'Python') @@ -220,10 +220,10 @@ class QuestionTestCases(unittest.TestCase): ############################################################################### 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.creator = User.objects.get(username="demo_user") + self.teacher = User.objects.get(username="demo_user2") + self.quiz1 = Quiz.objects.get(description='demo quiz 1') + self.quiz2 = Quiz.objects.get(description='demo quiz 2') self.trial_course = Course.objects.create_trial_course(self.creator) def test_quiz(self): @@ -234,7 +234,7 @@ class QuizTestCases(unittest.TestCase): '10:08:15') self.assertEqual(self.quiz1.duration, 30) self.assertTrue(self.quiz1.active) - self.assertEqual(self.quiz1.description, 'demo quiz') + self.assertEqual(self.quiz1.description, 'demo quiz 1') self.assertEqual(self.quiz1.language, 'Python') self.assertEqual(self.quiz1.pass_criteria, 0) self.assertEqual(self.quiz1.prerequisite, None) @@ -270,7 +270,9 @@ class QuizTestCases(unittest.TestCase): self.creator, True ) - self.assertEqual(trial_quiz.description, "Trial_orig_id_1_godmode") + self.assertEqual(trial_quiz.description, + "Trial_orig_id_{}_godmode".format(self.quiz1.id) + ) self.assertTrue(trial_quiz.is_trial) self.assertEqual(trial_quiz.duration, 1000) self.assertTrue(trial_quiz.active) @@ -285,7 +287,8 @@ class QuizTestCases(unittest.TestCase): self.creator, False ) - self.assertEqual(trial_quiz.description, "Trial_orig_id_2_usermode") + self.assertEqual(trial_quiz.description, + "Trial_orig_id_{}_usermode".format(self.quiz2.id)) self.assertTrue(trial_quiz.is_trial) self.assertEqual(trial_quiz.duration, self.quiz2.duration) self.assertEqual(trial_quiz.active, self.quiz2.active) @@ -316,7 +319,7 @@ class QuestionPaperTestCases(unittest.TestCase): def setUpClass(self): # All active questions self.questions = Question.objects.filter(active=True) - self.quiz = Quiz.objects.get(id=1) + self.quiz = Quiz.objects.get(description="demo quiz 1") # create question paper self.question_paper = QuestionPaper.objects.create(quiz=self.quiz, @@ -359,7 +362,7 @@ class QuestionPaperTestCases(unittest.TestCase): # ip address for AnswerPaper self.ip = '127.0.0.1' - self.user = User.objects.get(pk=1) + self.user = User.objects.get(username="demo_user") self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, @@ -373,7 +376,7 @@ class QuestionPaperTestCases(unittest.TestCase): def test_questionpaper(self): """ Test question paper""" - self.assertEqual(self.question_paper.quiz.description, 'demo quiz') + self.assertEqual(self.question_paper.quiz.description, 'demo quiz 1') self.assertSequenceEqual(self.question_paper.fixed_questions.all(), [self.questions[3], self.questions[5]]) self.assertTrue(self.question_paper.shuffle_questions) @@ -453,17 +456,17 @@ class AnswerPaperTestCases(unittest.TestCase): @classmethod def setUpClass(self): self.ip = '101.0.0.1' - self.user = User.objects.get(id=1) + self.user = User.objects.get(username='demo_user') self.profile = self.user.profile - self.quiz = Quiz.objects.get(pk=1) + self.quiz = Quiz.objects.get(description='demo quiz 1') 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.questions = Question.objects.all()[0:3] self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) - self.question1 = self.questions.get(id=1) - self.question2 = self.questions.get(id=2) - self.question3 = self.questions.get(id=3) + self.question1 = self.questions[0] + self.question2 = self.questions[1] + self.question3 = self.questions[2] # create answerpaper self.answerpaper = AnswerPaper(user=self.user, @@ -483,12 +486,12 @@ class AnswerPaperTestCases(unittest.TestCase): self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() # answers for the Answer Paper - self.answer_right = Answer(question=Question.objects.get(id=1), + self.answer_right = Answer(question=self.question1, answer="Demo answer", correct=True, marks=1, error=json.dumps([]) ) - self.answer_wrong = Answer(question=Question.objects.get(id=2), + self.answer_wrong = Answer(question=self.question2, answer="My answer", correct=False, marks=0, @@ -501,14 +504,17 @@ class AnswerPaperTestCases(unittest.TestCase): self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' + self.question1.summary = "Question1" self.question1.save() self.question2.language = 'python' self.question2.type = 'mcq' self.question2.test_case_type = 'mcqtestcase' + self.question2.summary = "Question2" self.question2.save() self.question3.language = 'python' self.question3.type = 'mcc' self.question3.test_case_type = 'mcqtestcase' + self.question3.summary = "Question3" self.question3.save() self.assertion_testcase = StandardTestCase( question=self.question1, @@ -660,21 +666,21 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(self.answerpaper.questions_left(), 3) # Test current_question() method of Answer Paper current_question = self.answerpaper.current_question() - self.assertEqual(current_question.id, 1) + self.assertEqual(current_question.summary, "Question1") # Test completed_question() method of Answer Paper - question = self.answerpaper.completed_question(1) + question = self.answerpaper.completed_question(self.question1.id) self.assertEqual(self.answerpaper.questions_left(), 2) # Test next_question() method of Answer Paper current_question = self.answerpaper.current_question() - self.assertEqual(current_question.id, 2) + self.assertEqual(current_question.summary, "Question2") # When next_question_id = self.answerpaper.next_question(current_question.id) # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.id, 3) + self.assertEqual(next_question_id.summary, "Question3") # Given, here question is already answered current_question_id = 1 @@ -684,7 +690,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.id, 2) + self.assertEqual(next_question_id.summary, "Question2") # Given, wrong question id current_question_id = 12 @@ -694,7 +700,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.id, 2) + self.assertEqual(next_question_id.summary, "Question2") # Given, last question in the list current_question_id = 3 @@ -704,7 +710,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.id, 2) + self.assertEqual(next_question_id.summary, "Question2") # Test get_questions_answered() method # When @@ -724,14 +730,14 @@ class AnswerPaperTestCases(unittest.TestCase): # Test completed_question and next_question # When all questions are answered - current_question = self.answerpaper.completed_question(2) + current_question = self.answerpaper.completed_question(self.question2.id) # Then self.assertEqual(self.answerpaper.questions_left(), 1) - self.assertEqual(current_question.id, 3) + self.assertEqual(current_question.summary, "Question3") # When - current_question = self.answerpaper.completed_question(3) + current_question = self.answerpaper.completed_question(self.question3.id) # Then self.assertEqual(self.answerpaper.questions_left(), 0) @@ -791,12 +797,12 @@ class AnswerPaperTestCases(unittest.TestCase): ############################################################################### class CourseTestCases(unittest.TestCase): def setUp(self): - self.course = Course.objects.get(pk=1) - self.creator = User.objects.get(pk=1) - self.student1 = User.objects.get(pk=2) - self.student2 = User.objects.get(pk=3) - self.quiz1 = Quiz.objects.get(pk=1) - self.quiz2 = Quiz.objects.get(pk=2) + self.course = Course.objects.get(name="Python Course") + self.creator = User.objects.get(username="demo_user") + self.student1 = User.objects.get(username="demo_user2") + self.student2 = User.objects.get(username="demo_user3") + self.quiz1 = Quiz.objects.get(description='demo quiz 1') + self.quiz2 = Quiz.objects.get(description='demo quiz 2') def test_is_creator(self): @@ -873,21 +879,19 @@ class CourseTestCases(unittest.TestCase): 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.assertEqual(self.creator, trial_course.creator) + self.assertIn(self.creator, trial_course.students.all()) self.assertTrue(trial_course.is_trial) ############################################################################### class TestCaseTestCases(unittest.TestCase): def setUp(self): - self.user = User.objects.get(pk=1) + self.user = User.objects.get(username="demo_user") self.question1 = Question(summary='Demo question 1', language='Python', type='Code', -- cgit From ac292ed1099b49f9d0e8a7ed3699bacdd8031de3 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Sat, 11 Mar 2017 02:17:41 +0530 Subject: made minor template changes and fixed test_models --- yaksh/test_models.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'yaksh/test_models.py') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index f0bb310..e6e4f74 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -669,7 +669,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(current_question.summary, "Question1") # Test completed_question() method of Answer Paper - question = self.answerpaper.completed_question(self.question1.id) + question = self.answerpaper.add_completed_question(self.question1.id) self.assertEqual(self.answerpaper.questions_left(), 2) # Test next_question() method of Answer Paper @@ -684,7 +684,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(next_question_id.summary, "Question3") # Given, here question is already answered - current_question_id = 1 + current_question_id = self.question1.id # When next_question_id = self.answerpaper.next_question(current_question_id) @@ -702,10 +702,10 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.summary, "Question2") + self.assertEqual(next_question_id.summary, "Question1") # Given, last question in the list - current_question_id = 3 + current_question_id = self.question3.id # When next_question_id = self.answerpaper.next_question(current_question_id) @@ -713,7 +713,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.summary, "Question2") + self.assertEqual(next_question_id.summary, "Question1") # Test get_questions_answered() method # When @@ -734,14 +734,18 @@ class AnswerPaperTestCases(unittest.TestCase): # Test completed_question and next_question # When all questions are answered - current_question = self.answerpaper.completed_question(self.question2.id) + current_question = self.answerpaper.add_completed_question( + self.question2.id + ) # Then self.assertEqual(self.answerpaper.questions_left(), 1) self.assertEqual(current_question.summary, "Question3") # When - current_question = self.answerpaper.completed_question(self.question3.id) + current_question = self.answerpaper.add_completed_question( + self.question3.id + ) # Then self.assertEqual(self.answerpaper.questions_left(), 0) -- cgit