From ed5d91d4ad52c442dc4d01d34bdecb6e5610882a Mon Sep 17 00:00:00 2001 From: Dontsov Evgeny Date: Wed, 13 Apr 2022 04:46:27 +0300 Subject: [PATCH] version 0.2 --- echobot/fixtures.py | 17 ++++++++- echobot/pytest_echobot.py | 12 +++++- setup.py | 9 ++++- telegram_bot_unittest/core.py | 5 +++ telegram_bot_unittest/fixtures.py | 10 +++-- telegram_bot_unittest/routes.py | 6 ++- telegram_bot_unittest/user.py | 63 ++++++++++++++++++------------- 7 files changed, 88 insertions(+), 34 deletions(-) diff --git a/echobot/fixtures.py b/echobot/fixtures.py index f08c7df..dec7967 100644 --- a/echobot/fixtures.py +++ b/echobot/fixtures.py @@ -2,7 +2,7 @@ import pytest from echobot.echobot import setup_bot from telegram_bot_unittest.routes import TELEGRAM_URL -from telegram_bot_unittest.user import BOT_TOKEN +from telegram_bot_unittest.user import BOT_TOKEN, CHAT_ID @pytest.fixture(scope='session') @@ -10,3 +10,18 @@ def bot(telegram_server): updater = setup_bot(BOT_TOKEN, TELEGRAM_URL) yield updater.bot updater.stop() + + +from telegram_bot_unittest.user import Tester, UserBase, ChatBase +from telegram_bot_unittest.core import core + +user2_id = CHAT_ID+1 + +u2 = UserBase(user2_id) +chat2 = ChatBase(user2_id) + + +@pytest.fixture(scope='session') +def user2() -> Tester: + user2 = Tester(core, u2, chat2) + return user2 diff --git a/echobot/pytest_echobot.py b/echobot/pytest_echobot.py index a412af4..3652bfa 100644 --- a/echobot/pytest_echobot.py +++ b/echobot/pytest_echobot.py @@ -6,6 +6,7 @@ def test_echobot_start(bot, user): message = user.get_message() + assert message assert message['text'] == 'Hi [FN LN](tg://user?id=1)\!' @@ -15,13 +16,22 @@ def test_echobot_help(bot, user): message = user.get_message() + assert message assert message['text'] == 'Help!' -def test_echobot_message(bot, user): +def test_echobot_message(bot, user, user2): user.send_message('testing message') message = user.get_message() + assert message + assert message['text'] == 'testing message' + + user2.send_message('testing message') + + message = user2.get_message() + + assert message assert message['text'] == 'testing message' diff --git a/setup.py b/setup.py index 201f454..b59b6c8 100644 --- a/setup.py +++ b/setup.py @@ -2,10 +2,17 @@ from setuptools import setup VERSION_MAJOR = 0 -VERSION_MINOR = 1 +VERSION_MINOR = 2 ver = '%d.%d' % (VERSION_MAJOR, VERSION_MINOR) +backlog = """ + +0.2 - 22.04.13 - add multiple users + +0.1 - 22.04.12 - init version +""" + if __name__ == '__main__': """ Create Packet: diff --git a/telegram_bot_unittest/core.py b/telegram_bot_unittest/core.py index 4b0c517..4899825 100644 --- a/telegram_bot_unittest/core.py +++ b/telegram_bot_unittest/core.py @@ -62,6 +62,7 @@ def user_send_command(self, bot_id: int, user_from, chat, command) -> None: }] } + self.init_queue(bot_id) self.income[bot_id].put(message) def bot_send(self, bot_from, chat, text) -> Dict: @@ -81,12 +82,16 @@ def get_updates(self, chat_id: int, timeout: float = 2.0) -> List[Dict]: ret = [] try: + if chat_id not in self.income: + self.init_queue(chat_id) + message = self.income[chat_id].get(timeout=timeout) self._update_counter += 1 ret = [{'update_id': self._update_counter, 'message': message }] + except Empty: pass diff --git a/telegram_bot_unittest/fixtures.py b/telegram_bot_unittest/fixtures.py index 6e7562a..9b4101b 100644 --- a/telegram_bot_unittest/fixtures.py +++ b/telegram_bot_unittest/fixtures.py @@ -1,13 +1,17 @@ import pytest from .routes import start_server, shutdown_server -from .user import Client +from .user import Tester, UserBase, ChatBase from .core import core +u = UserBase() +chat = ChatBase() + + @pytest.fixture(scope='session') -def user() -> Client: - user = Client(core) +def user() -> Tester: + user = Tester(core, u, chat) return user diff --git a/telegram_bot_unittest/routes.py b/telegram_bot_unittest/routes.py index b29a781..68fe374 100644 --- a/telegram_bot_unittest/routes.py +++ b/telegram_bot_unittest/routes.py @@ -6,7 +6,7 @@ from flask import json from .core import result_ok, core -from .user import virtual_bot, chat +from .user import virtual_bot logger = logging.getLogger(__name__) @@ -66,8 +66,10 @@ def sendMessage(token: str): chat_id = int(data['chat_id']) text = data['text'] + chat = {'id': chat_id, 'type': 'private'} # simple chat structure + ret = core.bot_send(bot_from=virtual_bot.to_dict(), - chat=chat.to_dict(), + chat=chat, text=text ) diff --git a/telegram_bot_unittest/user.py b/telegram_bot_unittest/user.py index fb0589e..9e466f9 100644 --- a/telegram_bot_unittest/user.py +++ b/telegram_bot_unittest/user.py @@ -1,18 +1,38 @@ from typing import Dict from telegram import User, Chat -CHAT_ID = 1 BOT_ID = 5000000000 + BOT_TOKEN = f'5000000000:BBFJVn-zqLnqQGv_Vrg75aJ5rqppy410rm0' +CHAT_ID = 1 + + +class UserBase(User): + + def __init__(self, id: int = CHAT_ID): + super().__init__( + id, # id + 'FN', # first_name + False, # is_bot + 'LN', # last_name + 'user1', # username + 'ru' # language_code + ) + + +class ChatBase(Chat): + + def __init__(self, id: int = CHAT_ID): + super().__init__( + id, + 'private', # type + None, # title + 'user1', # username + 'FN', # first_name + 'LN', # last_name + ) -user = User( - CHAT_ID, # id - 'FN', # first_name - False, # is_bot - 'LN', # last_name - 'user1', # username - 'ru') # language_code virtual_bot = User( BOT_ID, @@ -26,40 +46,31 @@ False # supports_inline_queries ) -chat = Chat( - CHAT_ID, - 'private', # type - None, # title - 'user1', # username - 'FN', # first_name - 'LN', # last_name -) - -class Client: +class Tester: - def __init__(self, core): - self.chat_id = CHAT_ID + def __init__(self, core, user, chat): self.core = core - self.core.init_queue(self.chat_id) + self.user = user + self.chat = chat def send_message(self, text: str) -> None: self.core.user_send(virtual_bot.id, - user_from=user.to_dict(), - chat=chat.to_dict(), + user_from=self.user.to_dict(), + chat=self.chat.to_dict(), text=text ) def send_command(self, command: str) -> None: self.core.user_send_command(virtual_bot.id, - user_from=user.to_dict(), - chat=chat.to_dict(), + user_from=self.user.to_dict(), + chat=self.chat.to_dict(), command=command ) def get_message(self, timeout=2.0) -> Dict: - messages = self.core.get_updates(self.chat_id, timeout) + messages = self.core.get_updates(self.user.id, timeout) if messages: return messages[0]['message']