def setUp(self): super(QuizAttemptTests, self).setUp() self.categories = ["adm", "apo", "cou", "dis", "eva", "fai", "giv", "hea", "int", "kno"] # To give some consistency, don't randomize the answers (after this point, anyway). answers = [5, 2, 0, 2, 0, 5, 2, 0, 0, 0, 2, 5, 2, 2, 2, 2, 2, 5, 0, 2] self.attempt = QuizAttempt.create(1) questions = [] for i in range(0, 20): questions.append(QuizAttemptAnswer( question_number=i + 1, answer=answers[i], category=self.categories[i % 10] )) self.attempt.questions = questions self.attempt.put() self.attempt2 = QuizAttempt.create(87752083) questions = [] for i in range(0, 20): questions.append(QuizAttemptAnswer( question_number=i + 1, answer=answers[i], category=self.categories[i % 10] )) self.attempt2.questions = questions self.attempt2.put()
def get_superadmin_index_data(self): paid_survey_users, all_quiz_attempts, paid_quiz_attempts = yield ( User.get_paid_survey_users().fetch_async(), QuizAttempt.get_all_attempts().fetch_async(), QuizAttempt.get_non_trial_attempts().fetch_async() ) all_users = yield ndb.get_multi_async([User.build_key(attempt.user_id) for attempt in all_quiz_attempts]) raise ndb.Return((paid_survey_users, all_quiz_attempts, all_users, paid_quiz_attempts))
def get_results_data(self, user_id): """ Get the results needed for the survey page """ users, quiz_attempts = yield( User.get_by_user_id_async(user_id), QuizAttempt.get_all_attempts_for_user_id_async(user_id) ) raise ndb.Return((users, quiz_attempts))
def get_superadmin_index_data(self): paid_survey_users, all_quiz_attempts, paid_quiz_attempts = yield ( User.get_paid_survey_users().fetch_async(), QuizAttempt.get_all_attempts().fetch_async(), QuizAttempt.get_non_trial_attempts().fetch_async() ) all_users = [] quiz_attempt_dicts = [] for attempt in all_quiz_attempts: user = User.build_key(attempt.user_id).get() quiz_attempt_dicts.append({ 'attempt': attempt, 'user': user }) all_users.append(user) raise ndb.Return((paid_survey_users, quiz_attempt_dicts, all_users, paid_quiz_attempts))
def first_page(self): """ Gets the first page a user id needs to start on. If they haven't answered any questions or up to 20 questions, that's page 1. If they've answered 21-40 questions, that's page 2, and so on. """ quiz_attempt = QuizAttempt.get_by_user_id(self.user_id, self.quiz_type) return (len(quiz_attempt.questions) / QUESTIONS_PER_PAGE) + 1
def get(self, **context): """ GET """ user = User.get_or_create_by_user_id(self.request.GET.get('userId')) attempt = QuizAttempt.get_by_user_id(user.user_id, context['quiz_type']) if not attempt: # Start up a new QuizAttempt! attempt = QuizAttempt.create(user_id=user.user_id, quiz_type=context['quiz_type']) if settings.is_devappserver(): user.paid = True context.update({ 'user': user, 'user_id': user.user_id, 'quiz_attempt': attempt }) logging.info(context) self.render_response('survey.html', **context)
def save_user_submitted_answers(self, submitted): """ Save answers submitted to the API. :param submitted: """ quiz_attempt = QuizAttempt.get_by_user_id(self.user_id, self.quiz_type) if type(submitted) == list and len(submitted) > 0: questions_start = submitted[0].question_number - 1 questions_end = submitted[-1].question_number else: return page_num = (questions_start / QUESTIONS_PER_PAGE) + 1 submitted = self._normalize_question_numbers(submitted, page_num, out=False) for question in quiz_attempt.questions[questions_start:questions_end]: if question.question_number == submitted[0].question_number: quiz_attempt.questions[question.question_number - 1].answer = submitted.pop(0).answer quiz_attempt.questions.extend(submitted) quiz_attempt.put()
def test_build_key_method(self): newk = QuizAttempt.build_key(1) from google.appengine.ext import ndb self.assertTrue(isinstance(newk, ndb.Key))
def test_get_by_user_id_raises_ValueError_if_quiz_type_not_in_allowed_list(self): with self.assertRaises(ValueError): QuizAttempt.get_by_user_id(1, 'not_real')
def test_get_by_user_id_returns_attempt_when_long_is_passed_in(self): self.assertIsNotNone(QuizAttempt.get_by_user_id(87752083))
def test_get_by_user_id_coerces_to_int_if_string_passed_in(self): with self.assertRaises(TypeError): QuizAttempt.get_by_user_id([1]) # Raises a TypeError when it goes to convert a list to an int.
def test_get_by_user_id_returns_None_when_no_attempts_exist(self): self.assertIsNone(QuizAttempt.get_by_user_id(100))
def test_get_by_user_id_returns_short_a_quiz_type_if_param_is_passed(self): QuizAttempt.create(1, quiz_type='short_a') self.assertEqual('short_a', QuizAttempt.get_by_user_id(1, quiz_type='short_a').quiz_type)
def test_get_by_user_id_returns_fullform_quiz_type_by_default(self): self.assertEqual('fullform', QuizAttempt.get_by_user_id(1).quiz_type)
def test_get_by_user_id_returns_attempt_for_user_that_has_an_attempt(self): self.assertIsNotNone(QuizAttempt.get_by_user_id(1))
def test_create_quiz_takes_user_id_and_quiz_type(self): QuizAttempt.create(1, 'short_a') my_key = QuizAttempt.build_key(1, 'short_a') self.assertEqual('short_a', my_key.get().quiz_type)
def test_get_all_attempts_for_user_id_returns_three_results_if_user_has_three_attempts(self): QuizAttempt.create(user_id=1, quiz_type='short_a') QuizAttempt.create(user_id=1, quiz_type='short_b') attempts = QuizAttempt.get_all_attempts_for_user_id(1) self.assertEqual(3, len(attempts))
def test_get_all_attempts_for_user_id_returns_empty_list_if_user_has_no_attempts(self): self.assertEqual([], QuizAttempt.get_all_attempts_for_user_id(1234))
def test_get_all_attempts_for_user_id_requires_user_id(self): with self.assertRaises(ValueError): QuizAttempt.get_all_attempts_for_user_id(None)
def test_get_by_user_id_raises_value_error_if_None_passed_in(self): with self.assertRaises(ValueError): QuizAttempt.get_by_user_id(None)
def test_first_page_property_returns_first_20_questions_for_new_user_id(self): QuizAttempt.create(1) self.assertEqual(1, self.survey.first_page)