Refactor test cases for improved readability and consistency
- Adjusted function signatures in various test files to enhance clarity by aligning parameters. - Updated patching syntax for better readability across test cases. - Improved formatting and spacing in test assertions and mock setups. - Ensured consistent use of async/await patterns in async test functions. - Enhanced comments for better understanding of test intentions.
This commit is contained in:
@@ -35,10 +35,10 @@ class TestSoundEndpoints:
|
||||
|
||||
with (
|
||||
patch(
|
||||
"app.services.extraction.ExtractionService.create_extraction"
|
||||
"app.services.extraction.ExtractionService.create_extraction",
|
||||
) as mock_create,
|
||||
patch(
|
||||
"app.services.extraction_processor.extraction_processor.queue_extraction"
|
||||
"app.services.extraction_processor.extraction_processor.queue_extraction",
|
||||
) as mock_queue,
|
||||
):
|
||||
mock_create.return_value = mock_extraction_info
|
||||
@@ -53,7 +53,10 @@ class TestSoundEndpoints:
|
||||
data = response.json()
|
||||
assert data["message"] == "Extraction queued successfully"
|
||||
assert data["extraction"]["id"] == 1
|
||||
assert data["extraction"]["url"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
||||
assert (
|
||||
data["extraction"]["url"]
|
||||
== "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_extraction_unauthenticated(self, client: AsyncClient) -> None:
|
||||
@@ -75,7 +78,7 @@ class TestSoundEndpoints:
|
||||
) -> None:
|
||||
"""Test extraction creation with invalid URL."""
|
||||
with patch(
|
||||
"app.services.extraction.ExtractionService.create_extraction"
|
||||
"app.services.extraction.ExtractionService.create_extraction",
|
||||
) as mock_create:
|
||||
mock_create.side_effect = ValueError("Invalid URL")
|
||||
|
||||
@@ -107,7 +110,7 @@ class TestSoundEndpoints:
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.services.extraction.ExtractionService.get_extraction_by_id"
|
||||
"app.services.extraction.ExtractionService.get_extraction_by_id",
|
||||
) as mock_get:
|
||||
mock_get.return_value = mock_extraction_info
|
||||
|
||||
@@ -128,7 +131,7 @@ class TestSoundEndpoints:
|
||||
) -> None:
|
||||
"""Test getting non-existent extraction."""
|
||||
with patch(
|
||||
"app.services.extraction.ExtractionService.get_extraction_by_id"
|
||||
"app.services.extraction.ExtractionService.get_extraction_by_id",
|
||||
) as mock_get:
|
||||
mock_get.return_value = None
|
||||
|
||||
@@ -169,7 +172,7 @@ class TestSoundEndpoints:
|
||||
]
|
||||
|
||||
with patch(
|
||||
"app.services.extraction.ExtractionService.get_user_extractions"
|
||||
"app.services.extraction.ExtractionService.get_user_extractions",
|
||||
) as mock_get:
|
||||
mock_get.return_value = mock_extractions
|
||||
|
||||
@@ -202,7 +205,9 @@ class TestSoundEndpoints:
|
||||
|
||||
with (
|
||||
patch("app.repositories.sound.SoundRepository.get_by_id") as mock_get_sound,
|
||||
patch("app.services.credit.CreditService.validate_and_reserve_credits") as mock_validate,
|
||||
patch(
|
||||
"app.services.credit.CreditService.validate_and_reserve_credits",
|
||||
) as mock_validate,
|
||||
patch("app.services.vlc_player.VLCPlayerService.play_sound") as mock_play,
|
||||
patch("app.services.credit.CreditService.deduct_credits") as mock_deduct,
|
||||
):
|
||||
@@ -227,7 +232,9 @@ class TestSoundEndpoints:
|
||||
authenticated_user: User,
|
||||
) -> None:
|
||||
"""Test playing non-existent sound with VLC."""
|
||||
with patch("app.repositories.sound.SoundRepository.get_by_id") as mock_get_sound:
|
||||
with patch(
|
||||
"app.repositories.sound.SoundRepository.get_by_id",
|
||||
) as mock_get_sound:
|
||||
mock_get_sound.return_value = None
|
||||
|
||||
response = await authenticated_client.post("/api/v1/sounds/play/999")
|
||||
@@ -259,11 +266,14 @@ class TestSoundEndpoints:
|
||||
|
||||
with (
|
||||
patch("app.repositories.sound.SoundRepository.get_by_id") as mock_get_sound,
|
||||
patch("app.services.credit.CreditService.validate_and_reserve_credits") as mock_validate,
|
||||
patch(
|
||||
"app.services.credit.CreditService.validate_and_reserve_credits",
|
||||
) as mock_validate,
|
||||
):
|
||||
mock_get_sound.return_value = mock_sound
|
||||
mock_validate.side_effect = InsufficientCreditsError(
|
||||
required=1, available=0
|
||||
required=1,
|
||||
available=0,
|
||||
)
|
||||
|
||||
response = await authenticated_client.post("/api/v1/sounds/play/1")
|
||||
@@ -286,7 +296,7 @@ class TestSoundEndpoints:
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.services.vlc_player.VLCPlayerService.stop_all_vlc_instances"
|
||||
"app.services.vlc_player.VLCPlayerService.stop_all_vlc_instances",
|
||||
) as mock_stop:
|
||||
mock_stop.return_value = mock_result
|
||||
|
||||
@@ -295,4 +305,4 @@ class TestSoundEndpoints:
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["message"] == "All VLC instances stopped"
|
||||
assert data["stopped_count"] == 3
|
||||
assert data["stopped_count"] == 3
|
||||
|
||||
Reference in New Issue
Block a user