def _get_poll(self, poll_type): import uuid from random import randint if poll_type == POLL_QUIZ: return Poll( id=str(uuid.uuid4()), question="Are you human?", options=[PollOption("Yes", 2), PollOption("No", 5)], total_voter_count=7, is_closed=False, is_anonymous=True, type=POLL_QUIZ, correct_option_id=0, allows_multiple_answers=False, ) else: return Poll( id=str(uuid.uuid4()), question="Are you human?", options=[PollOption("Yes", 2), PollOption("No", 5)], total_voter_count=7, is_closed=False, is_anonymous=True, type=POLL_REGULAR, allows_multiple_answers=False, )
def __init__(self, browser: Browser) -> None: self._browser = browser self.click_quiz = ClickQuiz(self._browser) self.drag_n_drop_quiz = DragDropQuiz(self._browser) self.lightning_quiz = LightningQuiz(self._browser) self.poll_quest = Poll(self._browser) self.link_explore = LinkExplore(self._browser)
def test_equality(self): a = Poll(123, 'question', ['O1', 'O2'], 1, False, True, Poll.REGULAR, True) b = Poll(123, 'question', ['o1', 'o2'], 1, True, False, Poll.REGULAR, True) c = Poll(456, 'question', ['o1', 'o2'], 1, True, False, Poll.REGULAR, True) d = PollOption('Text', 1) assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)
def test_de_json(self, bot): json_dict = { "id": self.id_, "question": self.question, "options": [o.to_dict() for o in self.options], "total_voter_count": self.total_voter_count, "is_closed": self.is_closed, "is_anonymous": self.is_anonymous, "type": self.type, "allows_multiple_answers": self.allows_multiple_answers, "explanation": self.explanation, "explanation_entities": [self.explanation_entities[0].to_dict()], "open_period": self.open_period, "close_date": to_timestamp(self.close_date), } poll = Poll.de_json(json_dict, bot) assert poll.id == self.id_ assert poll.question == self.question assert poll.options == self.options assert poll.options[0].text == self.options[0].text assert poll.options[0].voter_count == self.options[0].voter_count assert poll.options[1].text == self.options[1].text assert poll.options[1].voter_count == self.options[1].voter_count assert poll.total_voter_count == self.total_voter_count assert poll.is_closed == self.is_closed assert poll.is_anonymous == self.is_anonymous assert poll.type == self.type assert poll.allows_multiple_answers == self.allows_multiple_answers assert poll.explanation == self.explanation assert poll.explanation_entities == self.explanation_entities assert poll.open_period == self.open_period assert abs(poll.close_date - self.close_date) < timedelta(seconds=1) assert to_timestamp(poll.close_date) == to_timestamp(self.close_date)
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)
class OfferQuests: def __init__(self, browser: Browser) -> None: self._browser = browser self.click_quiz = ClickQuiz(self._browser) self.drag_n_drop_quiz = DragDropQuiz(self._browser) self.lightning_quiz = LightningQuiz(self._browser) self.poll_quest = Poll(self._browser) self.link_explore = LinkExplore(self._browser) def do_quest(self): if self._browser.click_element( By.ID, 'rqStartQuiz', ignore_no_ele_exc=True) and self._has_the_quiz_started(): return self._do_quiz() if self.poll_quest.do(): return True return self.link_explore.do() def _has_the_quiz_started(self): return len(self._browser.find_elements(By.CLASS_NAME, 'rqECredits')) > 0 def _do_quiz(self): if self.click_quiz.do(): return True if self.drag_n_drop_quiz.do(): return True return self.lightning_quiz.do()
def test_de_json(self, bot): json_dict = { 'id': self.id_, 'question': self.question, 'options': [o.to_dict() for o in self.options], 'total_voter_count': self.total_voter_count, 'is_closed': self.is_closed, 'is_anonymous': self.is_anonymous, 'type': self.type, 'allows_multiple_answers': self.allows_multiple_answers, 'explanation': self.explanation, 'explanation_entities': [self.explanation_entities[0].to_dict()], 'open_period': self.open_period, 'close_date': to_timestamp(self.close_date), } poll = Poll.de_json(json_dict, bot) assert poll.id == self.id_ assert poll.question == self.question assert poll.options == self.options assert poll.options[0].text == self.options[0].text assert poll.options[0].voter_count == self.options[0].voter_count assert poll.options[1].text == self.options[1].text assert poll.options[1].voter_count == self.options[1].voter_count assert poll.total_voter_count == self.total_voter_count assert poll.is_closed == self.is_closed assert poll.is_anonymous == self.is_anonymous assert poll.type == self.type assert poll.allows_multiple_answers == self.allows_multiple_answers assert poll.explanation == self.explanation assert poll.explanation_entities == self.explanation_entities assert poll.open_period == self.open_period assert pytest.approx(poll.close_date == self.close_date) assert to_timestamp(poll.close_date) == to_timestamp(self.close_date)
def test_de_json(self): json_dict = { 'id': self.id_, 'question': self.question, 'options': [o.to_dict() for o in self.options], 'total_voter_count': self.total_voter_count, 'is_closed': self.is_closed, 'is_anonymous': self.is_anonymous, 'type': self.type, 'allows_multiple_answers': self.allows_multiple_answers } poll = Poll.de_json(json_dict, None) assert poll.id == self.id_ assert poll.question == self.question assert poll.options == self.options assert poll.options[0].text == self.options[0].text assert poll.options[0].voter_count == self.options[0].voter_count assert poll.options[1].text == self.options[1].text assert poll.options[1].voter_count == self.options[1].voter_count assert poll.total_voter_count == self.total_voter_count assert poll.is_closed == self.is_closed assert poll.is_anonymous == self.is_anonymous assert poll.type == self.type assert poll.allows_multiple_answers == self.allows_multiple_answers
def de_json(cls, data, bot): if not data: return None data = super(Update, cls).de_json(data, bot) message = data.get('message') if message: message['default_quote'] = data.get('default_quote') data['message'] = Message.de_json(message, bot) edited_message = data.get('edited_message') if edited_message: edited_message['default_quote'] = data.get('default_quote') data['edited_message'] = Message.de_json(edited_message, bot) data['inline_query'] = InlineQuery.de_json(data.get('inline_query'), bot) data['chosen_inline_result'] = ChosenInlineResult.de_json( data.get('chosen_inline_result'), bot) callback_query = data.get('callback_query') if callback_query: callback_query['default_quote'] = data.get('default_quote') data['callback_query'] = CallbackQuery.de_json(callback_query, bot) data['shipping_query'] = ShippingQuery.de_json(data.get('shipping_query'), bot) data['pre_checkout_query'] = PreCheckoutQuery.de_json(data.get('pre_checkout_query'), bot) channel_post = data.get('channel_post') if channel_post: channel_post['default_quote'] = data.get('default_quote') data['channel_post'] = Message.de_json(channel_post, bot) edited_channel_post = data.get('edited_channel_post') if edited_channel_post: edited_channel_post['default_quote'] = data.get('default_quote') data['edited_channel_post'] = Message.de_json(edited_channel_post, bot) data['poll'] = Poll.de_json(data.get('poll'), bot) return cls(**data)
def de_json(cls, data, bot): if not data: return None data = super(Update, cls).de_json(data, bot) data['message'] = Message.de_json(data.get('message'), bot) data['edited_message'] = Message.de_json(data.get('edited_message'), bot) data['inline_query'] = InlineQuery.de_json(data.get('inline_query'), bot) data['chosen_inline_result'] = ChosenInlineResult.de_json( data.get('chosen_inline_result'), bot) data['callback_query'] = CallbackQuery.de_json( data.get('callback_query'), bot) data['shipping_query'] = ShippingQuery.de_json( data.get('shipping_query'), bot) data['pre_checkout_query'] = PreCheckoutQuery.de_json( data.get('pre_checkout_query'), bot) data['channel_post'] = Message.de_json(data.get('channel_post'), bot) data['edited_channel_post'] = Message.de_json( data.get('edited_channel_post'), bot) data['poll'] = Poll.de_json(data.get('poll'), bot) return cls(**data)
def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Update']: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data['message'] = Message.de_json(data.get('message'), bot) data['edited_message'] = Message.de_json(data.get('edited_message'), bot) data['inline_query'] = InlineQuery.de_json(data.get('inline_query'), bot) data['chosen_inline_result'] = ChosenInlineResult.de_json( data.get('chosen_inline_result'), bot) data['callback_query'] = CallbackQuery.de_json( data.get('callback_query'), bot) data['shipping_query'] = ShippingQuery.de_json( data.get('shipping_query'), bot) data['pre_checkout_query'] = PreCheckoutQuery.de_json( data.get('pre_checkout_query'), bot) data['channel_post'] = Message.de_json(data.get('channel_post'), bot) data['edited_channel_post'] = Message.de_json( data.get('edited_channel_post'), bot) data['poll'] = Poll.de_json(data.get('poll'), bot) data['poll_answer'] = PollAnswer.de_json(data.get('poll_answer'), bot) data['my_chat_member'] = ChatMemberUpdated.de_json( data.get('my_chat_member'), bot) data['chat_member'] = ChatMemberUpdated.de_json( data.get('chat_member'), bot) data['chat_join_request'] = ChatJoinRequest.de_json( data.get('chat_join_request'), bot) return cls(**data)
def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Update']: data = cls.parse_data(data) if not data: return None data['message'] = Message.de_json(data.get('message'), bot) data['edited_message'] = Message.de_json(data.get('edited_message'), bot) data['inline_query'] = InlineQuery.de_json(data.get('inline_query'), bot) data['chosen_inline_result'] = ChosenInlineResult.de_json( data.get('chosen_inline_result'), bot) data['callback_query'] = CallbackQuery.de_json( data.get('callback_query'), bot) data['shipping_query'] = ShippingQuery.de_json( data.get('shipping_query'), bot) data['pre_checkout_query'] = PreCheckoutQuery.de_json( data.get('pre_checkout_query'), bot) data['channel_post'] = Message.de_json(data.get('channel_post'), bot) data['edited_channel_post'] = Message.de_json( data.get('edited_channel_post'), bot) data['poll'] = Poll.de_json(data.get('poll'), bot) data['poll_answer'] = PollAnswer.de_json(data.get('poll_answer'), bot) return cls(**data)
def test_equality(self): a = Poll(123, "question", ["O1", "O2"], 1, False, True, Poll.REGULAR, True) b = Poll(123, "question", ["o1", "o2"], 1, True, False, Poll.REGULAR, True) c = Poll(456, "question", ["o1", "o2"], 1, True, False, Poll.REGULAR, True) d = PollOption("Text", 1) assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)
def poll(): return Poll(TestPoll.id_, TestPoll.question, TestPoll.options, TestPoll.total_voter_count, TestPoll.is_closed, TestPoll.is_anonymous, TestPoll.type, TestPoll.allows_multiple_answers, explanation=TestPoll.explanation, explanation_entities=TestPoll.explanation_entities, open_period=TestPoll.open_period, close_date=TestPoll.close_date, )
def poll(bot): return Update( 0, poll=Poll( 1, "question", [PollOption("1", 0), PollOption("2", 0)], 0, False, False, Poll.REGULAR, True, ), )
def test_enum_init(self): poll = Poll( type="foo", id="id", question="question", options=[], total_voter_count=0, is_closed=False, is_anonymous=False, allows_multiple_answers=False, ) assert poll.type == "foo" poll = Poll( type=PollType.QUIZ, id="id", question="question", options=[], total_voter_count=0, is_closed=False, is_anonymous=False, allows_multiple_answers=False, ) assert poll.type is PollType.QUIZ
def poll(bot): return Update( 0, poll=Poll( 1, 'question', [PollOption('1', 0), PollOption('2', 0)], 0, False, False, Poll.REGULAR, True, ), )
def test_de_json(self): json_dict = { 'id': self.id_, 'question': self.question, 'options': [o.to_dict() for o in self.options], 'is_closed': self.is_closed } poll = Poll.de_json(json_dict, None) assert poll.id == self.id_ assert poll.question == self.question assert poll.options == self.options assert poll.options[0].text == self.options[0].text assert poll.options[0].voter_count == self.options[0].voter_count assert poll.options[1].text == self.options[1].text assert poll.options[1].voter_count == self.options[1].voter_count assert poll.is_closed == self.is_closed
def test_parse_entity(self, poll): entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17) poll.explanation_entities = [entity] assert poll.parse_explanation_entity(entity) == "http://google.com" with pytest.raises(RuntimeError, match="Poll has no"): Poll( "id", "question", [PollOption("text", voter_count=0)], total_voter_count=0, is_closed=False, is_anonymous=False, type=Poll.QUIZ, allows_multiple_answers=False, ).parse_explanation_entity(entity)
def test_equality(self): a = PollOption('text', 1) b = PollOption('text', 1) c = PollOption('text_1', 1) d = PollOption('text', 2) e = Poll(123, 'question', ['O1', 'O2'], 1, False, True, Poll.REGULAR, True) assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d) assert a != e assert hash(a) != hash(e)
def fake_poll(): return Message( message_id=123, from_user=None, chat=None, date=None, poll=Poll( random.randint(100, 500), 'question', None, 0, False, False, Poll.QUIZ, False, random.randint(0, 3), ), )
def fake_poll(): return Message( 123, None, None, None, poll=Poll( random.randint(100, 500), 'question', None, 0, False, False, Poll.QUIZ, False, random.randint(0, 3), ), )
def test_equality(self): a = PollOption("text", 1) b = PollOption("text", 1) c = PollOption("text_1", 1) d = PollOption("text", 2) e = Poll(123, "question", ["O1", "O2"], 1, False, True, Poll.REGULAR, True) assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d) assert a != e assert hash(a) != hash(e)
'photo': [PhotoSize('photo_id', 'unique_id', 50, 50)], 'caption': 'photo_file', 'media_group_id': 1234443322222, }, { 'passport_data': PassportData.de_json(RAW_PASSPORT_DATA, None) }, { 'poll': Poll( id='abc', question='What is this?', options=[ PollOption(text='a', voter_count=1), PollOption(text='b', voter_count=2) ], is_closed=False, total_voter_count=0, is_anonymous=False, type=Poll.REGULAR, allows_multiple_answers=True, explanation_entities=[], ) }, { 'text': 'a text message', 'reply_markup': { 'inline_keyboard': [ [ { 'text': 'start', 'url': 'http://google.com'
}, { 'edited_channel_post': message }, { 'inline_query': InlineQuery(1, User(1, '', False), '', '') }, { 'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '') }, { 'shipping_query': ShippingQuery('id', User(1, '', False), '', None) }, { 'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '') }, { 'callback_query': CallbackQuery(1, User(1, '', False), 'chat') }, { 'poll': Poll('id', '?', [PollOption('.', 1)], False) }] all_types = ('message', 'edited_message', 'callback_query', 'channel_post', 'edited_channel_post', 'inline_query', 'chosen_inline_result', 'shipping_query', 'pre_checkout_query', 'poll') ids = all_types + ('callback_query_without_message', ) @pytest.fixture(params=params, ids=ids) def update(request): return Update(update_id=TestUpdate.update_id, **request.param) class TestUpdate(object):
'inline_query': InlineQuery(1, User(1, '', False), '', '') }, { 'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '') }, { 'shipping_query': ShippingQuery('id', User(1, '', False), '', None) }, { 'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '') }, { 'poll': Poll('id', '?', [PollOption('.', 1)], False, False, False, Poll.REGULAR, True) }, { 'poll_answer': PollAnswer("id", User(1, '', False), [1]) }, { 'my_chat_member': chat_member_updated }, { 'chat_member': chat_member_updated }, { 'chat_join_request': chat_join_request }, # Must be last to conform with `ids` below! {
def poll(): return Poll(TestPoll.id_, TestPoll.question, TestPoll.options, TestPoll.is_closed)
}, { 'forward_signature': 'some_forward_sign' }, { 'author_signature': 'some_author_sign' }, { 'photo': [PhotoSize('photo_id', 50, 50)], 'caption': 'photo_file', 'media_group_id': 1234443322222 }, { 'passport_data': PassportData.de_json(RAW_PASSPORT_DATA, None) }, { 'poll': Poll(id='abc', question='What is this?', options=[ PollOption(text='a', voter_count=1), PollOption(text='b', voter_count=2) ], is_closed=False) }, { 'text': 'a text message', 'reply_markup': { 'inline_keyboard': [[{ 'text': 'start', 'url': 'http://google.com' }, { 'text': 'next', 'callback_data': 'abcd' }], [{ 'text': 'Cancel', 'callback_data': 'Cancel'
"inline_query": InlineQuery(1, User(1, "", False), "", "") }, { "chosen_inline_result": ChosenInlineResult("id", User(1, "", False), "") }, { "shipping_query": ShippingQuery("id", User(1, "", False), "", None) }, { "pre_checkout_query": PreCheckoutQuery("id", User(1, "", False), "", 0, "") }, { "poll": Poll("id", "?", [PollOption(".", 1)], False, False, False, Poll.REGULAR, True) }, { "poll_answer": PollAnswer("id", User(1, "", False), [1]) }, { "my_chat_member": chat_member_updated }, { "chat_member": chat_member_updated }, { "chat_join_request": chat_join_request }, # Must be last to conform with `ids` below! {
def poll(): return Poll(TestPoll.id_, TestPoll.question, TestPoll.options, TestPoll.total_voter_count, TestPoll.is_closed, TestPoll.is_anonymous, TestPoll.type, TestPoll.allows_multiple_answers)