From 4e6756025d14ad8097f01e8a96c3b016be71aa07 Mon Sep 17 00:00:00 2001 From: Nikolay Tatarinov Date: Tue, 17 Feb 2026 20:05:49 +0300 Subject: [PATCH] Enhance database initialization and improve command handling - Added `__all__` declaration in `db/__init__.py` for better module export management. - Simplified command text formatting in `handlers/commands.py` for improved readability. - Refactored error handler function signature in `handlers/errors.py` for better code style. - Introduced comprehensive tests for API duties and Telegram authentication in new test files. --- db/__init__.py | 18 ++++++++++++++++++ handlers/commands.py | 4 +--- handlers/errors.py | 4 +++- {api => tests}/test_app.py | 2 +- {api => tests}/test_telegram_auth.py | 0 5 files changed, 23 insertions(+), 5 deletions(-) rename {api => tests}/test_app.py (98%) rename {api => tests}/test_telegram_auth.py (100%) diff --git a/db/__init__.py b/db/__init__.py index 618ec9e..26d70fd 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -5,6 +5,24 @@ from db.schemas import UserCreate, UserInDb, DutyCreate, DutyInDb, DutyWithUser from db.session import get_engine, get_session_factory, get_session from db.repository import get_or_create_user, get_duties, insert_duty +__all__ = [ + "Base", + "User", + "Duty", + "UserCreate", + "UserInDb", + "DutyCreate", + "DutyInDb", + "DutyWithUser", + "get_engine", + "get_session_factory", + "get_session", + "get_or_create_user", + "get_duties", + "insert_duty", + "init_db", +] + def init_db(database_url: str) -> None: """Create tables from metadata (Alembic migrations handle schema in production).""" diff --git a/handlers/commands.py b/handlers/commands.py index be687dd..b7dbd86 100644 --- a/handlers/commands.py +++ b/handlers/commands.py @@ -48,9 +48,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def help_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if update.message: await update.message.reply_text( - "Доступные команды:\n" - "/start — Начать\n" - "/help — Показать эту справку" + "Доступные команды:\n/start — Начать\n/help — Показать эту справку" ) diff --git a/handlers/errors.py b/handlers/errors.py index e70d98d..1307424 100644 --- a/handlers/errors.py +++ b/handlers/errors.py @@ -8,7 +8,9 @@ from telegram.ext import ContextTypes logger = logging.getLogger(__name__) -async def error_handler(update: Update | None, context: ContextTypes.DEFAULT_TYPE) -> None: +async def error_handler( + update: Update | None, context: ContextTypes.DEFAULT_TYPE +) -> None: logger.exception("Exception while handling an update") if isinstance(update, Update) and update.effective_message: await update.effective_message.reply_text("Произошла ошибка. Попробуйте позже.") diff --git a/api/test_app.py b/tests/test_app.py similarity index 98% rename from api/test_app.py rename to tests/test_app.py index 14d4caf..7bc2a22 100644 --- a/api/test_app.py +++ b/tests/test_app.py @@ -8,7 +8,7 @@ from fastapi.testclient import TestClient import config from api.app import app -from api.test_telegram_auth import _make_init_data +from tests.test_telegram_auth import _make_init_data @pytest.fixture diff --git a/api/test_telegram_auth.py b/tests/test_telegram_auth.py similarity index 100% rename from api/test_telegram_auth.py rename to tests/test_telegram_auth.py