Example #1
0
    def test_all_but_photo(self, member):
        for q in [
                a for a in Question.SUPPORTED_ATTRIBUTES if a != Question.PHOTO
        ]:
            for h in Question.SUPPORTED_ATTRIBUTES:
                if h != q:
                    for m in [True, False]:
                        text = question_text(member, q, h, m)
                        assert isinstance(text, str)
                        assert text != ''
                        assert '{' not in text
                        assert '}' not in text

        for q in [
                k for k in Question.SUPPORTED_ATTRIBUTES if k != Question.PHOTO
        ]:
            for h in [
                    k for k in Question.SUPPORTED_ATTRIBUTES
                    if k != Question.PHOTO
            ]:
                if h not in q and q not in h:
                    for m in [True, False]:
                        text = question_text(member, q, h, m)
                        assert isinstance(text, str)
                        assert text != ''
                        assert '{' not in text
                        assert '}' not in text
Example #2
0
 def test_hint_value(self, member):
     text = question_text(
         member,
         Question.FULL_NAME,
         Question.INSTRUMENT,
         multiple_choice=False,
         hint_value='foo',
     )
     assert 'foo' in text
     assert member.instruments_str not in text
Example #3
0
    def test_photo(self, member):
        q = Question.PHOTO
        for h in [a for a in Question.SUPPORTED_ATTRIBUTES if a != q]:
            with pytest.raises(ValueError, match='Photos are'):
                question_text(member, q, h, False)

            text = question_text(member, q, h, True)
            assert text != ''
            assert '{' not in text
            assert '}' not in text

        q = 'photo_file_id'
        for h in [
                k for k in Question.SUPPORTED_ATTRIBUTES if k != Question.PHOTO
        ]:
            with pytest.raises(ValueError, match='Photos are'):
                question_text(member, q, h, False)

            text = question_text(member, q, h, True)
            assert text != ''
            assert '{' not in text
            assert '}' not in text
Example #4
0
 def test_errors(self):
     with pytest.raises(ValueError, match='Unsupported'):
         question_text(member, 'abc', Question.FULL_NAME)
     with pytest.raises(ValueError, match='Unsupported'):
         question_text(member, Question.FULL_NAME, 'abc')
Example #5
0
    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