def submit_quiz(quiz_submission: QuizSubmission, answers: List[dict]): """ execute a quiz answers submission :param quiz_submission: a QuizSubmission object :param answers: calculated answers for all submission questions. """ quiz_submission.answer_submission_questions(quiz_questions=answers) logger.info( f"successfully sent answers for quiz {str(quiz_submission)}")
class TestQuizSubmission(unittest.TestCase): def setUp(self): self.canvas = Canvas(settings.BASE_URL, settings.API_KEY) self.submission = QuizSubmission( self.canvas._Canvas__requester, { "id": 1, "quiz_id": 1, "user_id": 1, "course_id": 1, "submission_id": 1, "attempt": 3, "validation_token": "this is a validation token", "manually_unlocked": None, "score": 7, }, ) # __str__() def test__str__(self, m): string = str(self.submission) self.assertIsInstance(string, str) # complete def test_complete(self, m): register_uris({"submission": ["complete"]}, m) submission = self.submission.complete() self.assertIsInstance(submission, QuizSubmission) self.assertTrue(hasattr(submission, "id")) self.assertTrue(hasattr(submission, "quiz_id")) self.assertTrue(hasattr(submission, "attempt")) self.assertTrue(hasattr(submission, "validation_token")) def test_complete_no_validation_token(self, m): del self.submission.validation_token with self.assertRaises(RequiredFieldMissing): self.submission.complete() # get_times def test_get_times(self, m): register_uris({"submission": ["get_times"]}, m) submission = self.submission.get_times() self.assertIsInstance(submission, dict) self.assertIn("end_at", submission) self.assertIn("time_left", submission) self.assertIsInstance(submission["time_left"], int) self.assertIsInstance(submission["end_at"], text_type) # update_score_and_comments def test_update_score_and_comments(self, m): register_uris({"submission": ["update_score_and_comments"]}, m) submission = self.submission.update_score_and_comments( quiz_submissions=[{ "attempt": 1, "fudge_points": 1, "questions": { "question id 1": { "score": 1, "comment": "question 1 comment" }, "question id 2": { "score": 2, "comment": "question 2 comment" }, "question id 3": { "score": 3, "comment": "question 3 comment" }, }, }]) self.assertIsInstance(submission, QuizSubmission) self.assertTrue(hasattr(submission, "id")) self.assertTrue(hasattr(submission, "attempt")) self.assertTrue(hasattr(submission, "quiz_id")) self.assertTrue(hasattr(submission, "validation_token")) self.assertEqual(submission.score, 7) # get_submission_events def test_get_submission_events(self, m): register_uris({"quiz": ["get_submission_events"]}, m) events = self.submission.get_submission_events() self.assertIsInstance(events, list) self.assertIsInstance(events[0], QuizSubmissionEvent) self.assertIsInstance(events[1], QuizSubmissionEvent) self.assertEqual(str(events[0]), "page_blurred") self.assertEqual(str(events[1]), "page_focused") # get_submission_questions def test_get_submission_questions(self, m): register_uris({"submission": ["get_submission_questions"]}, m) questions = self.submission.get_submission_questions() self.assertIsInstance(questions, list) self.assertIsInstance(questions[0], QuizSubmissionQuestion) self.assertTrue(hasattr(questions[0], "id")) self.assertTrue(hasattr(questions[0], "flagged")) # answer_submission_questions def test_answer_submission_questions(self, m): register_uris({"submission": ["answer_submission_questions"]}, m) answered_questions = self.submission.answer_submission_questions() self.assertIsInstance(answered_questions, list) self.assertIsInstance(answered_questions[0], QuizSubmissionQuestion) self.assertTrue(hasattr(answered_questions[0], "id")) self.assertTrue(hasattr(answered_questions[0], "flagged")) def test_answer_submission_questions_manual_validation_token(self, m): register_uris({"submission": ["answer_submission_questions"]}, m) del self.submission.validation_token answered_questions = self.submission.answer_submission_questions( validation_token="new validation token") self.assertIsInstance(answered_questions, list) self.assertIsInstance(answered_questions[0], QuizSubmissionQuestion) self.assertTrue(hasattr(answered_questions[0], "id")) self.assertTrue(hasattr(answered_questions[0], "flagged")) def test_answer_submission_questions_no_validation_token(self, m): del self.submission.validation_token with self.assertRaises(RequiredFieldMissing): self.submission.answer_submission_questions() # submit_events() def test_submit_events(self, m): register_uris({"quiz": ["get_submission_events", "submit_events"]}, m) test_events = self.submission.get_submission_events() result = self.submission.submit_events(test_events) self.assertTrue(result) def test_submit_events_fail(self, m): with self.assertRaises(RequiredFieldMissing): self.submission.submit_events([{}])