def test_global(self, cls): instrument = cls() assert isinstance(instrument, Instrument) assert instrument <= Instrument() assert Instrument() >= instrument assert instrument < Instrument() or type(instrument) is Instrument assert Instrument() > instrument or type(instrument) is Instrument assert not instrument > Instrument() assert instrument == cls() assert hash(instrument) == hash(cls())
def parse_instruments_keyboard( keyboard: InlineKeyboardMarkup) -> Dict[Instrument, bool]: """ Parses the given keyboard and returns ad dictionary, indicating for every instrument, whether it has been selected or not. Args: keyboard: The :class:`telegram.InlineKeyboardMarkup` from an update. Buttons must have the structure as provided by :meth:`build_instruments_keyboard`. """ current_selection = dict() for row in keyboard.inline_keyboard[:-1]: for button in row: instrument, selection = button.text.split(' ') key = Instrument.from_string(instrument, allowed=Member.ALLOWED_INSTRUMENTS) if key is not None: current_selection[key] = selection == SELECTED return current_selection
def instruments(update: Update, context: CallbackContext) -> str: """ Parses the reply and for the instruments. Args: update: The update. context: The context as provided by the :class:`telegram.ext.Dispatcher`. Returns: :attr:`MENU`: If the user is not yet done selecting :attr:`ALLOW_CONTACT_SHARING`: Else. """ message = update.effective_message data = update.callback_query.data orchestra = context.bot_data[ORCHESTRA_KEY] member = get_member(update, context) update.callback_query.answer() if data == DONE: message.edit_text( text=TEXTS[MENU].format(member.to_str()), reply_markup=selection_keyboard(context) ) return MENU current_selection = parse_instruments_keyboard(message.reply_markup) instrument, selection = data.split(' ') key = Instrument.from_string(instrument, allowed=Member.ALLOWED_INSTRUMENTS) if key is not None: current_selection[key] = not selection == SELECTED member.instruments = [i for i, s in current_selection.items() if s] orchestra.update_member(member) message.edit_reply_markup(reply_markup=build_instruments_keyboard(current_selection)) return INSTRUMENTS
def string_to_instrument(string: str) -> Optional[Instrument]: try: return Instrument.from_string(string) except ValueError: return None
def test_from_string_allowed(self): assert Instrument.from_string('tub', []) is None assert Instrument.from_string('tub', [Trombone(), 'Schlagzeug']) is None assert Instrument.from_string('tub', ['Tuba']) == Tuba() assert Instrument.from_string('tub', [Tuba()]) == Tuba()
def test_from_string_unknown(self, string): with pytest.raises(ValueError, match='Unknown'): Instrument.from_string(string)
def test_from_string_by_abbreviation(self, abbr, expected): assert Instrument.from_string(abbr) == expected
def test_from_string_by_name(self, cls): assert Instrument.from_string(cls.name) == cls()