Refactor test files for improved readability and consistency

- Removed unnecessary blank lines and adjusted formatting in test files.
- Ensured consistent use of commas in function calls and assertions across various test cases.
- Updated import statements for better organization and clarity.
- Enhanced mock setups in tests for better isolation and reliability.
- Improved assertions to follow a consistent style for better readability.
This commit is contained in:
JSC
2025-07-31 21:37:04 +02:00
parent e69098d633
commit 8847131f24
42 changed files with 602 additions and 616 deletions

View File

@@ -8,7 +8,12 @@ from unittest.mock import patch
import pytest
from app.models.sound import Sound
from app.utils.audio import get_audio_duration, get_file_hash, get_file_size, get_sound_file_path
from app.utils.audio import (
get_audio_duration,
get_file_hash,
get_file_size,
get_sound_file_path,
)
class TestAudioUtils:
@@ -301,7 +306,7 @@ class TestAudioUtils:
type="SDB",
is_normalized=False,
)
result = get_sound_file_path(sound)
expected = Path("sounds/originals/soundboard/test.mp3")
assert result == expected
@@ -310,13 +315,13 @@ class TestAudioUtils:
"""Test getting sound file path for SDB type normalized file."""
sound = Sound(
id=1,
name="Test Sound",
name="Test Sound",
filename="original.mp3",
normalized_filename="normalized.mp3",
type="SDB",
is_normalized=True,
)
result = get_sound_file_path(sound)
expected = Path("sounds/normalized/soundboard/normalized.mp3")
assert result == expected
@@ -326,11 +331,11 @@ class TestAudioUtils:
sound = Sound(
id=2,
name="TTS Sound",
filename="tts_file.wav",
filename="tts_file.wav",
type="TTS",
is_normalized=False,
)
result = get_sound_file_path(sound)
expected = Path("sounds/originals/text_to_speech/tts_file.wav")
assert result == expected
@@ -342,10 +347,10 @@ class TestAudioUtils:
name="TTS Sound",
filename="original.wav",
normalized_filename="normalized.mp3",
type="TTS",
type="TTS",
is_normalized=True,
)
result = get_sound_file_path(sound)
expected = Path("sounds/normalized/text_to_speech/normalized.mp3")
assert result == expected
@@ -359,7 +364,7 @@ class TestAudioUtils:
type="EXT",
is_normalized=False,
)
result = get_sound_file_path(sound)
expected = Path("sounds/originals/extracted/extracted.mp3")
assert result == expected
@@ -370,11 +375,11 @@ class TestAudioUtils:
id=3,
name="Extracted Sound",
filename="original.mp3",
normalized_filename="normalized.mp3",
normalized_filename="normalized.mp3",
type="EXT",
is_normalized=True,
)
result = get_sound_file_path(sound)
expected = Path("sounds/normalized/extracted/normalized.mp3")
assert result == expected
@@ -388,7 +393,7 @@ class TestAudioUtils:
type="CUSTOM",
is_normalized=False,
)
result = get_sound_file_path(sound)
expected = Path("sounds/originals/custom/unknown.mp3")
assert result == expected
@@ -403,7 +408,7 @@ class TestAudioUtils:
type="SDB",
is_normalized=True, # True but no normalized_filename
)
result = get_sound_file_path(sound)
# Should fall back to original file
expected = Path("sounds/originals/soundboard/original.mp3")

View File

