Code Monkey home page Code Monkey logo

aiogram_tests's Introduction

aiogram_tests's People

Contributors

aiogram-tests avatar bomzheg avatar occcas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

aiogram_tests's Issues

pytest.mark.asyncio

Since all related test should be async, it would be nice add global async support

pyproject.toml example:

[tool.pytest.ini_options]
asyncio_mode = "auto"

No module named 'aiogram.utils.helper'

I can't even run the basic example even though version aiogram==3.0.0b7.

Tell me please, what could be the problem?
All code and error is attached below.

@router.message(Command(commands=["start"]))
async def command_handler(message: Message, state: FSMContext) -> None:
await message.answer("Hello, new user!")

import pytest
from aiogram.filters import Command
from aiogram_tests import MockedBot
from aiogram_tests.handler import MessageHandler
from aiogram_tests.types.dataset import MESSAGE

from handlers.users.start import command_handler

@pytest.mark.asyncio
async def test_command_handler():
requester = MockedBot(MessageHandler(command_handler, Command(commands=["start"])))
calls = await requester.query(MESSAGE.as_object(text="/start"))
answer_message = calls.send_message.fetchone().text
assert answer_message == "Hi there! What's your name?"

============================= test session starts =============================
collecting ...
test_start.py:None (test_start.py)
ImportError while importing test module 'C:\Users\inver\PycharmProjects\aiogram3-bot-template\tests\test_start.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\venv\Lib\site-packages_pytest\python.py:617: in importtestmodule
mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
..\venv\Lib\site-packages_pytest\pathlib.py:565: in import_path
importlib.import_module(module_name)
......\AppData\Local\Programs\Python\Python311\Lib\importlib_init
.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1147: in _find_and_load_unlocked
???
:690: in load_unlocked
???
..\venv\Lib\site-packages_pytest\assertion\rewrite.py:178: in exec_module
exec(co, module.dict)
test_start.py:28: in
from aiogram_tests import MockedBot
..\venv\Lib\site-packages\aiogram_tests_init
.py:1: in
from .requester import MockedBot
..\venv\Lib\site-packages\aiogram_tests\requester.py:1: in
from aiogram.utils.helper import Helper
E ModuleNotFoundError: No module named 'aiogram.utils.helper'
collected 0 items / 1 error

!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 1.04s ===============================

How to test update flow through handlers?

Hello and thank you for the library.
In your examples, the handler is predefined each time. How can I just create an update and check if it hits the right handler is my code?

Can't reproduce simple example from README.md

Just copied 2 peaces of code from README.md in 2 files: bot.py and test.py.
Full traceback:

test.py::test_echo FAILED                                                [100%]
test.py:9 (test_echo)
@pytest.mark.asyncio
    async def test_echo():
        request = MockedBot(MessageHandler(echo))
>       calls = await request.query(message=MESSAGE.as_object(text="Hello, Bot!"))

test.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
venv/lib/python3.11/site-packages/aiogram_tests/requester.py:52: in query
    await self._handler(*args, **kwargs)
venv/lib/python3.11/site-packages/aiogram_tests/handler/handler.py:53: in __call__
    await self.feed_update(*args, **kwargs)
venv/lib/python3.11/site-packages/aiogram_tests/handler/handler.py:95: in feed_update
    await self.dp.feed_update(self.bot, types.Update(update_id=12345678, message=message))
