Refactor metrics handling and improve logging

- Removed the MetricsManager initialization from `run_helper.py` to avoid duplication, as metrics are now handled in `main.py`.
- Updated logging levels in `server_prometheus.py` and `metrics_middleware.py` to use debug instead of info for less critical messages.
- Added metrics configuration to `BaseDependencyFactory` for better management of metrics settings.
- Deleted the obsolete `metrics_exporter.py` file to streamline the codebase.
- Updated various tests to reflect changes in the metrics handling and ensure proper functionality.
This commit is contained in:
2025-09-03 00:33:20 +03:00
parent 6fcecff97c
commit c8c7d50cbb
19 changed files with 402 additions and 605 deletions

View File

@@ -39,70 +39,83 @@ class TestVoiceUtils:
message.chat.id = 456
return message
def test_get_last_message_text(self, mock_bot_db):
@pytest.mark.asyncio
async def test_get_last_message_text(self, mock_bot_db):
"""Тест получения последнего сообщения"""
mock_bot_db.last_date_audio.return_value = "2025-01-01 12:00:00"
# Возвращаем UNIX timestamp
from unittest.mock import AsyncMock
mock_bot_db.last_date_audio = AsyncMock(return_value=1641034800) # 2022-01-01 12:00:00
result = get_last_message_text(mock_bot_db)
result = await get_last_message_text(mock_bot_db)
assert result is not None
assert "минут" in result or "часа" in result or "дня" in result
assert "минут" in result or "часа" in result or "дня" in result or "день" in result or "дней" in result
mock_bot_db.last_date_audio.assert_called_once()
def test_validate_voice_message_valid(self):
@pytest.mark.asyncio
async def test_validate_voice_message_valid(self):
"""Тест валидации голосового сообщения"""
mock_message = Mock()
mock_message.content_type = 'voice'
mock_message.voice = Mock()
result = validate_voice_message(mock_message)
result = await validate_voice_message(mock_message)
assert result is True
def test_validate_voice_message_invalid(self):
@pytest.mark.asyncio
async def test_validate_voice_message_invalid(self):
"""Тест валидации невалидного сообщения"""
mock_message = Mock()
mock_message.voice = None
result = validate_voice_message(mock_message)
result = await validate_voice_message(mock_message)
assert result is False
def test_get_user_emoji_safe_with_emoji(self, mock_bot_db):
@pytest.mark.asyncio
async def test_get_user_emoji_safe_with_emoji(self, mock_bot_db):
"""Тест безопасного получения эмодзи пользователя когда эмодзи есть"""
mock_bot_db.check_emoji_for_user.return_value = "😊"
from unittest.mock import AsyncMock
mock_bot_db.get_user_emoji = AsyncMock(return_value="😊")
result = get_user_emoji_safe(mock_bot_db, 123)
result = await get_user_emoji_safe(mock_bot_db, 123)
assert result == "😊"
mock_bot_db.check_emoji_for_user.assert_called_once_with(123)
mock_bot_db.get_user_emoji.assert_called_once_with(123)
def test_get_user_emoji_safe_without_emoji(self, mock_bot_db):
@pytest.mark.asyncio
async def test_get_user_emoji_safe_without_emoji(self, mock_bot_db):
"""Тест безопасного получения эмодзи пользователя когда эмодзи нет"""
mock_bot_db.check_emoji_for_user.return_value = None
from unittest.mock import AsyncMock
mock_bot_db.get_user_emoji = AsyncMock(return_value=None)
result = get_user_emoji_safe(mock_bot_db, 123)
result = await get_user_emoji_safe(mock_bot_db, 123)
assert result == "😊"
mock_bot_db.check_emoji_for_user.assert_called_once_with(123)
mock_bot_db.get_user_emoji.assert_called_once_with(123)
def test_get_user_emoji_safe_with_empty_emoji(self, mock_bot_db):
@pytest.mark.asyncio
async def test_get_user_emoji_safe_with_empty_emoji(self, mock_bot_db):
"""Тест безопасного получения эмодзи пользователя с пустым эмодзи"""
mock_bot_db.check_emoji_for_user.return_value = ""
from unittest.mock import AsyncMock
mock_bot_db.get_user_emoji = AsyncMock(return_value="")
result = get_user_emoji_safe(mock_bot_db, 123)
result = await get_user_emoji_safe(mock_bot_db, 123)
assert result == "😊"
mock_bot_db.check_emoji_for_user.assert_called_once_with(123)
mock_bot_db.get_user_emoji.assert_called_once_with(123)
def test_get_user_emoji_safe_with_error(self, mock_bot_db):
@pytest.mark.asyncio
async def test_get_user_emoji_safe_with_error(self, mock_bot_db):
"""Тест безопасного получения эмодзи пользователя при ошибке"""
mock_bot_db.check_emoji_for_user.return_value = "Ошибка"
from unittest.mock import AsyncMock
mock_bot_db.get_user_emoji = AsyncMock(return_value="Ошибка")
result = get_user_emoji_safe(mock_bot_db, 123)
result = await get_user_emoji_safe(mock_bot_db, 123)
assert result == "Ошибка"
mock_bot_db.check_emoji_for_user.assert_called_once_with(123)
mock_bot_db.get_user_emoji.assert_called_once_with(123)
def test_format_time_ago_minutes(self):
"""Тест форматирования времени в минутах"""