Files
telegram-helper-bot/tests/test_async_db.py
Andrey 5f6882d348 Implement audio record management features in AsyncBotDB and AudioRepository
- Added methods to delete audio moderation records and retrieve all audio records in async_db.py.
- Enhanced AudioRepository with functionality to delete audio records by file name and retrieve all audio message records.
- Improved logging for audio record operations to enhance monitoring and debugging capabilities.
- Updated related handlers to ensure proper integration of new audio management features.
2025-09-05 01:31:50 +03:00

105 lines
5.0 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
from unittest.mock import Mock, AsyncMock, patch
from database.async_db import AsyncBotDB
class TestAsyncBotDB:
"""Тесты для AsyncBotDB"""
@pytest.fixture
def mock_factory(self):
"""Мок для RepositoryFactory"""
mock_factory = Mock()
mock_factory.audio = Mock()
mock_factory.audio.delete_audio_moderate_record = AsyncMock()
mock_factory.users = Mock()
mock_factory.users.logger = Mock()
return mock_factory
@pytest.fixture
def async_bot_db(self, mock_factory):
"""Экземпляр AsyncBotDB для тестов"""
with patch('database.async_db.RepositoryFactory') as mock_factory_class:
mock_factory_class.return_value = mock_factory
db = AsyncBotDB("test.db")
return db
@pytest.mark.asyncio
async def test_delete_audio_moderate_record(self, async_bot_db, mock_factory):
"""Тест метода delete_audio_moderate_record"""
message_id = 12345
await async_bot_db.delete_audio_moderate_record(message_id)
# Проверяем, что метод вызван в репозитории
mock_factory.audio.delete_audio_moderate_record.assert_called_once_with(message_id)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_with_different_message_id(self, async_bot_db, mock_factory):
"""Тест метода delete_audio_moderate_record с разными message_id"""
test_cases = [123, 456, 789, 99999]
for message_id in test_cases:
await async_bot_db.delete_audio_moderate_record(message_id)
mock_factory.audio.delete_audio_moderate_record.assert_called_with(message_id)
# Проверяем, что метод вызван для каждого message_id
assert mock_factory.audio.delete_audio_moderate_record.call_count == len(test_cases)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_exception_handling(self, async_bot_db, mock_factory):
"""Тест обработки исключений в delete_audio_moderate_record"""
message_id = 12345
mock_factory.audio.delete_audio_moderate_record.side_effect = Exception("Database error")
# Метод должен пробросить исключение
with pytest.raises(Exception, match="Database error"):
await async_bot_db.delete_audio_moderate_record(message_id)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_integration_with_other_methods(self, async_bot_db, mock_factory):
"""Тест интеграции delete_audio_moderate_record с другими методами"""
message_id = 12345
user_id = 67890
# Мокаем другие методы
mock_factory.audio.get_user_id_by_message_id_for_voice_bot = AsyncMock(return_value=user_id)
mock_factory.audio.set_user_id_and_message_id_for_voice_bot = AsyncMock(return_value=True)
# Тестируем последовательность операций
await async_bot_db.get_user_id_by_message_id_for_voice_bot(message_id)
await async_bot_db.set_user_id_and_message_id_for_voice_bot(message_id, user_id)
await async_bot_db.delete_audio_moderate_record(message_id)
# Проверяем, что все методы вызваны
mock_factory.audio.get_user_id_by_message_id_for_voice_bot.assert_called_once_with(message_id)
mock_factory.audio.set_user_id_and_message_id_for_voice_bot.assert_called_once_with(message_id, user_id)
mock_factory.audio.delete_audio_moderate_record.assert_called_once_with(message_id)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_zero_message_id(self, async_bot_db, mock_factory):
"""Тест delete_audio_moderate_record с message_id = 0"""
message_id = 0
await async_bot_db.delete_audio_moderate_record(message_id)
mock_factory.audio.delete_audio_moderate_record.assert_called_once_with(message_id)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_negative_message_id(self, async_bot_db, mock_factory):
"""Тест delete_audio_moderate_record с отрицательным message_id"""
message_id = -12345
await async_bot_db.delete_audio_moderate_record(message_id)
mock_factory.audio.delete_audio_moderate_record.assert_called_once_with(message_id)
@pytest.mark.asyncio
async def test_delete_audio_moderate_record_large_message_id(self, async_bot_db, mock_factory):
"""Тест delete_audio_moderate_record с большим message_id"""
message_id = 999999999
await async_bot_db.delete_audio_moderate_record(message_id)
mock_factory.audio.delete_audio_moderate_record.assert_called_once_with(message_id)