Files
telegram-helper-bot/tests/conftest_message_repository.py
Andrey 1c6a37bc12 Enhance bot functionality and refactor database interactions
- Added `ca-certificates` installation to Dockerfile for improved network security.
- Updated health check command in Dockerfile to include better timeout handling.
- Refactored `run_helper.py` to implement proper signal handling and logging during shutdown.
- Transitioned database operations to an asynchronous model in `async_db.py`, improving performance and responsiveness.
- Updated database schema to support new foreign key relationships and optimized indexing for better query performance.
- Enhanced various bot handlers to utilize async database methods, improving overall efficiency and user experience.
- Removed obsolete database and fix scripts to streamline the project structure.
2025-09-02 18:22:02 +03:00

126 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
import tempfile
import os
from datetime import datetime
from database.repositories.message_repository import MessageRepository
from database.models import UserMessage
@pytest.fixture(scope="session")
def test_db_path():
"""Фикстура для пути к тестовой БД (сессионная область)."""
with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as f:
temp_path = f.name
yield temp_path
# Очистка после всех тестов
try:
os.unlink(temp_path)
except OSError:
pass
@pytest.fixture
def message_repository(test_db_path):
"""Фикстура для MessageRepository."""
return MessageRepository(test_db_path)
@pytest.fixture
def sample_messages():
"""Фикстура для набора тестовых сообщений."""
base_timestamp = int(datetime.now().timestamp())
return [
UserMessage(
message_text="Первое тестовое сообщение",
user_id=1001,
telegram_message_id=2001,
date=base_timestamp
),
UserMessage(
message_text="Второе тестовое сообщение",
user_id=1002,
telegram_message_id=2002,
date=base_timestamp + 1
),
UserMessage(
message_text="Третье тестовое сообщение",
user_id=1003,
telegram_message_id=2003,
date=base_timestamp + 2
)
]
@pytest.fixture
def message_without_date():
"""Фикстура для сообщения без даты."""
return UserMessage(
message_text="Сообщение без даты",
user_id=1004,
telegram_message_id=2004,
date=None
)
@pytest.fixture
def message_with_zero_date():
"""Фикстура для сообщения с нулевой датой."""
return UserMessage(
message_text="Сообщение с нулевой датой",
user_id=1005,
telegram_message_id=2005,
date=0
)
@pytest.fixture
def message_with_special_chars():
"""Фикстура для сообщения со специальными символами."""
return UserMessage(
message_text="Сообщение с 'кавычками', \"двойными кавычками\" и эмодзи 😊\nНовая строка",
user_id=1006,
telegram_message_id=2006,
date=int(datetime.now().timestamp())
)
@pytest.fixture
def long_message():
"""Фикстура для длинного сообщения."""
long_text = "Очень длинное сообщение " * 100 # ~2400 символов
return UserMessage(
message_text=long_text,
user_id=1007,
telegram_message_id=2007,
date=int(datetime.now().timestamp())
)
@pytest.fixture
def message_with_unicode():
"""Фикстура для сообщения с Unicode символами."""
return UserMessage(
message_text="Сообщение с Unicode: 你好世界 🌍 Привет мир",
user_id=1008,
telegram_message_id=2008,
date=int(datetime.now().timestamp())
)
@pytest.fixture
async def initialized_repository(message_repository):
"""Фикстура для инициализированного репозитория с созданными таблицами."""
await message_repository.create_tables()
return message_repository
@pytest.fixture
async def repository_with_data(initialized_repository, sample_messages):
"""Фикстура для репозитория с тестовыми данными."""
for message in sample_messages:
await initialized_repository.add_message(message)
return initialized_repository