def test_check_update_free_text(self, member): valid_update = Update(1, message=Message(1, None, None, None, text='text')) invalid_update = Update(1, poll_answer=PollAnswer(123, None, [1])) for attr in [ a for a in Question.SUPPORTED_ATTRIBUTES if a != Question.PHOTO ]: q = Question(member, attr, multiple_choice=False) assert q.check_update(valid_update) assert not q.check_update(invalid_update) q = Question(member, Question.ADDRESS, multiple_choice=False) valid_update = Update(1, message=Message(1, None, None, None, location=Location(longitude=1, latitude=1))) invalid_update = Update(1, poll_answer=PollAnswer(123, None, [1])) assert q.check_update(valid_update) assert not q.check_update(invalid_update)
def test_init(self, member): for attr in Question.SUPPORTED_ATTRIBUTES: if attr != Question.PHOTO: q = Question(member, attr, multiple_choice=False) assert q.member == member assert q.attribute == attr assert q.multiple_choice is False q = Question(member, attr, poll=self.poll) assert q.member == member assert q.attribute == attr assert q.multiple_choice is True assert q.poll == self.poll
def test_check_update_multiple_choice(self, member): valid_update = Update(1, poll_answer=PollAnswer(123, None, [1])) invalid_update = Update(1, message=True) for attr in Question.SUPPORTED_ATTRIBUTES: q = Question(member, attr, poll=self.poll) assert q.check_update(valid_update) assert not q.check_update(invalid_update)
def test_check_answer_multiple_choice(self, member): correct_update = Update(1, poll_answer=PollAnswer(123, None, [1])) false_update = Update(1, poll_answer=PollAnswer(123, None, [0])) for attr in Question.SUPPORTED_ATTRIBUTES: q = Question(member, attr, poll=self.poll) assert q.check_answer(correct_update) assert not q.check_answer(false_update) assert q.correct_answer == 'Opt2'
def test_check_answer_free_text_location_coords(self, answer, result, member): q = Question(member, Question.ADDRESS, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, location=Location(*reversed(answer)))) assert q.check_answer(update) is result assert q.correct_answer == member.address
def test_init_errors(self): member = Member(1) invalid_poll = Poll(123, 'question', ['Opt1', 'Opt2'], 0, False, False, Poll.REGULAR, True) with pytest.raises(ValueError, match='must be a quiz poll'): Question(member, Question.AGE, poll=invalid_poll) with pytest.raises(ValueError, match='poll if and only if'): Question(member, Question.AGE, multiple_choice=False, poll=self.poll) with pytest.raises(ValueError, match='poll if and only if'): Question(member, Question.AGE) with pytest.raises(ValueError, match='Attribute not supported'): Question(member, 'attribute') with pytest.raises(ValueError, match='Photos are'): Question(member, Question.PHOTO, multiple_choice=False) for attr in Question.SUPPORTED_ATTRIBUTES: if attr != Question.PHOTO: with pytest.raises(ValueError, match='the required attribute'): Question(member, attr, multiple_choice=False)
def test_check_answer_free_text_functions(self, answer, result, member): q = Question(member, Question.FUNCTIONS, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == member.functions_str
def test_check_answer_free_text_instrument(self, answer, result, member): q = Question(member, Question.INSTRUMENT, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == member.instruments_str
def test_check_answer_free_text_joined(self, answer, result, member): q = Question(member, Question.JOINED, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == str(member.joined)
def test_check_answer_free_text_birthday(self, answer, result, member): q = Question(member, Question.BIRTHDAY, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == str(member.birthday)
def test_check_answer_free_text_full_name(self, answer, result, member): member.nickname = 'nick' q = Question(member, Question.FULL_NAME, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == member.full_name
def test_check_answer_free_text_last_name(self, answer, result, member): q = Question(member, Question.LAST_NAME, multiple_choice=False) update = Update(1, message=Message(1, None, None, None, text=answer)) assert q.check_answer(update) is result assert q.correct_answer == member.last_name
def ask_question(self) -> None: """ Asks the next question, if there is another. Raises: RuntimeError: If there are no more questions to be asked. """ if self.number_of_questions_asked == self.number_of_questions: raise RuntimeError('No more questions to ask!') hint_manager, question_manager = random.choice(self.questionable()) hint_attribute = hint_manager.description question_attribute = question_manager.description if question_manager.description == Question.PHOTO: multiple_choice = True photo_question = True else: multiple_choice = self.multiple_choice photo_question = False if multiple_choice: member, hint, opts, index = hint_manager.build_question_with( question_manager, multiple_choice=True, exclude_members=[self.member] ) question = question_text( member, question_attribute, hint_attribute, multiple_choice=True ) options = list(str(o) for o in opts) # Truncate long options for idx, opt in enumerate(options): if len(opt) > 100: options[idx] = opt[:96] + ' ...' # Send photos if needed if photo_question: self.bot.send_media_group( chat_id=self.member.user_id, media=[InputMediaPhoto(options[0]), InputMediaPhoto(options[1])], ) self.bot.send_media_group( chat_id=self.member.user_id, media=[InputMediaPhoto(options[2]), InputMediaPhoto(options[3])], ) if hint_attribute == Question.PHOTO: self.bot.send_photo(chat_id=self.member.user_id, photo=hint) # Send the question poll = self.bot.send_poll( chat_id=self.member.user_id, question=question, options=(PHOTO_OPTIONS if photo_question else options), is_anonymous=False, type=Poll.QUIZ, correct_option_id=index, ).poll self.current_question = Question( member, question_attribute, poll=poll, multiple_choice=multiple_choice ) else: member, hint, _ = hint_manager.build_question_with( question_manager, multiple_choice=False, exclude_members=[self.member] ) question = question_text( member, question_attribute, hint_attribute, multiple_choice=False, hint_value=hint ) if hint_attribute == Question.PHOTO: self.bot.send_photo( chat_id=self.member.user_id, photo=member.photo_file_id, caption=question ) else: self.bot.send_message(chat_id=self.member.user_id, text=question) self.current_question = Question( member, question_attribute, multiple_choice=multiple_choice ) self.number_of_questions_asked += 1