def test_slot_behaviour(self, recwarn, mro_slots): inst = TelegramObject() for attr in inst.__slots__: assert getattr(inst, attr, 'err') != 'err', f"got extra slot '{attr}'" assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot" inst.custom = 'should give warning' assert len(recwarn) == 1 and 'custom' in str( recwarn[0].message), recwarn.list
def test_slot_behaviour(self, mro_slots): inst = TelegramObject() for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot"
def test_bot_instance_states(self, bot_inst): tg_object = TelegramObject() tg_object.set_bot("bot" if bot_inst == "bot" else bot_inst) if bot_inst == "bot": assert tg_object.get_bot() == "bot" elif bot_inst is None: with pytest.raises(RuntimeError): tg_object.get_bot()
def self_from_object(self, object: TelegramObject): if hasattr(object, 'bot') and isinstance(object.bot, Bot): self._bot = object.bot for key, value in self._get_dict_from_object(self, object).items(): self.__setattr__(key, value) if hasattr(self, 'original_object') and isinstance( self._fields.get('original_object'), DictField): self.original_object = object.to_dict()
def test_to_json_ujson(self, monkeypatch): # to_json simply takes whatever comes from to_dict, therefore we only need to test it once telegram_object = TelegramObject() # Test that it works with a dict with str keys as well as dicts as lists as values d = {'str': 'str', 'str2': ['str', 'str'], 'str3': {'str': 'str'}} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) json = telegram_object.to_json() # Order isn't guarantied and ujon discards whitespace assert '"str":"str"' in json assert '"str2":["str","str"]' in json assert '"str3":{"str":"str"}' in json # Test that ujson allows tuples # NOTE: This could be seen as a bug (since it's differnt from the normal "json", # but we test it anyways d = {('str', 'str'): 'str'} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) telegram_object.to_json()
def test_to_json(self, monkeypatch): # to_json simply takes whatever comes from to_dict, therefore we only need to test it once telegram_object = TelegramObject() # Test that it works with a dict with str keys as well as dicts as lists as values d = {"str": "str", "str2": ["str", "str"], "str3": {"str": "str"}} monkeypatch.setattr("telegram.TelegramObject.to_dict", lambda _: d) json = telegram_object.to_json() # Order isn't guarantied assert '"str": "str"' in json assert '"str2": ["str", "str"]' in json assert '"str3": {"str": "str"}' in json # Now make sure that it doesn't work with not json stuff and that it fails loudly # Tuples aren't allowed as keys in json d = {("str", "str"): "str"} monkeypatch.setattr("telegram.TelegramObject.to_dict", lambda _: d) with pytest.raises(TypeError): telegram_object.to_json()
def test_to_json_native(self, monkeypatch): if ujson: monkeypatch.setattr('ujson.dumps', json_lib.dumps) # to_json simply takes whatever comes from to_dict, therefore we only need to test it once telegram_object = TelegramObject() # Test that it works with a dict with str keys as well as dicts as lists as values d = {'str': 'str', 'str2': ['str', 'str'], 'str3': {'str': 'str'}} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) json = telegram_object.to_json() # Order isn't guarantied assert '"str": "str"' in json assert '"str2": ["str", "str"]' in json assert '"str3": {"str": "str"}' in json # Now make sure that it doesn't work with not json stuff and that it fails loudly # Tuples aren't allowed as keys in json d = {('str', 'str'): 'str'} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) with pytest.raises(TypeError): telegram_object.to_json()
def test_to_json_native(monkeypatch): if rapidjson: monkeypatch.setattr('rapidjson.dumps', json_lib.dumps) # to_json simply takes whatever comes from to_dict, therefore we only need to test it once telegram_object = TelegramObject() # Test that it works with a dict with str keys as well as dicts as lists as values d = {'str': 'str', 'str2': ['str', 'str'], 'str3': {'str': 'str'}} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) json = telegram_object.to_json() # Order isn't guarantied assert '"str": "str"' in json assert '"str2": ["str", "str"]' in json assert '"str3": {"str": "str"}' in json # Now make sure that it doesn't work with not json stuff and that it fails loudly # Tuples aren't allowed as keys in json d = {('str', 'str'): 'str'} monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) with pytest.raises(TypeError): telegram_object.to_json()
def prepare_params(attachment: TelegramObject, at: str, reply_to_id: int) -> Optional[dict]: """Функция-помощник, которая подготовит данные для всех методов бота по шаблону ``send_*``.""" if isinstance(attachment, list): if len(attachment) > 0: attachment = attachment[0] else: return None params = attachment.to_dict() logger.debug(json.dumps(params)) file_id = params.pop("file_id", None) if file_id is not None: params[at] = file_id params["chat_id"] = reply_to_id return params
def prepare_params(attachment: TelegramObject, at: str, reply_to_id: int) -> Optional[dict]: """prepare params for bots' ``send_*``.""" if isinstance(attachment, list): if len(attachment) > 0: attachment = attachment[0] else: return None params = attachment.to_dict() logger.debug(json.dumps(params)) file_id = params.pop("file_id", None) if file_id is not None: params[at] = file_id params["chat_id"] = reply_to_id return params
def test_bot_instance_none(self): tg_object = TelegramObject() with pytest.raises(RuntimeError): tg_object.get_bot()
def tgoToJson(tgo: TelegramObject): return json.dumps(tgo.to_dict(), sort_keys=True, indent=2)