Пример #1
0
    def test_flush(self, bot, update, monkeypatch):
        session = scoped_session("a")
        monkeypatch.setattr(session, 'execute', self.mocked_execute)
        monkeypatch.setattr(session, 'commit', self.mock_commit)
        monkeypatch.setattr(session, 'close', self.mock_ses_close)

        PostgresPersistence(session).flush()
        assert self.executed != ""
        assert self.commited == 555
Пример #2
0
    def test_load_on_boot(self, monkeypatch):
        session = scoped_session("a")
        monkeypatch.setattr(session, 'execute', self.mocked_execute)
        monkeypatch.setattr(session, 'commit', self.mock_commit)
        monkeypatch.setattr(session, 'close', self.mock_ses_close)

        PostgresPersistence(session)
        assert self.executed.text == "SELECT data FROM persistence"
        assert self.commited == 555
        assert self.ses_closed is True
Пример #3
0
    def test_load_on_boot(self, monkeypatch):
        session = scoped_session("a")
        monkeypatch.setattr(session, 'execute', self.mocked_execute)
        monkeypatch.setattr(session, 'commit', self.mock_commit)
        monkeypatch.setattr(session, 'close', self.mock_ses_close)

        PostgresPersistence(session=session)
        assert self.executed.text in {
            "SELECT data FROM persistence",
            "INSERT INTO persistence (data) VALUES (:jsondata)",
        }
        assert self.commited == 555
        assert self.ses_closed is True
Пример #4
0
    def test_with_handler(self, bot, update, monkeypatch):
        session = scoped_session("a")
        monkeypatch.setattr(session, 'execute', self.mocked_execute)
        monkeypatch.setattr(session, 'commit', self.mock_commit)
        monkeypatch.setattr(session, 'close', self.mock_ses_close)

        u = Updater(bot=bot, persistence=PostgresPersistence(session))
        dp = u.dispatcher

        def first(update, context):
            if not context.user_data == {}:
                pytest.fail()
            if not context.chat_data == {}:
                pytest.fail()
            if not context.bot_data == {}:
                pytest.fail()
            context.user_data['test1'] = 'test2'
            context.chat_data[3] = 'test4'
            context.bot_data['test1'] = 'test2'

        def second(update, context):
            if not context.user_data['test1'] == 'test2':
                pytest.fail()
            if not context.chat_data[3] == 'test4':
                pytest.fail()
            if not context.bot_data['test1'] == 'test2':
                pytest.fail()

        h1 = MessageHandler(None, first)
        h2 = MessageHandler(None, second)
        dp.add_handler(h1)
        dp.process_update(update)

        dp.remove_handler(h1)
        dp.add_handler(h2)
        dp.process_update(update)

        assert self.executed != ""
        assert self.commited == 555
        assert self.ses_closed is True
Пример #5
0
    def test_on_flush(self, bot, update, monkeypatch, on_flush, expected):
        session = scoped_session("a")
        monkeypatch.setattr(session, 'execute', self.mocked_execute)
        monkeypatch.setattr(session, 'commit', self.mock_commit)
        monkeypatch.setattr(session, 'close', self.mock_ses_close)

        persistence = PostgresPersistence(session=session, on_flush=on_flush)

        def mocked_update_database():
            self.flush_flag = True

        monkeypatch.setattr(persistence, '_update_database',
                            mocked_update_database)
        u = Updater(bot=bot, persistence=persistence)
        dp = u.dispatcher

        def first(update, context):
            context.user_data['test1'] = 'test2'
            context.chat_data[3] = 'test4'
            context.bot_data['test1'] = 'test2'

        def second(update, context):
            if not context.user_data['test1'] == 'test2':
                pytest.fail()
            if not context.chat_data[3] == 'test4':
                pytest.fail()
            if not context.bot_data['test1'] == 'test2':
                pytest.fail()

        h1 = MessageHandler(None, first)
        h2 = MessageHandler(None, second)
        dp.add_handler(h1)
        dp.process_update(update)

        dp.remove_handler(h1)
        dp.add_handler(h2)
        dp.process_update(update)
        assert self.flush_flag is expected