@@ -1,12 +1,16 @@
"""Tests for credit decorators."""
from unittest.mock import AsyncMock, Mock
from unittest.mock import AsyncMock
import pytest
from app.models.credit_action import CreditActionType
from app.services.credit import CreditService, InsufficientCreditsError
from app.utils.credit_decorators import CreditManager, requires_credits, validate_credits_only
from app.utils.credit_decorators import (
CreditManager,
requires_credits,
validate_credits_only,
)
class TestRequiresCreditsDecorator:
@@ -32,7 +36,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int, message: str) -> str:
return f"Success: {message}"
@@ -41,10 +45,10 @@ class TestRequiresCreditsDecorator:
assert result == "Success: test"
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, None
123, CreditActionType.VLC_PLAY_SOUND, None,
)
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, True, None
123, CreditActionType.VLC_PLAY_SOUND, True, None,
)
@pytest.mark.asyncio
@@ -58,7 +62,7 @@ class TestRequiresCreditsDecorator:
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id",
metadata_extractor=extract_metadata
metadata_extractor=extract_metadata,
)
async def test_action(user_id: int, sound_name: str) -> bool:
return True
@@ -66,10 +70,10 @@ class TestRequiresCreditsDecorator:
await test_action(user_id=123, sound_name="test.mp3")
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, {"sound_name": "test.mp3"}
123, CreditActionType.VLC_PLAY_SOUND, {"sound_name": "test.mp3"},
)
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, True, {"sound_name": "test.mp3"}
123, CreditActionType.VLC_PLAY_SOUND, True, {"sound_name": "test.mp3"},
)
@pytest.mark.asyncio
@@ -79,7 +83,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int) -> bool:
return False # Action fails
@@ -88,7 +92,7 @@ class TestRequiresCreditsDecorator:
assert result is False
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, False, None
123, CreditActionType.VLC_PLAY_SOUND, False, None,
)
@pytest.mark.asyncio
@@ -98,7 +102,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int) -> str:
raise ValueError("Test error")
@@ -107,7 +111,7 @@ class TestRequiresCreditsDecorator:
await test_action(user_id=123)
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, False, None
123, CreditActionType.VLC_PLAY_SOUND, False, None,
)
@pytest.mark.asyncio
@@ -118,7 +122,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int) -> str:
return "Should not execute"
@@ -136,7 +140,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int, message: str) -> str:
return message
@@ -145,7 +149,7 @@ class TestRequiresCreditsDecorator:
assert result == "test"
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, None
123, CreditActionType.VLC_PLAY_SOUND, None,
)
@pytest.mark.asyncio
@@ -155,7 +159,7 @@ class TestRequiresCreditsDecorator:
@requires_credits(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(other_param: str) -> str:
return other_param
@@ -186,7 +190,7 @@ class TestValidateCreditsOnlyDecorator:
@validate_credits_only(
CreditActionType.VLC_PLAY_SOUND,
credit_service_factory,
user_id_param="user_id"
user_id_param="user_id",
)
async def test_action(user_id: int, message: str) -> str:
return f"Validated: {message}"
@@ -195,7 +199,7 @@ class TestValidateCreditsOnlyDecorator:
assert result == "Validated: test"
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND
123, CreditActionType.VLC_PLAY_SOUND,
)
# Should not deduct credits, only validate
mock_credit_service.deduct_credits.assert_not_called()
@@ -219,15 +223,15 @@ class TestCreditManager:
mock_credit_service,
123,
CreditActionType.VLC_PLAY_SOUND,
{"test": "data"}
{"test": "data"},
) as manager:
manager.mark_success()
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, {"test": "data"}
123, CreditActionType.VLC_PLAY_SOUND, {"test": "data"},
)
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, True, {"test": "data"}
123, CreditActionType.VLC_PLAY_SOUND, True, {"test": "data"},
)
@pytest.mark.asyncio
@@ -236,13 +240,13 @@ class TestCreditManager:
async with CreditManager(
mock_credit_service,
123,
CreditActionType.VLC_PLAY_SOUND
CreditActionType.VLC_PLAY_SOUND,
):
# Don't mark as success - should be considered failed
pass
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, False, None
123, CreditActionType.VLC_PLAY_SOUND, False, None,
)
@pytest.mark.asyncio
@@ -252,12 +256,12 @@ class TestCreditManager:
async with CreditManager(
mock_credit_service,
123,
CreditActionType.VLC_PLAY_SOUND
CreditActionType.VLC_PLAY_SOUND,
):
raise ValueError("Test error")
mock_credit_service.deduct_credits.assert_called_once_with(
123, CreditActionType.VLC_PLAY_SOUND, False, None
123, CreditActionType.VLC_PLAY_SOUND, False, None,
)
@pytest.mark.asyncio
@@ -269,9 +273,9 @@ class TestCreditManager:
async with CreditManager(
mock_credit_service,
123,
CreditActionType.VLC_PLAY_SOUND
CreditActionType.VLC_PLAY_SOUND,
):
pass
# Should not call deduct_credits since validation failed
mock_credit_service.deduct_credits.assert_not_called()
mock_credit_service.deduct_credits.assert_not_called()