def director_question(movie, movie2): director = random.choice([movie['director'], movie2['director']]) question = Question( question= f'Did {director} direct the movie \"{movie["title"]} ({movie["release"]})\" ?', answer='yes' if movie['director'] == director else 'no', choices='yes,no') db.session.add(question)
def home(): context = dict( key_name='answer', form={}, questions=Question.fetch_quiz() ) if request.method == 'POST': answered_questions, score = Question.check_answers(key_name=context['key_name'], answers=request.form, current_user=current_user) context.update(dict( form=request.form, questions=answered_questions, show_answer=True, score=score )) return render_template('pages/movies.html', **context)
def rating_question(movie): rating = int(float(movie['rating']) * 10) choices_ = random_sampler(rating) question = Question( question= f'Guess the rating of movie \"{movie["title"]} ({movie["release"]})\".', answer=str(rating), choices=choices_) db.session.add(question)
def actor_question(movie, movie2): wrong = movie2['actors'][0] actors = movie['actors'][:3] + [wrong] random.shuffle(actors) question = Question( question=f'Which of the following actors did not' f' star in the movie {movie["title"]}({movie["release"]}) ?', answer=wrong, choices=",".join(actors)) db.session.add(question)
def release_date_question(movie): try: release_date = int(movie['release']) except ValueError: return choices_ = random_sampler(release_date) question = Question(question=f'When was {movie["title"]} released?', answer=release_date, choices=choices_) db.session.add(question)
def test_best_score_is_preserved(self): # this makes total score of 10 self.test_check_answers() key = 'answer' answers = { key + str(question.id): 'wrong-answer' for question in self.questions[:10] } questions, score = Question.check_answers(key, answers, self.user) self.assertEqual(score, 0) # however the old score (higher) is preserved self.assertEqual(self.user.score.score, 10)
def test_check_answers(self): # user has no score yet self.assertIsNone(self.user.score) key = 'answer' answers = { key + str(question.id): question.answer for question in self.questions[:10] } questions, score = Question.check_answers(key, answers, self.user) self.assertEqual(score, 10) # check score table has been populated self.assertEqual(self.user.score.score, 10)
def test_fetch_quiz_should_be_random(self): sample1 = Question.fetch_quiz(3) sample2 = Question.fetch_quiz(3) self.assertNotEqual(sample1, sample2)