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:
@@ -7,10 +7,8 @@ from unittest.mock import AsyncMock, Mock, patch
|
||||
import pytest
|
||||
|
||||
from app.models.sound import Sound
|
||||
from app.models.sound_played import SoundPlayed
|
||||
from app.models.user import User
|
||||
from app.services.vlc_player import VLCPlayerService, get_vlc_player_service
|
||||
from app.utils.audio import get_sound_file_path
|
||||
|
||||
|
||||
class TestVLCPlayerService:
|
||||
@@ -79,16 +77,16 @@ class TestVLCPlayerService:
|
||||
def test_find_vlc_executable_found_by_path(self, mock_run):
|
||||
"""Test VLC executable detection when found by absolute path."""
|
||||
mock_run.return_value.returncode = 1 # which command fails
|
||||
|
||||
|
||||
# Mock Path to return True for the first absolute path
|
||||
with patch("app.services.vlc_player.Path") as mock_path:
|
||||
def path_side_effect(path_str):
|
||||
mock_instance = Mock()
|
||||
mock_instance.exists.return_value = str(path_str) == "/usr/bin/vlc"
|
||||
return mock_instance
|
||||
|
||||
|
||||
mock_path.side_effect = path_side_effect
|
||||
|
||||
|
||||
service = VLCPlayerService()
|
||||
assert service.vlc_executable == "/usr/bin/vlc"
|
||||
|
||||
@@ -100,10 +98,10 @@ class TestVLCPlayerService:
|
||||
mock_path_instance = Mock()
|
||||
mock_path_instance.exists.return_value = False
|
||||
mock_path.return_value = mock_path_instance
|
||||
|
||||
|
||||
# Mock which command as failing
|
||||
mock_run.return_value.returncode = 1
|
||||
|
||||
|
||||
service = VLCPlayerService()
|
||||
assert service.vlc_executable == "vlc"
|
||||
|
||||
@@ -111,26 +109,26 @@ class TestVLCPlayerService:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.vlc_player.asyncio.create_subprocess_exec")
|
||||
async def test_play_sound_success(
|
||||
self, mock_subprocess, vlc_service, sample_sound
|
||||
self, mock_subprocess, vlc_service, sample_sound,
|
||||
):
|
||||
"""Test successful sound playback."""
|
||||
# Mock subprocess
|
||||
mock_process = Mock()
|
||||
mock_process.pid = 12345
|
||||
mock_subprocess.return_value = mock_process
|
||||
|
||||
|
||||
# Mock the file path utility to avoid Path issues
|
||||
with patch("app.services.vlc_player.get_sound_file_path") as mock_get_path:
|
||||
mock_path = Mock()
|
||||
mock_path.exists.return_value = True
|
||||
mock_get_path.return_value = mock_path
|
||||
|
||||
|
||||
result = await vlc_service.play_sound(sample_sound)
|
||||
|
||||
|
||||
assert result is True
|
||||
mock_subprocess.assert_called_once()
|
||||
args = mock_subprocess.call_args
|
||||
|
||||
|
||||
# Check command arguments
|
||||
cmd_args = args[1] # keyword arguments
|
||||
assert "--play-and-exit" in args[0]
|
||||
@@ -144,7 +142,7 @@ class TestVLCPlayerService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_play_sound_file_not_found(
|
||||
self, vlc_service, sample_sound
|
||||
self, vlc_service, sample_sound,
|
||||
):
|
||||
"""Test sound playback when file doesn't exist."""
|
||||
# Mock the file path utility to return a non-existent path
|
||||
@@ -152,15 +150,15 @@ class TestVLCPlayerService:
|
||||
mock_path = Mock()
|
||||
mock_path.exists.return_value = False
|
||||
mock_get_path.return_value = mock_path
|
||||
|
||||
|
||||
result = await vlc_service.play_sound(sample_sound)
|
||||
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.vlc_player.asyncio.create_subprocess_exec")
|
||||
async def test_play_sound_subprocess_error(
|
||||
self, mock_subprocess, vlc_service, sample_sound
|
||||
self, mock_subprocess, vlc_service, sample_sound,
|
||||
):
|
||||
"""Test sound playback when subprocess fails."""
|
||||
# Mock the file path utility to return an existing path
|
||||
@@ -168,12 +166,12 @@ class TestVLCPlayerService:
|
||||
mock_path = Mock()
|
||||
mock_path.exists.return_value = True
|
||||
mock_get_path.return_value = mock_path
|
||||
|
||||
|
||||
# Mock subprocess exception
|
||||
mock_subprocess.side_effect = Exception("Subprocess failed")
|
||||
|
||||
|
||||
result = await vlc_service.play_sound(sample_sound)
|
||||
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -184,27 +182,27 @@ class TestVLCPlayerService:
|
||||
mock_find_process = Mock()
|
||||
mock_find_process.returncode = 0
|
||||
mock_find_process.communicate = AsyncMock(
|
||||
return_value=(b"12345\n67890\n", b"")
|
||||
return_value=(b"12345\n67890\n", b""),
|
||||
)
|
||||
|
||||
|
||||
# Mock pkill process (kill VLC processes)
|
||||
mock_kill_process = Mock()
|
||||
mock_kill_process.communicate = AsyncMock(return_value=(b"", b""))
|
||||
|
||||
|
||||
# Mock verify process (check remaining processes)
|
||||
mock_verify_process = Mock()
|
||||
mock_verify_process.returncode = 1 # No processes found
|
||||
mock_verify_process.communicate = AsyncMock(return_value=(b"", b""))
|
||||
|
||||
|
||||
# Set up subprocess mock to return different processes for each call
|
||||
mock_subprocess.side_effect = [
|
||||
mock_find_process,
|
||||
mock_kill_process,
|
||||
mock_verify_process,
|
||||
]
|
||||
|
||||
|
||||
result = await vlc_service.stop_all_vlc_instances()
|
||||
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["processes_found"] == 2
|
||||
assert result["processes_killed"] == 2
|
||||
@@ -214,18 +212,18 @@ class TestVLCPlayerService:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.vlc_player.asyncio.create_subprocess_exec")
|
||||
async def test_stop_all_vlc_instances_no_processes(
|
||||
self, mock_subprocess, vlc_service
|
||||
self, mock_subprocess, vlc_service,
|
||||
):
|
||||
"""Test stopping VLC instances when none are running."""
|
||||
# Mock pgrep process (no VLC processes found)
|
||||
mock_find_process = Mock()
|
||||
mock_find_process.returncode = 1 # No processes found
|
||||
mock_find_process.communicate = AsyncMock(return_value=(b"", b""))
|
||||
|
||||
|
||||
mock_subprocess.return_value = mock_find_process
|
||||
|
||||
|
||||
result = await vlc_service.stop_all_vlc_instances()
|
||||
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["processes_found"] == 0
|
||||
assert result["processes_killed"] == 0
|
||||
@@ -234,33 +232,33 @@ class TestVLCPlayerService:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.vlc_player.asyncio.create_subprocess_exec")
|
||||
async def test_stop_all_vlc_instances_partial_kill(
|
||||
self, mock_subprocess, vlc_service
|
||||
self, mock_subprocess, vlc_service,
|
||||
):
|
||||
"""Test stopping VLC instances when some processes remain."""
|
||||
# Mock pgrep process (find VLC processes)
|
||||
mock_find_process = Mock()
|
||||
mock_find_process.returncode = 0
|
||||
mock_find_process.communicate = AsyncMock(
|
||||
return_value=(b"12345\n67890\n11111\n", b"")
|
||||
return_value=(b"12345\n67890\n11111\n", b""),
|
||||
)
|
||||
|
||||
|
||||
# Mock pkill process (kill VLC processes)
|
||||
mock_kill_process = Mock()
|
||||
mock_kill_process.communicate = AsyncMock(return_value=(b"", b""))
|
||||
|
||||
|
||||
# Mock verify process (one process remains)
|
||||
mock_verify_process = Mock()
|
||||
mock_verify_process.returncode = 0
|
||||
mock_verify_process.communicate = AsyncMock(return_value=(b"11111\n", b""))
|
||||
|
||||
|
||||
mock_subprocess.side_effect = [
|
||||
mock_find_process,
|
||||
mock_kill_process,
|
||||
mock_verify_process,
|
||||
]
|
||||
|
||||
|
||||
result = await vlc_service.stop_all_vlc_instances()
|
||||
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["processes_found"] == 3
|
||||
assert result["processes_killed"] == 2
|
||||
@@ -272,9 +270,9 @@ class TestVLCPlayerService:
|
||||
"""Test stopping VLC instances when an error occurs."""
|
||||
# Mock subprocess exception
|
||||
mock_subprocess.side_effect = Exception("Command failed")
|
||||
|
||||
|
||||
result = await vlc_service.stop_all_vlc_instances()
|
||||
|
||||
|
||||
assert result["success"] is False
|
||||
assert result["processes_found"] == 0
|
||||
assert result["processes_killed"] == 0
|
||||
@@ -286,16 +284,16 @@ class TestVLCPlayerService:
|
||||
with patch("app.services.vlc_player.VLCPlayerService") as mock_service_class:
|
||||
mock_instance = Mock()
|
||||
mock_service_class.return_value = mock_instance
|
||||
|
||||
|
||||
# Clear the global instance
|
||||
import app.services.vlc_player
|
||||
app.services.vlc_player.vlc_player_service = None
|
||||
|
||||
|
||||
# First call should create new instance
|
||||
service1 = get_vlc_player_service()
|
||||
assert service1 == mock_instance
|
||||
mock_service_class.assert_called_once()
|
||||
|
||||
|
||||
# Second call should return same instance
|
||||
service2 = get_vlc_player_service()
|
||||
assert service2 == mock_instance
|
||||
@@ -306,22 +304,22 @@ class TestVLCPlayerService:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.vlc_player.asyncio.create_subprocess_exec")
|
||||
async def test_play_sound_with_play_count_tracking(
|
||||
self, mock_subprocess, vlc_service_with_db, sample_sound
|
||||
self, mock_subprocess, vlc_service_with_db, sample_sound,
|
||||
):
|
||||
"""Test sound playback with play count tracking."""
|
||||
# Mock subprocess
|
||||
mock_process = Mock()
|
||||
mock_process.pid = 12345
|
||||
mock_subprocess.return_value = mock_process
|
||||
|
||||
|
||||
# Mock session and repositories
|
||||
mock_session = AsyncMock()
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_session
|
||||
|
||||
|
||||
# Mock repositories
|
||||
mock_sound_repo = AsyncMock()
|
||||
mock_user_repo = AsyncMock()
|
||||
|
||||
|
||||
with patch("app.services.vlc_player.SoundRepository", return_value=mock_sound_repo):
|
||||
with patch("app.services.vlc_player.UserRepository", return_value=mock_user_repo):
|
||||
with patch("app.services.vlc_player.socket_manager") as mock_socket:
|
||||
@@ -331,7 +329,7 @@ class TestVLCPlayerService:
|
||||
mock_path = Mock()
|
||||
mock_path.exists.return_value = True
|
||||
mock_get_path.return_value = mock_path
|
||||
|
||||
|
||||
# Mock sound repository responses
|
||||
updated_sound = Sound(
|
||||
id=1,
|
||||
@@ -345,7 +343,7 @@ class TestVLCPlayerService:
|
||||
)
|
||||
mock_sound_repo.get_by_id.return_value = sample_sound
|
||||
mock_sound_repo.update.return_value = updated_sound
|
||||
|
||||
|
||||
# Mock admin user
|
||||
admin_user = User(
|
||||
id=1,
|
||||
@@ -354,20 +352,20 @@ class TestVLCPlayerService:
|
||||
role="admin",
|
||||
)
|
||||
mock_user_repo.get_by_id.return_value = admin_user
|
||||
|
||||
|
||||
# Mock socket broadcast
|
||||
mock_socket.broadcast_to_all = AsyncMock()
|
||||
|
||||
|
||||
result = await vlc_service_with_db.play_sound(sample_sound)
|
||||
|
||||
|
||||
# Wait a bit for the async task to complete
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
assert result is True
|
||||
|
||||
|
||||
# Verify subprocess was called
|
||||
mock_subprocess.assert_called_once()
|
||||
|
||||
|
||||
# Note: The async task runs in the background, so we can't easily
|
||||
# verify the database operations in this test without more complex
|
||||
# mocking or using a real async test framework setup
|
||||
@@ -378,10 +376,10 @@ class TestVLCPlayerService:
|
||||
# Mock session and repositories
|
||||
mock_session = AsyncMock()
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_session
|
||||
|
||||
|
||||
mock_sound_repo = AsyncMock()
|
||||
mock_user_repo = AsyncMock()
|
||||
|
||||
|
||||
# Create test sound and user
|
||||
test_sound = Sound(
|
||||
id=1,
|
||||
@@ -399,7 +397,7 @@ class TestVLCPlayerService:
|
||||
name="Admin User",
|
||||
role="admin",
|
||||
)
|
||||
|
||||
|
||||
with patch("app.services.vlc_player.SoundRepository", return_value=mock_sound_repo):
|
||||
with patch("app.services.vlc_player.UserRepository", return_value=mock_user_repo):
|
||||
with patch("app.services.vlc_player.socket_manager") as mock_socket:
|
||||
@@ -407,26 +405,26 @@ class TestVLCPlayerService:
|
||||
# Setup mocks
|
||||
mock_sound_repo.get_by_id.return_value = test_sound
|
||||
mock_user_repo.get_by_id.return_value = admin_user
|
||||
|
||||
|
||||
# Mock socket broadcast
|
||||
mock_socket.broadcast_to_all = AsyncMock()
|
||||
|
||||
|
||||
await vlc_service_with_db._record_play_count(1, "Test Sound")
|
||||
|
||||
|
||||
# Verify sound repository calls
|
||||
mock_sound_repo.get_by_id.assert_called_once_with(1)
|
||||
mock_sound_repo.update.assert_called_once_with(
|
||||
test_sound, {"play_count": 1}
|
||||
test_sound, {"play_count": 1},
|
||||
)
|
||||
|
||||
|
||||
# Verify user repository calls
|
||||
mock_user_repo.get_by_id.assert_called_once_with(1)
|
||||
|
||||
|
||||
# Verify session operations
|
||||
mock_session.add.assert_called_once()
|
||||
mock_session.commit.assert_called_once()
|
||||
mock_session.close.assert_called_once()
|
||||
|
||||
|
||||
# Verify socket broadcast
|
||||
mock_socket.broadcast_to_all.assert_called_once_with(
|
||||
"sound_played",
|
||||
@@ -451,10 +449,10 @@ class TestVLCPlayerService:
|
||||
# Mock session and repositories
|
||||
mock_session = AsyncMock()
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_session
|
||||
|
||||
|
||||
mock_sound_repo = AsyncMock()
|
||||
mock_user_repo = AsyncMock()
|
||||
|
||||
|
||||
# Create test sound and user
|
||||
test_sound = Sound(
|
||||
id=1,
|
||||
@@ -472,27 +470,27 @@ class TestVLCPlayerService:
|
||||
name="Admin User",
|
||||
role="admin",
|
||||
)
|
||||
|
||||
|
||||
with patch("app.services.vlc_player.SoundRepository", return_value=mock_sound_repo):
|
||||
with patch("app.services.vlc_player.UserRepository", return_value=mock_user_repo):
|
||||
with patch("app.services.vlc_player.socket_manager") as mock_socket:
|
||||
# Setup mocks
|
||||
mock_sound_repo.get_by_id.return_value = test_sound
|
||||
mock_user_repo.get_by_id.return_value = admin_user
|
||||
|
||||
|
||||
# Mock socket broadcast
|
||||
mock_socket.broadcast_to_all = AsyncMock()
|
||||
|
||||
|
||||
await vlc_service_with_db._record_play_count(1, "Test Sound")
|
||||
|
||||
|
||||
# Verify sound play count was updated
|
||||
mock_sound_repo.update.assert_called_once_with(
|
||||
test_sound, {"play_count": 6}
|
||||
test_sound, {"play_count": 6},
|
||||
)
|
||||
|
||||
|
||||
# Verify new SoundPlayed record was always added
|
||||
mock_session.add.assert_called_once()
|
||||
|
||||
|
||||
# Verify commit happened
|
||||
mock_session.commit.assert_called_once()
|
||||
|
||||
@@ -502,10 +500,10 @@ class TestVLCPlayerService:
|
||||
mock_file_path = Mock(spec=Path)
|
||||
mock_file_path.exists.return_value = False # File doesn't exist
|
||||
mock_path.return_value = mock_file_path
|
||||
|
||||
|
||||
# This should fail because file doesn't exist
|
||||
result = asyncio.run(vlc_service.play_sound(sample_sound))
|
||||
|
||||
|
||||
# Verify the utility was called and returned False
|
||||
mock_path.assert_called_once_with(sample_sound)
|
||||
assert result is False
|
||||
assert result is False
|
||||
|
||||
Reference in New Issue
Block a user