Skip to content

Commit

Permalink
version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dontsovcmc committed Apr 13, 2022
1 parent d61c9ea commit ed5d91d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 34 deletions.
17 changes: 16 additions & 1 deletion echobot/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@
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')
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
12 changes: 11 additions & 1 deletion echobot/pytest_echobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)\!'


Expand All @@ -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'
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions telegram_bot_unittest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
10 changes: 7 additions & 3 deletions telegram_bot_unittest/fixtures.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
6 changes: 4 additions & 2 deletions telegram_bot_unittest/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
)

Expand Down
63 changes: 37 additions & 26 deletions telegram_bot_unittest/user.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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']

0 comments on commit ed5d91d

Please sign in to comment.