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

@@ -19,13 +19,13 @@ class TestPrivateHandlers:
def mock_db(self):
"""Mock database"""
db = Mock()
db.user_exists.return_value = False
db.add_new_user_in_db = Mock()
db.update_date_for_user = Mock()
db.update_info_about_stickers = Mock()
db.add_post_in_db = Mock()
db.add_new_message_in_db = Mock()
db.update_helper_message_in_db = Mock()
db.user_exists = AsyncMock(return_value=False)
db.add_user = AsyncMock()
db.update_user_date = AsyncMock()
db.update_stickers_info = AsyncMock()
db.add_post = AsyncMock()
db.add_message = AsyncMock()
db.update_helper_message = AsyncMock()
return db
@pytest.fixture
@@ -101,7 +101,8 @@ class TestPrivateHandlers:
# Mock the check_user_emoji function
with pytest.MonkeyPatch().context() as m:
m.setattr('helper_bot.handlers.private.private_handlers.check_user_emoji', lambda x: "😊")
mock_check_emoji = AsyncMock(return_value="😊")
m.setattr('helper_bot.handlers.private.private_handlers.check_user_emoji', mock_check_emoji)
# Test the handler
await handlers.handle_emoji_message(mock_message, mock_state)
@@ -121,7 +122,8 @@ class TestPrivateHandlers:
with pytest.MonkeyPatch().context() as m:
m.setattr('helper_bot.handlers.private.private_handlers.get_first_name', lambda x: "Test")
m.setattr('helper_bot.handlers.private.private_handlers.messages.get_message', lambda x, y: "Hello Test!")
m.setattr('helper_bot.handlers.private.private_handlers.get_reply_keyboard', lambda x, y: Mock())
mock_keyboard = AsyncMock(return_value=Mock())
m.setattr('helper_bot.handlers.private.private_handlers.get_reply_keyboard', mock_keyboard)
# Test the handler
await handlers.handle_start_message(mock_message, mock_state)
@@ -130,8 +132,8 @@ class TestPrivateHandlers:
mock_state.set_state.assert_called_once_with(FSM_STATES["START"])
# Verify user was ensured to exist
mock_db.add_new_user_in_db.assert_called_once()
mock_db.update_date_for_user.assert_called_once()
mock_db.add_user.assert_called_once()
mock_db.update_user_date.assert_called_once()
class TestBotSettings: