fix: Lint fixes of utils tests
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"""Tests for credit decorators."""
|
||||
# ruff: noqa: ARG001, ANN001, E501, PT012
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Never
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
@@ -17,7 +20,7 @@ class TestRequiresCreditsDecorator:
|
||||
"""Test requires_credits decorator."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_credit_service(self):
|
||||
def mock_credit_service(self) -> AsyncMock:
|
||||
"""Create a mock credit service."""
|
||||
service = AsyncMock(spec=CreditService)
|
||||
service.validate_and_reserve_credits = AsyncMock()
|
||||
@@ -25,12 +28,14 @@ class TestRequiresCreditsDecorator:
|
||||
return service
|
||||
|
||||
@pytest.fixture
|
||||
def credit_service_factory(self, mock_credit_service):
|
||||
def credit_service_factory(self, mock_credit_service: AsyncMock) -> Callable[[], AsyncMock]:
|
||||
"""Create a credit service factory."""
|
||||
return lambda: mock_credit_service
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_success(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_success(
|
||||
self, credit_service_factory: Callable[[], AsyncMock], mock_credit_service: AsyncMock,
|
||||
) -> None:
|
||||
"""Test decorator with successful action."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -52,10 +57,12 @@ class TestRequiresCreditsDecorator:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_with_metadata(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_with_metadata(
|
||||
self, credit_service_factory: Callable[[], AsyncMock], mock_credit_service: AsyncMock,
|
||||
) -> None:
|
||||
"""Test decorator with metadata extraction."""
|
||||
|
||||
def extract_metadata(user_id: int, sound_name: str) -> dict:
|
||||
def extract_metadata(user_id: int, sound_name: str) -> dict[str, str]:
|
||||
return {"sound_name": sound_name}
|
||||
|
||||
@requires_credits(
|
||||
@@ -77,7 +84,7 @@ class TestRequiresCreditsDecorator:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_failed_action(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_failed_action(self, credit_service_factory, mock_credit_service) -> None:
|
||||
"""Test decorator with failed action."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -96,7 +103,7 @@ class TestRequiresCreditsDecorator:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_exception_in_action(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_exception_in_action(self, credit_service_factory, mock_credit_service) -> None:
|
||||
"""Test decorator when action raises exception."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -105,7 +112,8 @@ class TestRequiresCreditsDecorator:
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int) -> str:
|
||||
raise ValueError("Test error")
|
||||
msg = "Test error"
|
||||
raise ValueError(msg)
|
||||
|
||||
with pytest.raises(ValueError, match="Test error"):
|
||||
await test_action(user_id=123)
|
||||
@@ -115,7 +123,7 @@ class TestRequiresCreditsDecorator:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_insufficient_credits(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_insufficient_credits(self, credit_service_factory, mock_credit_service) -> None:
|
||||
"""Test decorator with insufficient credits."""
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = InsufficientCreditsError(1, 0)
|
||||
|
||||
@@ -134,7 +142,7 @@ class TestRequiresCreditsDecorator:
|
||||
mock_credit_service.deduct_credits.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_user_id_in_args(self, credit_service_factory, mock_credit_service):
|
||||
async def test_decorator_user_id_in_args(self, credit_service_factory, mock_credit_service) -> None:
|
||||
"""Test decorator extracting user_id from positional args."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -153,7 +161,7 @@ class TestRequiresCreditsDecorator:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_missing_user_id(self, credit_service_factory):
|
||||
async def test_decorator_missing_user_id(self, credit_service_factory) -> None:
|
||||
"""Test decorator when user_id cannot be extracted."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -172,19 +180,19 @@ class TestValidateCreditsOnlyDecorator:
|
||||
"""Test validate_credits_only decorator."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_credit_service(self):
|
||||
def mock_credit_service(self) -> AsyncMock:
|
||||
"""Create a mock credit service."""
|
||||
service = AsyncMock(spec=CreditService)
|
||||
service.validate_and_reserve_credits = AsyncMock()
|
||||
return service
|
||||
|
||||
@pytest.fixture
|
||||
def credit_service_factory(self, mock_credit_service):
|
||||
def credit_service_factory(self, mock_credit_service: AsyncMock) -> Callable[[], AsyncMock]:
|
||||
"""Create a credit service factory."""
|
||||
return lambda: mock_credit_service
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_validate_only_decorator(self, credit_service_factory, mock_credit_service):
|
||||
async def test_validate_only_decorator(self, credit_service_factory, mock_credit_service) -> None:
|
||||
"""Test validate_credits_only decorator."""
|
||||
|
||||
@validate_credits_only(
|
||||
@@ -209,7 +217,7 @@ class TestCreditManager:
|
||||
"""Test CreditManager context manager."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_credit_service(self):
|
||||
def mock_credit_service(self) -> AsyncMock:
|
||||
"""Create a mock credit service."""
|
||||
service = AsyncMock(spec=CreditService)
|
||||
service.validate_and_reserve_credits = AsyncMock()
|
||||
@@ -217,7 +225,7 @@ class TestCreditManager:
|
||||
return service
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_credit_manager_success(self, mock_credit_service):
|
||||
async def test_credit_manager_success(self, mock_credit_service) -> None:
|
||||
"""Test CreditManager with successful operation."""
|
||||
async with CreditManager(
|
||||
mock_credit_service,
|
||||
@@ -235,7 +243,7 @@ class TestCreditManager:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_credit_manager_failure(self, mock_credit_service):
|
||||
async def test_credit_manager_failure(self, mock_credit_service) -> None:
|
||||
"""Test CreditManager with failed operation."""
|
||||
async with CreditManager(
|
||||
mock_credit_service,
|
||||
@@ -250,7 +258,7 @@ class TestCreditManager:
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_credit_manager_exception(self, mock_credit_service):
|
||||
async def test_credit_manager_exception(self, mock_credit_service) -> Never:
|
||||
"""Test CreditManager when exception occurs."""
|
||||
with pytest.raises(ValueError, match="Test error"):
|
||||
async with CreditManager(
|
||||
@@ -258,14 +266,15 @@ class TestCreditManager:
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
):
|
||||
raise ValueError("Test error")
|
||||
msg = "Test error"
|
||||
raise ValueError(msg)
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=False, metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_credit_manager_validation_failure(self, mock_credit_service):
|
||||
async def test_credit_manager_validation_failure(self, mock_credit_service) -> None:
|
||||
"""Test CreditManager when validation fails."""
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = InsufficientCreditsError(1, 0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user