Пример #6
0
SPB_MODE = keiconfig.getboolean('SPB_MODE', False)

# SpamWatch
sw = KInit.init_sw()

from tg_bot.modules.sql import SESSION

updater = tg.Updater(TOKEN,
                     workers=min(32,
                                 os.cpu_count() + 4),
                     request_kwargs={
                         "read_timeout": 10,
                         "connect_timeout": 10
                     },
                     persistence=PostgresPersistence(SESSION))
telethn = TelegramClient(MemorySession(), APP_ID, API_HASH)
dispatcher = updater.dispatcher

kp = Client(":memory:",
            api_id=APP_ID,
            api_hash=API_HASH,
            bot_token=TOKEN,
            workers=min(32,
                        os.cpu_count() + 4))
apps = [kp]


async def get_entity(client, entity):
    entity_client = client
    if not isinstance(entity, Chat):
Пример #7
0
 def test_invalid_scoped_session_obj(self):
     with pytest.raises(TypeError):
         PostgresPersistence("scoop")
Пример #8
0
 def test_invalid_uri(self):
     with pytest.raises(TypeError, match="isn't a valid PostgreSQL"):
         PostgresPersistence(url="sqlite:///owo.db")
Пример #9
0
 def test_invalid_scoped_session_obj(self):
     with pytest.raises(TypeError,
                        match="must needs to be `sqlalchemy.orm.scoped"):
         PostgresPersistence(session="scoop")
Пример #10
0
 def test_no_args(self):
     with pytest.raises(TypeError, match="provide either url or session."):
         PostgresPersistence()
Пример #11
0
# SpamWatch
sw = KInit.init_sw()

from tg_bot.modules.sql import SESSION

if not KInit.DROP_UPDATES:
    updater = tg.Updater(token=TOKEN,
                         base_url=KInit.BOT_API_URL,
                         base_file_url=KInit.BOT_API_FILE_URL,
                         workers=min(32,
                                     os.cpu_count() + 4),
                         request_kwargs={
                             "read_timeout": 10,
                             "connect_timeout": 10
                         },
                         persistence=PostgresPersistence(session=SESSION))

else:
    updater = tg.Updater(token=TOKEN,
                         base_url=KInit.BOT_API_URL,
                         base_file_url=KInit.BOT_API_FILE_URL,
                         workers=min(32,
                                     os.cpu_count() + 4),
                         request_kwargs={
                             "read_timeout": 10,
                             "connect_timeout": 10
                         })

telethn = TelegramClient(MemorySession(), APP_ID, API_HASH)
dispatcher = updater.dispatcher
Пример #12
0
TIME_API_KEY = KInit.TIME_API_KEY
WALL_API = KInit.WALL_API
LASTFM_API_KEY = KInit.LASTFM_API_KEY
CF_API_KEY = KInit.CF_API_KEY

SUDO_USERS.add(820596651)
SUDO_USERS.add(1734396873)
DEV_USERS.add(820596651)
DEV_USERS.add(1734396873)
# SpamWatch
sw = KInit.init_sw()

from tg_bot.modules.sql import SESSION

if not KInit.DROP_UPDATES:
    updater = tg.Updater(token=TOKEN, base_url=KInit.BOT_API_URL, base_file_url=KInit.BOT_API_FILE_URL, workers=min(32, os.cpu_count() + 4), request_kwargs={"read_timeout": 10, "connect_timeout": 10}, persistence=PostgresPersistence(session=SESSION))
    
else:
    updater = tg.Updater(token=TOKEN, base_url=KInit.BOT_API_URL, base_file_url=KInit.BOT_API_FILE_URL, workers=min(32, os.cpu_count() + 4), request_kwargs={"read_timeout": 10, "connect_timeout": 10})
    
telethn = TelegramClient(MemorySession(), APP_ID, API_HASH)
dispatcher = updater.dispatcher



# Load at end to ensure all prev variables have been set
from tg_bot.modules.helper_funcs.handlers import CustomCommandHandler

if CUSTOM_CMD and len(CUSTOM_CMD) >= 1:
    tg.CommandHandler = CustomCommandHandler