Ejemplo n.º 1
0
    def test_get(self, mocker, is_match_value, rule, content_type, incoming,
                 chat_type):
        chat_type = mocker.MagicMock() if chat_type else None
        incoming = mocker.MagicMock() if incoming else None
        content_type = mocker.MagicMock() if content_type else None
        handler = mocker.MagicMock()
        handler.rule = rule
        raw = mocker.MagicMock()

        mock_is_match = mocker.patch("aiotelegrambot.handler.is_match",
                                     return_value=is_match_value)

        h = Handlers()
        h._handlers[chat_type][incoming][content_type].append(handler)

        _incoming = incoming if incoming else 1
        _content_type = content_type if content_type else 1

        assert h.get(chat_type if chat_type else 1, _incoming,
                     content_type if content_type else 1,
                     raw) == (handler if rule is None or is_match_value is True
                              else h._default_handler)

        if rule is None:
            assert mock_is_match.call_count == 0
        else:
            mock_is_match.assert_called_once_with(handler.rule, _incoming,
                                                  _content_type, raw)
Ejemplo n.º 2
0
    def test_add(self, mocker):
        mock_prepare_rule = mocker.patch("aiotelegrambot.handler.prepare_rule")

        h = Handlers()
        mock_handler_cls = mocker.patch.object(h, "_handler_cls")

        chat_type = mocker.MagicMock()
        incoming = mocker.MagicMock()
        incoming.is_message_or_post = True
        content_type = mocker.MagicMock()
        rule = "/help"

        def handler():
            pass

        result = h.add(chat_type=chat_type,
                       incoming=incoming,
                       content_type=content_type,
                       rule=rule)(handler)
        assert result is handler
        assert len(h._handlers[chat_type][incoming][content_type]) == 1
        assert h._handlers[chat_type][incoming][content_type][
            0] is mock_handler_cls.return_value
        mock_handler_cls.assert_called_once_with(
            handler, chat_type, incoming, content_type,
            mock_prepare_rule.return_value)
Ejemplo n.º 3
0
    def test___init__(self):
        h = Handlers()

        assert h._handler_cls is Handler
        assert isinstance(h._default_handler, Handler)
        assert h._default_handler.handler is None
        h._handlers[1][2][3].append(4)
Ejemplo n.º 4
0
    def test_add_sorted(self, mocker):
        chat_type = mocker.MagicMock()
        incoming = mocker.MagicMock()
        incoming.is_message_or_post = True
        content_type = mocker.MagicMock()

        rule1 = mocker.MagicMock()
        rule1.priority = 1

        rule2 = mocker.MagicMock()
        rule2.priority = 2

        rule3 = mocker.MagicMock()
        rule3.priority = 3

        def handler():
            pass

        h = Handlers()
        h.add(chat_type=chat_type,
              incoming=incoming,
              content_type=content_type,
              rule=rule3)(handler)
        h.add(chat_type=chat_type,
              incoming=incoming,
              content_type=content_type,
              rule=rule1)(handler)
        h.add(chat_type=chat_type,
              incoming=incoming,
              content_type=content_type,
              rule=rule2)(handler)

        handlers = h._handlers[chat_type][incoming][content_type]
        assert handlers[0].priority == rule1.priority
        assert handlers[1].priority == rule2.priority
        assert handlers[2].priority == rule3.priority
Ejemplo n.º 5
0
#!/usr/bin/env python

import settings

import json
import os
import ssl

from aiohttp import web, ClientSession
from async_generator import async_generator, yield_

from aiotelegrambot import Bot, Client, Content, Handlers, Message
from aiotelegrambot.rules import Contains

handlers = Handlers()


@handlers.add(content_type=Content.PHOTO)
async def photo(message: Message):
    photos = message.raw.get("message", {}).get("photo", [])
    date = message.raw.get("message", {}).get("date", 0)
    fid = None
    size = 0
    for p in photos:
        if size < p.get("file_size", 0):
            fid = p.get("file_id", None)
            size = p.get("file_size")
    if not fid:
        print("error:", message.raw)
        await message.send_message("Error retrieving photo details, sorry.")
        return
Ejemplo n.º 6
0
    def test___bool__(self):
        h = Handlers()
        assert bool(h) is False

        h._handlers[1][2][3].append(4)
        assert bool(h) is True
Ejemplo n.º 7
0
    def test_add_error(self, mocker):
        mock_prepare_rule = mocker.patch("aiotelegrambot.handler.prepare_rule")

        h = Handlers()

        incoming = mocker.MagicMock()
        incoming.is_message_or_post = False

        with pytest.raises(HandlerError):
            h.add(incoming=incoming, content_type=Content.TEXT)

        ##################################

        incoming = mocker.MagicMock()
        incoming.is_message_or_post = False

        with pytest.raises(HandlerError):
            h.add(incoming=incoming, rule="/help")

        ##################################

        incoming = mocker.MagicMock()
        incoming.is_message_or_post = False

        assert callable(h.add(incoming=incoming))
        assert mock_prepare_rule.call_count == 0

        ##################################

        incoming = mocker.MagicMock()
        incoming.is_message_or_post = True
        content_type = mocker.MagicMock()
        rule = "/help"
        with pytest.raises(ValueError):
            h.add(incoming=incoming, content_type=content_type,
                  rule=rule)(None)
        mock_prepare_rule.assert_called_once_with(content_type, rule)

        ##################################

        chat_type = mocker.MagicMock()
        incoming = mocker.MagicMock()
        incoming.is_message_or_post = True
        content_type = mocker.MagicMock()
        rule = None
        handler = mocker.MagicMock()
        handler.rule = rule
        h._handlers[chat_type][incoming][content_type].append(handler)
        with pytest.raises(HandlerError):
            h.add(chat_type=chat_type,
                  incoming=incoming,
                  content_type=content_type,
                  rule=rule)(handler)
        assert mock_prepare_rule.call_count == 1