Skip to content

Commit 21fa158

Browse files
committed
test: Настройка отображения покрытия кода
1 parent 52ef4e0 commit 21fa158

File tree

7 files changed

+161
-1
lines changed

7 files changed

+161
-1
lines changed

.github/workflows/test_runner.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: run tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
postgres:
15+
image: postgres:13
16+
ports:
17+
- 5432:5432
18+
env:
19+
POSTGRES_USER: a-v-tor
20+
POSTGRES_PASSWORD: postgres
21+
POSTGRES_DB: test_bot_secretar
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v2
26+
27+
- name: Set up Python
28+
uses: actions/setup-python@v2
29+
with:
30+
python-version: "3.11"
31+
32+
- name: Install Poetry
33+
run: |
34+
pip install poetry
35+
36+
- name: Install dependencies
37+
run: |
38+
poetry install
39+
40+
- name: Wait for PostgreSQL to start
41+
run: |
42+
sudo apt-get update
43+
sudo apt-get install -y postgresql-client
44+
until pg_isready -h localhost -p 5432 -U a-v-tor; do
45+
sleep 1
46+
done
47+
48+
- name: Environment variables setup
49+
run: |
50+
echo "DATABASE_URL=postgresql+psycopg2://a-v-tor:postgres@localhost:5432/test_bot_secretar" >> $GITHUB_ENV
51+
52+
- name: Debug environment
53+
run: |
54+
printenv | grep DATABASE_URL
55+
56+
- name: Apply migrations
57+
env:
58+
DATABASE_URL: ${{ env.DATABASE_URL }}
59+
run: |
60+
poetry run alembic upgrade head
61+
62+
- name: Run tests
63+
run: |
64+
poetry run pytest --cov=./ --cov-report=xml
65+
66+
- name: Upload coverage to Codecov
67+
uses: codecov/codecov-action@v1
68+
with:
69+
token: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<h1 align="center">Личный бот - секретарь</h1>
2+
<a href="https://codecov.io/github/A-V-tor/Bot-secretar" >
3+
<img src="https://codecov.io/github/A-V-tor/Bot-secretar/graph/badge.svg?token=65PRUK4GYD"/>
4+
</a>
25
<br>
36
<div id="header" align="center">
47
<img src='https://media.giphy.com/media/wlR4kWTnwEyY8RwHKM/giphy.gif' width="100"/>

env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DB_USER=a-v-tor
88
DB_PASS=postgres
99
DB_HOST=localhost
1010
DB_PORT=5432
11-
DB_NAME=bot_secretar
11+
DB_NAME=test_bot_secretar
1212

1313
URL_ADMIN=https://localhost/login
1414

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ loguru = "^0.7.3"
3636
ipykernel = "^6.19.4"
3737
pre-commit = "^3.6.0"
3838
ruff = "^0.11.0"
39+
pytest-cov = "^6.1.1"
40+
pytest-asyncio = "^0.26.0"
41+
pytest-dotenv = "^0.5.2"
3942

4043
[build-system]
4144
requires = ["poetry-core"]

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
pythonpath = . src
3+
env_files = env.example

tests/conftest.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
import pytest
4+
from dotenv import load_dotenv
5+
from sqlalchemy import create_engine
6+
from sqlalchemy.orm import sessionmaker
7+
8+
from config import DevelopConfig
9+
from src.database.base import Base
10+
11+
12+
@pytest.fixture(scope='session', autouse=True)
13+
def load_env():
14+
load_dotenv(dotenv_path='env.example')
15+
16+
17+
@pytest.fixture(name='get_db', scope='session')
18+
def get_test_db():
19+
# SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://a-v-tor:postgres@localhost:5432/test_bot_secretar'
20+
# engine = create_engine(SQLALCHEMY_DATABASE_URI)
21+
22+
settings = DevelopConfig()
23+
engine = create_engine(settings.SQLALCHEMY_DATABASE_URI)
24+
25+
Base.metadata.drop_all(bind=engine)
26+
Base.metadata.create_all(bind=engine)
27+
session_factory = sessionmaker(
28+
engine,
29+
expire_on_commit=False,
30+
)
31+
yield session_factory

tests/test_bot_handlers.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import re
2+
from unittest.mock import AsyncMock, create_autospec
3+
4+
import pytest
5+
from aiogram import types
6+
7+
from src.telegram.handlers.base import start_command
8+
from src.telegram.keyboards.base_kb import start_kb
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_start_command_with_create_user(get_db):
13+
message_mock = create_autospec(types.Message)
14+
message_mock.text = '/start'
15+
16+
message_mock.chat = create_autospec(types.Chat, instance=True)
17+
message_mock.chat.id = 123456789
18+
message_mock.chat.username = 'test_username'
19+
message_mock.chat.first_name = 'first_name'
20+
message_mock.chat.last_name = 'last_name'
21+
22+
message_mock.reply = AsyncMock()
23+
24+
await start_command(message=message_mock)
25+
26+
message_mock.reply.assert_called()
27+
actual_msg = message_mock.reply.call_args[0][0]
28+
assert re.search(r'Вы добавлены в пользователи бота', actual_msg)
29+
30+
# Проверка других аргументов (reply_markup и parse_mode)
31+
actual_reply_markup = message_mock.reply.call_args[1]['reply_markup']
32+
assert actual_reply_markup == await start_kb()
33+
assert message_mock.reply.call_args[1]['parse_mode'] == 'HTML'
34+
35+
36+
@pytest.mark.asyncio
37+
async def test_start_command_with_existing_user(get_db):
38+
message_mock = create_autospec(types.Message)
39+
message_mock.text = '/start'
40+
41+
message_mock.chat = create_autospec(types.Chat, instance=True)
42+
message_mock.chat.id = 123456789
43+
message_mock.chat.username = 'test_username'
44+
message_mock.chat.first_name = 'first_name'
45+
message_mock.chat.last_name = 'last_name'
46+
47+
message_mock.reply = AsyncMock()
48+
49+
await start_command(message=message_mock)
50+
51+
message_mock.reply.assert_called_with('Главное меню', reply_markup=await start_kb(), parse_mode='HTML')

0 commit comments

Comments
 (0)