diff options
Diffstat (limited to 'api/tests.py')
-rw-r--r-- | api/tests.py | 244 |
1 files changed, 131 insertions, 113 deletions
diff --git a/api/tests.py b/api/tests.py index 06f45d6..feffcc0 100644 --- a/api/tests.py +++ b/api/tests.py @@ -37,7 +37,6 @@ class QuestionListTestCase(TestCase): Question.objects.create(summary='test question 2', language='python', type='mcq', user=self.user) - def test_get_all_questions_anonymous(self): # When response = self.client.get(reverse('api:questions')) @@ -55,7 +54,7 @@ class QuestionListTestCase(TestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 2) self.assertEqual(response.data, serializer.data) - + def test_create_question_invalid_data(self): # Given data = {'summary': 'Add test question', 'user': self.user.id} @@ -65,7 +64,7 @@ class QuestionListTestCase(TestCase): # Then self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(Question.objects.filter( - summary='Add test question').exists()) + summary='Add test question').exists()) def test_create_question_valid_data(self): # Given @@ -96,13 +95,14 @@ class QuestionDetailTestCase(TestCase): self.user = User.objects.create_user(username=self.username, password=self.password) self.otheruser = User.objects.create_user(username=self.otherusername, - password=self.password) + password=self.password) Question.objects.create(summary='test question', language='python', type='mcq', user=self.user) Question.objects.create(summary='delete question', language='python', type='mcq', user=self.user) - Question.objects.create(summary='Created by other user', language='python', - type='mcq', user=self.otheruser) + Question.objects.create(summary='Created by other user', + language='python', type='mcq', + user=self.otheruser) def test_get_question_anonymous(self): # Given @@ -152,7 +152,7 @@ class QuestionDetailTestCase(TestCase): 'language': 'python', 'type': 'mcq', 'user': self.user.id} # When response = self.client.put(reverse('api:question', - kwargs={'pk': question.id}), data) + kwargs={'pk': question.id}), data) # Then self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) @@ -163,7 +163,7 @@ class QuestionDetailTestCase(TestCase): # When self.client.login(username=self.username, password=self.password) response = self.client.put(reverse('api:question', - kwargs={'pk': question.id}), data) + kwargs={'pk': question.id}), data) # Then self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) @@ -175,7 +175,7 @@ class QuestionDetailTestCase(TestCase): # When self.client.login(username=self.username, password=self.password) response = self.client.put(reverse('api:question', - kwargs={'pk': question.id}), data) + kwargs={'pk': question.id}), data) # Then self.assertEqual(response.status_code, status.HTTP_200_OK) question = Question.objects.get(pk=question.pk) @@ -186,28 +186,28 @@ class QuestionDetailTestCase(TestCase): question = Question.objects.get(summary='delete question') # When response = self.client.delete(reverse('api:question', - kwargs={'pk':question.id})) + kwargs={'pk': question.id})) # Then self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - + def test_delete_question_not_own(self): # Given question = Question.objects.get(summary='Created by other user') # When self.client.login(username=self.username, password=self.password) response = self.client.delete(reverse('api:question', - kwargs={'pk':question.id})) + kwargs={'pk': question.id})) # Then self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertTrue(Question.objects.filter(pk=question.id).exists()) - + def test_delete_question(self): # Given question = Question.objects.get(summary='delete question') # When self.client.login(username=self.username, password=self.password) response = self.client.delete(reverse('api:question', - kwargs={'pk':question.id})) + kwargs={'pk': question.id})) # Then self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Question.objects.filter(pk=question.id).exists()) @@ -229,14 +229,17 @@ class QuestionPaperListTestCase(TestCase): self.user = User.objects.create_user(username=self.username, password=self.password) self.otheruser = User.objects.create_user(username=self.otherusername, - password=self.password) - self.quiz1 = Quiz.objects.create(description='Quiz1', creator=self.user) - self.quiz2 = Quiz.objects.create(description='Quiz2', creator=self.user) + password=self.password) + self.quiz1 = Quiz.objects.create(description='Quiz1', + creator=self.user) + self.quiz2 = Quiz.objects.create(description='Quiz2', + creator=self.user) self.quiz3 = Quiz.objects.create(description='Quiz3', creator=self.otheruser) - - self.quiz4 = Quiz.objects.create(description='Quiz4', creator=self.user) - self.quiz5 = Quiz.objects.create(description='Quiz5', creator=self.user) + self.quiz4 = Quiz.objects.create(description='Quiz4', + creator=self.user) + self.quiz5 = Quiz.objects.create(description='Quiz5', + creator=self.user) self.quiz6 = Quiz.objects.create(description='Quiz6', creator=self.otheruser) self.questionpaper = QuestionPaper.objects.create(quiz=self.quiz1) @@ -251,7 +254,8 @@ class QuestionPaperListTestCase(TestCase): language='python', type='mcq') self.question4 = Question.objects.create(summary='Q4', user=self.user, language='python', type='mcq') - self.question5 = Question.objects.create(summary='Q5', user=self.otheruser, + self.question5 = Question.objects.create(summary='Q5', + user=self.otheruser, language='python', type='mcq') self.questionset = QuestionSet.objects.create(marks=1, num_questions=1) self.questionset.questions.add(self.question3) @@ -268,11 +272,12 @@ class QuestionPaperListTestCase(TestCase): response = self.client.get(reverse('api:questionpapers')) # Then self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - + def test_get_all_questionpaper(self): # Given questionpapers = QuestionPaper.objects.filter( - quiz__in=[self.quiz1, self.quiz2]) + quiz__in=[self.quiz1, self.quiz2] + ) serializer = QuestionPaperSerializer(questionpapers, many=True) # When self.client.login(username=self.username, password=self.password) @@ -295,8 +300,7 @@ class QuestionPaperListTestCase(TestCase): # Given data = {'quiz': self.quiz4.id, 'fixed_questions': [self.question1.id, self.question2.id], - 'random_questions': [self.questionset.id] - } + 'random_questions': [self.questionset.id]} # When self.client.login(username=self.username, password=self.password) response = self.client.post(reverse('api:questionpapers'), data) @@ -306,7 +310,7 @@ class QuestionPaperListTestCase(TestCase): def test_create_questionpaper_not_own_quiz(self): # Given - data = {'quiz': self.quiz5.id, 'fixed_questions':[self.question1.id], + data = {'quiz': self.quiz5.id, 'fixed_questions': [self.question1.id], 'random_questions': [self.questionset.id]} # When self.client.login(username=self.otherusername, password=self.password) @@ -317,21 +321,19 @@ class QuestionPaperListTestCase(TestCase): def test_create_questionpaper_not_own_questions(self): # Given data = {'quiz': self.quiz6.id, - 'fixed_questions':[self.question1.id, self.question5.id], - 'random_questions': [self.questionset.id] - } + 'fixed_questions': [self.question1.id, self.question5.id], + 'random_questions': [self.questionset.id]} # When self.client.login(username=self.otherusername, password=self.password) response = self.client.post(reverse('api:questionpapers'), data) # Then - self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)\ + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_create_questionpaper_not_own_questionsets(self): # Given data = {'quiz': self.quiz6.id, - 'fixed_questions':[self.question5.id], - 'random_questions': [self.questionset.id] - } + 'fixed_questions': [self.question5.id], + 'random_questions': [self.questionset.id]} # When self.client.login(username=self.otherusername, password=self.password) response = self.client.post(reverse('api:questionpapers'), data) @@ -342,8 +344,7 @@ class QuestionPaperListTestCase(TestCase): # Given data = {'quiz': self.quiz1.id, 'fixed_questions': [self.question1.id], - 'random_questions': [self.questionset.id] - } + 'random_questions': [self.questionset.id]} # When self.client.login(username=self.username, password=self.password) response = self.client.post(reverse('api:questionpapers'), data) @@ -375,14 +376,12 @@ class QuestionPaperListTestCase(TestCase): # Then self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - def test_edit_questionpaper(self): # Given questionpaper = self.questionpaper2 data = {'quiz': self.quiz5.id, 'fixed_questions': [self.question1.id, self.question2.id], - 'random_questions': [self.questionset.id] - } + 'random_questions': [self.questionset.id]} # When self.client.login(username=self.username, password=self.password) response = self.client.put(reverse('api:questionpaper', @@ -392,17 +391,17 @@ class QuestionPaperListTestCase(TestCase): questionpaper = QuestionPaper.objects.get(pk=questionpaper.id) self.assertEqual(questionpaper.quiz.id, self.quiz5.id) - def test_delete_questionpaper(self): # Given questionpaper = self.questionpaper2 # When self.client.login(username=self.username, password=self.password) response = self.client.delete(reverse('api:questionpaper', - kwargs={'pk':questionpaper.id})) + kwargs={'pk': questionpaper.id})) # Then self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) - self.assertFalse(QuestionPaper.objects.filter(quiz=self.quiz2).exists()) + questionpapers = QuestionPaper.objects.filter(quiz=self.quiz2) + self.assertFalse(questionpapers.exists()) def tearDown(self): self.client.logout() @@ -414,7 +413,7 @@ class QuestionPaperListTestCase(TestCase): class QuizListTestCase(TestCase): """ Test get all quizzes and create a new quiz """ - + def setUp(self): self.client = APIClient() self.username = 'demo' @@ -430,7 +429,6 @@ class QuizListTestCase(TestCase): # Then self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - def test_get_all_quizzes(self): # Given quizzes = Quiz.objects.filter(creator=self.user) @@ -566,7 +564,7 @@ class QuizDetailTestCase(TestCase): kwargs={'pk': quiz.id})) # Then self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - + def test_delete_quiz_not_own(self): # Given quiz = Quiz.objects.get(description='Quiz3') @@ -577,14 +575,14 @@ class QuizDetailTestCase(TestCase): # Then self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertTrue(Quiz.objects.filter(pk=quiz.id).exists()) - + def test_delete_quiz(self): # Given quiz = Quiz.objects.get(description='delete quiz') # When self.client.login(username=self.username, password=self.password) response = self.client.delete(reverse('api:quiz', - kwargs={'pk':quiz.id})) + kwargs={'pk': quiz.id})) # Then self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Quiz.objects.filter(pk=quiz.id).exists()) @@ -609,14 +607,16 @@ class AnswerPaperListTestCase(TestCase): password=self.password) self.student = User.objects.create_user(username=self.studentusername, password='demo') - self.quiz1 = Quiz.objects.create(description='Quiz1', creator=self.user) + self.quiz1 = Quiz.objects.create(description='Quiz1', + creator=self.user) self.quiz2 = Quiz.objects.create(description='Quiz2', creator=self.otheruser) self.questionpaper1 = QuestionPaper.objects.create(quiz=self.quiz1) self.questionpaper2 = QuestionPaper.objects.create(quiz=self.quiz2) self.question1 = Question.objects.create(summary='Q1', user=self.user, language='python', type='mcq') - self.question2 = Question.objects.create(summary='Q5', user=self.otheruser, + self.question2 = Question.objects.create(summary='Q5', + user=self.otheruser, language='python', type='mcq') self.questionpaper1.fixed_questions.add(self.question1) self.questionpaper2.fixed_questions.add(self.question2) @@ -624,23 +624,25 @@ class AnswerPaperListTestCase(TestCase): self.questionpaper2.save() self.questionpaper1.update_total_marks() self.questionpaper2.update_total_marks() - self.answerpaper1 = AnswerPaper.objects.create(user=self.user, - question_paper=self.questionpaper1, attempt_number=1, - start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), - end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc)) - self.answerpaper2 = AnswerPaper.objects.create(user=self.otheruser, - question_paper=self.questionpaper2, attempt_number=1, - start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), - end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc)) - + self.answerpaper1 = AnswerPaper.objects.create( + user=self.user, + question_paper=self.questionpaper1, attempt_number=1, + start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), + end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc) + ) + self.answerpaper2 = AnswerPaper.objects.create( + user=self.otheruser, + question_paper=self.questionpaper2, attempt_number=1, + start_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), + end_time=datetime(2015, 10, 9, 10, 28, 15, 0, tzinfo=pytz.utc) + ) self.course = Course.objects.create(name="Python Course", enrollment="Enroll Request", creator=self.user) - # Learing module learning_module = LearningModule.objects.create( name='LM1', description='module one', creator=self.user - ) + ) learning_unit_quiz = LearningUnit.objects.create(quiz=self.quiz1, type='quiz', order=1) learning_module.learning_unit.add(learning_unit_quiz) @@ -649,7 +651,6 @@ class AnswerPaperListTestCase(TestCase): self.course.students.add(self.student) self.course.save() - def test_get_all_answerpapers(self): # Given answerpapers = [self.answerpaper1] @@ -665,41 +666,43 @@ class AnswerPaperListTestCase(TestCase): def test_create_answerpaper_valid_data(self): # Given data = {'question_paper': self.questionpaper1.id, - 'attempt_number': 1, 'course': self.course.id - } + 'attempt_number': 1, 'course': self.course.id} # When - self.client.login(username=self.studentusername, password=self.password) + self.client.login(username=self.studentusername, + password=self.password) response = self.client.post(reverse('api:answerpapers'), data) # Then self.assertEqual(response.status_code, status.HTTP_201_CREATED) - answerpapers = AnswerPaper.objects.filter(question_paper=self.questionpaper1, - user=self.student, attempt_number=1, - course=self.course) + answerpapers = AnswerPaper.objects.filter( + user=self.student, attempt_number=1, + question_paper=self.questionpaper1, course=self.course + ) self.assertTrue(answerpapers.exists()) self.assertEqual(answerpapers.count(), 1) - + def test_create_answerpaper_invalid_data(self): # Given data = {'question_paper': self.questionpaper1.id} # When - self.client.login(username=self.studentusername, password=self.password) + self.client.login(username=self.studentusername, + password=self.password) response = self.client.post(reverse('api:answerpapers'), data) # Then self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - + def test_create_answerpaper_not_enrolled(self): # Given data = {'question_paper': self.questionpaper1.id, - 'attempt_number': 1, 'course': self.course.id - } + 'attempt_number': 1, 'course': self.course.id} # When self.client.login(username=self.otherusername, password=self.password) response = self.client.post(reverse('api:answerpapers'), data) # Then self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - answerpapers = AnswerPaper.objects.filter(question_paper=self.questionpaper1, - user=self.otheruser, attempt_number=1, - course=self.course) + answerpapers = AnswerPaper.objects.filter( + question_paper=self.questionpaper1, user=self.otheruser, + attempt_number=1, course=self.course + ) self.assertFalse(answerpapers.exists()) self.assertEqual(answerpapers.count(), 0) @@ -713,7 +716,7 @@ class AnswerPaperListTestCase(TestCase): class AnswerValidatorTestCase(TestCase): - + @classmethod def setUpClass(self): self.client = APIClient() @@ -725,15 +728,20 @@ class AnswerValidatorTestCase(TestCase): self.quiz = Quiz.objects.create(description='Quiz', creator=self.user) self.questionpaper = QuestionPaper.objects.create(quiz=self.quiz) self.question1 = Question.objects.create(summary='Q1', user=self.user, - points=1.0, language='python', type='code') + points=1.0, language='python', + type='code') self.question2 = Question.objects.create(summary='Q2', user=self.user, - points=1.0, language='python', type='mcq') + points=1.0, language='python', + type='mcq') self.question3 = Question.objects.create(summary='Q3', user=self.user, - points=1.0, language='python', type='mcc') + points=1.0, language='python', + type='mcc') self.question4 = Question.objects.create(summary='Q4', user=self.user, - points=1.0, language='python', type='mcq') + points=1.0, language='python', + type='mcq') self.question5 = Question.objects.create(summary='Q5', user=self.user, - points=1.0, language='python', type='mcq') + points=1.0, language='python', + type='mcq') self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', @@ -776,7 +784,7 @@ class AnswerValidatorTestCase(TestCase): # Learing module learning_module = LearningModule.objects.create( name='LM1', description='module one', creator=self.user - ) + ) learning_unit_quiz = LearningUnit.objects.create(quiz=self.quiz, type='quiz', order=1) learning_module.learning_unit.add(learning_unit_quiz) @@ -811,50 +819,57 @@ class AnswerValidatorTestCase(TestCase): def test_correct_mcq(self): # Given - data = {'answer': str( self.mcq_based_testcase1.id)} + data = {'answer': str(self.mcq_based_testcase1.id)} answerpaper_id = self.answerpaper.id question_id = self.question2.id # When self.client.login(username=self.username, password=self.password) - response = self.client.post(reverse('api:validators', - kwargs={'answerpaper_id': answerpaper_id, 'question_id': question_id}), data) + response = self.client.post( + reverse('api:validators', kwargs={'answerpaper_id': answerpaper_id, + 'question_id': question_id}), data + ) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) - self.assertTrue(response.data['success']) - answerpaper = AnswerPaper.objects.get(user=self.user, course=self.course, - attempt_number=1, - question_paper=self.questionpaper) + self.assertTrue(response.data.get('success')) + answerpaper = AnswerPaper.objects.get( + user=self.user, course=self.course, attempt_number=1, + question_paper=self.questionpaper + ) self.assertTrue(answerpaper.marks_obtained > 0) - def test_wrong_mcq(self): # Given - data = {'answer': str( self.mcq_based_testcase2.id)} + data = {'answer': str(self.mcq_based_testcase2.id)} answerpaper_id = self.answerpaper.id question_id = self.question2.id # When self.client.login(username=self.username, password=self.password) - response = self.client.post(reverse('api:validators', - kwargs={'answerpaper_id': answerpaper_id, 'question_id': question_id}), data) + response = self.client.post( + reverse('api:validators', kwargs={'answerpaper_id': answerpaper_id, + 'question_id': question_id}), data + ) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) - self.assertFalse(response.data['success']) - + self.assertFalse(response.data.get('success')) + def test_correct_mcc(self): # Given - data = {'answer': str( self.mcc_based_testcase.id)} + data = {'answer': str(self.mcc_based_testcase.id)} answerpaper_id = self.answerpaper.id question_id = self.question3.id # When self.client.login(username=self.username, password=self.password) - response = self.client.post(reverse('api:validators', - kwargs={'answerpaper_id': answerpaper_id, 'question_id': question_id}), data) + response = self.client.post( + reverse('api:validators', kwargs={'answerpaper_id': answerpaper_id, + 'question_id': question_id}), data + ) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) - self.assertTrue(response.data['success']) - answerpaper = AnswerPaper.objects.get(user=self.user, course=self.course, - attempt_number=1, - question_paper=self.questionpaper) + self.assertTrue(response.data.get('success')) + answerpaper = AnswerPaper.objects.get( + user=self.user, course=self.course, attempt_number=1, + question_paper=self.questionpaper + ) self.assertTrue(answerpaper.marks_obtained > 0) def test_correct_code(self): @@ -868,21 +883,24 @@ class AnswerValidatorTestCase(TestCase): question_id = self.question1.id # When self.client.login(username=self.username, password=self.password) - response = self.client.post(reverse('api:validators', - kwargs={'answerpaper_id': answerpaper_id, 'question_id': question_id}), data) + response = self.client.post( + reverse('api:validators', kwargs={'answerpaper_id': answerpaper_id, + 'question_id': question_id}), data + ) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data['status'], 'running') + self.assertEqual(response.data.get('status'), 'running') uid = response.data['uid'] time.sleep(2) - response = self.client.get(reverse('api:validator', kwargs={'uid': uid})) + response = self.client.get(reverse('api:validator', + kwargs={'uid': uid})) self.assertTrue(response.status_code, status.HTTP_200_OK) - answerpaper = AnswerPaper.objects.get(user=self.user, course=self.course, - attempt_number=1, - question_paper=self.questionpaper) - if response.data['status'] == 'done': - result = json.loads(response.data['result']) - self.assertTrue(result['success']) + answerpaper = AnswerPaper.objects.get( + user=self.user, course=self.course, attempt_number=1, + question_paper=self.questionpaper + ) + if response.data.get('status') == 'done': + result = json.loads(response.data.get('result')) + self.assertTrue(result.get('success')) else: - self.assertEqual(response.data['status'], 'running') - + self.assertEqual(response.data.get('status'), 'running') |