All checks were successful
CI pipeline / Test & Code Quality (push) Successful in 34s
172 lines
6.4 KiB
Python
172 lines
6.4 KiB
Python
"""Тесты для BotSettingsRepository."""
|
||
|
||
from unittest.mock import AsyncMock, MagicMock, patch
|
||
|
||
import pytest
|
||
|
||
from database.repositories.bot_settings_repository import BotSettingsRepository
|
||
|
||
|
||
class TestBotSettingsRepository:
|
||
"""Тесты для репозитория настроек бота."""
|
||
|
||
@pytest.fixture
|
||
def repository(self):
|
||
"""Создает экземпляр репозитория с замоканным путем к БД."""
|
||
return BotSettingsRepository("test.db")
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_setting_returns_value(self, repository):
|
||
"""Тест получения настройки по ключу."""
|
||
with patch.object(
|
||
repository, "_execute_query_with_result", new_callable=AsyncMock
|
||
) as mock_query:
|
||
mock_query.return_value = [("true",)]
|
||
|
||
result = await repository.get_setting("auto_publish_enabled")
|
||
|
||
assert result == "true"
|
||
mock_query.assert_called_once()
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_setting_returns_none_when_not_found(self, repository):
|
||
"""Тест получения несуществующей настройки."""
|
||
with patch.object(
|
||
repository, "_execute_query_with_result", new_callable=AsyncMock
|
||
) as mock_query:
|
||
mock_query.return_value = []
|
||
|
||
result = await repository.get_setting("nonexistent_key")
|
||
|
||
assert result is None
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_set_setting(self, repository):
|
||
"""Тест установки настройки."""
|
||
with patch.object(
|
||
repository, "_execute_query", new_callable=AsyncMock
|
||
) as mock_query:
|
||
await repository.set_setting("auto_publish_enabled", "true")
|
||
|
||
mock_query.assert_called_once()
|
||
call_args = mock_query.call_args[0]
|
||
assert "auto_publish_enabled" in str(call_args)
|
||
assert "true" in str(call_args)
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_bool_setting_true(self, repository):
|
||
"""Тест получения булевой настройки со значением true."""
|
||
with patch.object(
|
||
repository, "get_setting", new_callable=AsyncMock
|
||
) as mock_get:
|
||
mock_get.return_value = "true"
|
||
|
||
result = await repository.get_bool_setting("auto_publish_enabled")
|
||
|
||
assert result is True
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_bool_setting_false(self, repository):
|
||
"""Тест получения булевой настройки со значением false."""
|
||
with patch.object(
|
||
repository, "get_setting", new_callable=AsyncMock
|
||
) as mock_get:
|
||
mock_get.return_value = "false"
|
||
|
||
result = await repository.get_bool_setting("auto_publish_enabled")
|
||
|
||
assert result is False
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_bool_setting_default(self, repository):
|
||
"""Тест получения булевой настройки с дефолтным значением."""
|
||
with patch.object(
|
||
repository, "get_setting", new_callable=AsyncMock
|
||
) as mock_get:
|
||
mock_get.return_value = None
|
||
|
||
result = await repository.get_bool_setting("auto_publish_enabled", True)
|
||
|
||
assert result is True
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_float_setting(self, repository):
|
||
"""Тест получения числовой настройки."""
|
||
with patch.object(
|
||
repository, "get_setting", new_callable=AsyncMock
|
||
) as mock_get:
|
||
mock_get.return_value = "0.8"
|
||
|
||
result = await repository.get_float_setting("auto_publish_threshold")
|
||
|
||
assert result == 0.8
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_float_setting_invalid_value(self, repository):
|
||
"""Тест получения числовой настройки с некорректным значением."""
|
||
with patch.object(
|
||
repository, "get_setting", new_callable=AsyncMock
|
||
) as mock_get:
|
||
mock_get.return_value = "invalid"
|
||
|
||
result = await repository.get_float_setting("auto_publish_threshold", 0.5)
|
||
|
||
assert result == 0.5
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_auto_moderation_settings(self, repository):
|
||
"""Тест получения всех настроек авто-модерации."""
|
||
with (
|
||
patch.object(
|
||
repository, "get_bool_setting", new_callable=AsyncMock
|
||
) as mock_bool,
|
||
patch.object(
|
||
repository, "get_float_setting", new_callable=AsyncMock
|
||
) as mock_float,
|
||
):
|
||
mock_bool.side_effect = [True, False]
|
||
mock_float.side_effect = [0.8, 0.4]
|
||
|
||
result = await repository.get_auto_moderation_settings()
|
||
|
||
assert result["auto_publish_enabled"] is True
|
||
assert result["auto_decline_enabled"] is False
|
||
assert result["auto_publish_threshold"] == 0.8
|
||
assert result["auto_decline_threshold"] == 0.4
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_toggle_auto_publish(self, repository):
|
||
"""Тест переключения авто-публикации."""
|
||
with (
|
||
patch.object(
|
||
repository, "get_bool_setting", new_callable=AsyncMock
|
||
) as mock_get,
|
||
patch.object(
|
||
repository, "set_bool_setting", new_callable=AsyncMock
|
||
) as mock_set,
|
||
):
|
||
mock_get.return_value = False
|
||
|
||
result = await repository.toggle_auto_publish()
|
||
|
||
assert result is True
|
||
mock_set.assert_called_once_with("auto_publish_enabled", True)
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_toggle_auto_decline(self, repository):
|
||
"""Тест переключения авто-отклонения."""
|
||
with (
|
||
patch.object(
|
||
repository, "get_bool_setting", new_callable=AsyncMock
|
||
) as mock_get,
|
||
patch.object(
|
||
repository, "set_bool_setting", new_callable=AsyncMock
|
||
) as mock_set,
|
||
):
|
||
mock_get.return_value = True
|
||
|
||
result = await repository.toggle_auto_decline()
|
||
|
||
assert result is False
|
||
mock_set.assert_called_once_with("auto_decline_enabled", False)
|