venv/lib/python3.11/site-packages/aiogram/dispatcher/dispatcher.py:146: in feed_update
    response = await self.update.wrap_outer_middleware(
venv/lib/python3.11/site-packages/aiogram/dispatcher/middlewares/error.py:25: in __call__
    return await handler(event, data)
venv/lib/python3.11/site-packages/aiogram/dispatcher/middlewares/user_context.py:23: in __call__
    return await handler(event, data)
/usr/local/lib/python3.11/contextlib.py:222: in __aexit__
    await self.gen.athrow(typ, value, traceback)
venv/lib/python3.11/site-packages/aiogram/fsm/storage/memory.py:57: in lock
    yield
venv/lib/python3.11/site-packages/aiogram/fsm/middleware.py:39: in __call__
    return await handler(event, data)
venv/lib/python3.11/site-packages/aiogram/dispatcher/event/telegram.py:123: in trigger
    return await wrapped_inner(event, kwargs)
venv/lib/python3.11/site-packages/aiogram/dispatcher/event/handler.py:42: in call
    return await wrapped()
venv/lib/python3.11/site-packages/aiogram/dispatcher/dispatcher.py:264: in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
venv/lib/python3.11/site-packages/aiogram/dispatcher/router.py:115: in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
venv/lib/python3.11/site-packages/aiogram/dispatcher/router.py:111: in _wrapped
    return await self._propagate_event(
venv/lib/python3.11/site-packages/aiogram/dispatcher/router.py:124: in _propagate_event
    response = await observer.trigger(event, **kwargs)
venv/lib/python3.11/site-packages/aiogram/dispatcher/event/telegram.py:123: in trigger
    return await wrapped_inner(event, kwargs)
venv/lib/python3.11/site-packages/aiogram/dispatcher/event/handler.py:42: in call
    return await wrapped()
bot.py:11: in echo
    await message.answer(message.text)
venv/lib/python3.11/site-packages/aiogram/methods/base.py:89: in emit
    return await bot(self)
venv/lib/python3.11/site-packages/aiogram_tests/mocked_bot.py:94: in __call__
    return await super().__call__(method, request_timeout)
venv/lib/python3.11/site-packages/aiogram/client/bot.py:374: in __call__
    return await self.session(self, method, timeout=request_timeout)
venv/lib/python3.11/site-packages/aiogram/client/session/base.py:194: in __call__
    return cast(TelegramType, await middleware(bot, method))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <aiogram_tests.mocked_bot.MockedSession object at 0x7f9343cbc7d0>
bot = <aiogram_tests.mocked_bot.MockedBot object at 0x7f9343cbc710>
method = SendMessage(chat_id=12345678, text='Hello, Bot!', message_thread_id=None, parse_mode=sentinel.UNSET, entities=None, di...notification=None, protect_content=None, reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None)
timeout = None

    async def make_request(
        self, bot: Bot, method: TelegramMethod[TelegramType], timeout: Optional[int] = UNSET
    ) -> TelegramType:
        self.closed = False
        self.requests.append(method.build_request(bot))
>       response: Response[TelegramType] = self.responses.pop()
E       IndexError: pop from an empty deque

venv/lib/python3.11/site-packages/aiogram_tests/mocked_bot.py:44: IndexError


============================== 1 failed in 0.54s ===============================

Process finished with exit code 1

Broken with recent version of aiogram

Broke after aiogram commit 3ea73fbbbdfd5dd5c71e9b19e39cbd1ff99b500d

`import pytest
from aiogram_tests import MockedBot
from aiogram_tests.handler import MessageHandler
from aiogram_tests.types.dataset import BOT_COMMAND

from denis_hodge_podge_bot.handlers.start import start

@pytest.mark.asyncio
async def test_start():
requester = MockedBot(MessageHandler(start))
calls = await requester.query(BOT_COMMAND(command='start'))
answer_message = calls.send_message.fetchone().text
print(answer_message)
`

______________________________________________________________ ERROR collecting tests/test_start.py ______________________________________________________________
ImportError while importing test module 'C:\Users\Sergei\PycharmProjects\Telegram\HodgepodgeBot\tests\test_start.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Python310\lib\importlib_init_.py:126: in import_module
return _bootstrap.gcd_import(name[level:], package, level)
tests\test_start.py:2: in
from aiogram_tests import MockedBot
.......virtualenvs\HodgepodgeBot-PgelrvFb\lib\site-packages\aiogram_tests_init
.py:1: in
from .requester import MockedBot
.......virtualenvs\HodgepodgeBot-PgelrvFb\lib\site-packages\aiogram_tests\requester.py:1: in
from aiogram.utils.helper import Helper
E ModuleNotFoundError: No module named 'aiogram.utils.helper'

aiogram.utils.helper file was deleted

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.