try github action to test selenoid

This commit is contained in:
Dmitry Afanasyev 2022-08-28 20:57:30 +03:00
parent 0c326fd12d
commit 78fd6ad503
6 changed files with 121 additions and 30 deletions

18
.github/workflows/test-selenoid.yml vendored Normal file
View File

@ -0,0 +1,18 @@
name: Test-selenoid
on:
pull_request:
push:
branches:
- master
jobs:
test-selenoid:
name: Run test suite
runs-on: ubuntu-latest
env:
LOCALTEST: 1
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
run: LOCALTEST=1 docker-compose run bot python -m pytest tests/bot/test_bot.py::test_selenoid_text -vv

View File

@ -10,7 +10,7 @@ app-up:
app-down: app-down:
docker-compose down -v docker-compose down -v
app-clean: app-cleanup:
docker-compose down -v && docker-clean run docker-compose down -v && docker-clean run
# standard commands to run on every commit # standard commands to run on every commit

View File

@ -1,4 +1,5 @@
# MosGotTrans bot # MosGotTrans bot
Бот для получения расписания конкретных автобусов для конкретных остановок
## Install & Update ## Install & Update
@ -22,6 +23,9 @@ killall firefox
killall python killall python
``` ```
## Tests
docker-compose run bot python -m pytest tests/bot/test_bot.py::test_selenoid_text
## Help article ## Help article
[Пишем асинхронного Телеграм-бота](https://habr.com/ru/company/kts/blog/598575/) [Пишем асинхронного Телеграм-бота](https://habr.com/ru/company/kts/blog/598575/)

View File

@ -21,8 +21,6 @@ services:
build: build:
context: . context: .
dockerfile: ./deploy/Dockerfile.selenoid dockerfile: ./deploy/Dockerfile.selenoid
args:
USER: web
restart: unless-stopped restart: unless-stopped
environment: environment:
- SESSION_TIMED_OUT=30s - SESSION_TIMED_OUT=30s
@ -45,8 +43,12 @@ services:
context: . context: .
dockerfile: ./deploy/Dockerfile.bot dockerfile: ./deploy/Dockerfile.bot
args: args:
USER: web USER: root
restart: unless-stopped restart: unless-stopped
environment:
LOCALTEST: ${LOCALTEST}
depends_on:
- selenoid
volumes: volumes:
- /etc/localtime:/etc/localtime:ro - /etc/localtime:/etc/localtime:ro
networks: networks:
@ -57,7 +59,7 @@ services:
command: bash start-bot.sh command: bash start-bot.sh
telebot-caddy: caddy:
image: "caddy:2.5.2" image: "caddy:2.5.2"
container_name: transport_bot_caddy container_name: transport_bot_caddy
hostname: transport_bot_caddy hostname: transport_bot_caddy

View File

View File

@ -1,4 +1,6 @@
import os
import time import time
from unittest import mock
import pytest import pytest
from aiogram import Bot, Dispatcher, types from aiogram import Bot, Dispatcher, types
@ -27,43 +29,108 @@ async def test_command1(bot: Bot) -> None:
TransportBot.dispatcher.bot = bot TransportBot.dispatcher.bot = bot
handlers = TransportBot.dispatcher.message_handlers.handlers handlers = TransportBot.dispatcher.message_handlers.handlers
commands = []
for handler in handlers: for handler in handlers:
handl = list( handl = list(
filter(lambda obj: isinstance(obj.filter, Command), handler.filters) filter(lambda obj: isinstance(obj.filter, Command), handler.filters)
) )
if handl: if handl:
command = handl[0].filter.commands[0] commands.append(handl[0].filter.commands[0])
assert command assert commands == ['chatid']
async def test_update(dispatcher_fixture: Dispatcher, bot: Bot) -> None: async def test_update(dispatcher_fixture: Dispatcher) -> None:
data = { data = {
"update_id": 957250703, 'update_id': 957250703,
"message": { 'message': {
"message_id": 417070387, 'message_id': 417070387,
"from": { 'from': {
"id": 417070387, 'id': 417070387,
"is_bot": False, 'is_bot': False,
"first_name": "Dmitry", 'first_name': 'Dmitry',
"last_name": "Afanasyev", 'last_name': 'Afanasyev',
"username": "Balshtg", 'username': 'Balshtg',
"language_code": "en", 'language_code': 'en',
}, },
"chat": { 'chat': {
"id": 417070387, 'id': 417070387,
"first_name": "Dmitry", 'first_name': 'Dmitry',
"last_name": "Afanasyev", 'last_name': 'Afanasyev',
"username": "Balshtg", 'username': 'Balshtg',
"type": "private", 'type': 'private',
}, },
"date": time.time(), 'date': time.time(),
"text": "/chatid", 'text': '/chatid',
"entities": [{"type": "bot_command", "offset": 0, "length": 7}], 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 7}],
}, },
} }
TransportBot.bot = dispatcher_fixture.bot
async with FakeTelegram(message_data=data): async with FakeTelegram(message_data=data):
update = Update(**data) update = Update(**data)
dispatcher_fixture.message_handler() result = await TransportBot.echo(update.message)
await dispatcher_fixture.process_update(update) assert result == types.Message(**data)
assert True
@pytest.mark.skipif(
bool(os.environ.get("LOCALTEST", False)) is False,
reason="Schemathesis test will be skipped if environment var SCHEMATHESIS=1 is not set",
)
async def test_selenoid_text(dispatcher_fixture: Dispatcher) -> None:
data = {
'id': '1791303673263594560',
'from': {
'id': 417070387,
'is_bot': False,
'first_name': 'Dmitry',
'last_name': 'Afanasyev',
'username': 'Balshtg',
'language_code': 'en',
},
'message': {
'message_id': 1316,
'from': {
'id': 5494499556,
'is_bot': False,
'first_name': 'balshbot_transport',
'username': 'balshbot_transport_bot',
},
'chat': {
'id': 417070387,
'first_name': 'Dmitry',
'last_name': 'Afanasyev',
'username': 'Balshtg',
'type': 'private',
},
'date': 1661692626,
'text': 'Остановка Б. Академическая ул, д. 15\n\nАвтобус 300 - прибывает\nАвтобус Т19 - 7 мин',
'reply_markup': {
'inline_keyboard': [
[
{
'text': 'Дом -> Офис',
'callback_data': 'station:home->office',
},
{
'text': 'Офис -> Дом',
'callback_data': 'station:office->home',
},
]
]
},
},
'chat_instance': '-6044557427944557947',
'data': 'station:home->office',
}
TransportBot.bot = dispatcher_fixture.bot
# @mock.patch('app.core.bot.TransportBot.bot.send_message')
with mock.patch(
'app.core.bot.TransportBot.bot.send_message',
return_value=data['message']['chat'], # type: ignore
):
async with FakeTelegram(message_data=data):
call_back = types.CallbackQuery(**data)
result = await TransportBot.home_office(query=call_back, callback_data={})
assert result == data['message']['chat'] # type: ignore