feat: Add endpoint to retrieve sounds with optional type filtering and implement corresponding repository method
Some checks failed
Backend CI / lint (push) Successful in 9m41s
Backend CI / test (push) Failing after 1m39s

This commit is contained in:
JSC
2025-08-01 22:03:09 +02:00
parent d2d0240fdb
commit 4bbae4c5d4
3 changed files with 127 additions and 2 deletions

View File

@@ -306,3 +306,94 @@ class TestSoundEndpoints:
data = response.json()
assert data["message"] == "All VLC instances stopped"
assert data["stopped_count"] == 3
@pytest.mark.asyncio
async def test_get_sounds_unauthenticated(self, client: AsyncClient) -> None:
"""Test getting sounds without authentication."""
response = await client.get("/api/v1/sounds/")
assert response.status_code == 401
data = response.json()
assert "Could not validate credentials" in data["detail"]
@pytest.mark.asyncio
async def test_get_sounds_authenticated(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
) -> None:
"""Test getting sounds with authentication."""
from app.models.sound import Sound
with patch("app.repositories.sound.SoundRepository.get_by_types") as mock_get:
# Create mock sounds with all required fields
mock_sound_1 = Sound(
id=1,
name="Test Sound 1",
type="SDB",
filename="test1.mp3",
duration=5000,
size=1024,
hash="test_hash_1",
play_count=0,
is_normalized=False,
is_music=False,
is_deletable=True,
)
mock_sound_2 = Sound(
id=2,
name="Test Sound 2",
type="EXT",
filename="test2.mp3",
duration=7000,
size=2048,
hash="test_hash_2",
play_count=5,
is_normalized=False,
is_music=False,
is_deletable=True,
)
mock_get.return_value = [mock_sound_1, mock_sound_2]
response = await authenticated_client.get("/api/v1/sounds/")
assert response.status_code == 200
data = response.json()
assert "sounds" in data
assert len(data["sounds"]) == 2
@pytest.mark.asyncio
async def test_get_sounds_with_type_filter_authenticated(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
) -> None:
"""Test getting sounds with type filtering."""
from app.models.sound import Sound
with patch("app.repositories.sound.SoundRepository.get_by_types") as mock_get:
# Create mock sound with all required fields
mock_sound = Sound(
id=1,
name="Test Sound 1",
type="SDB",
filename="test1.mp3",
duration=5000,
size=1024,
hash="test_hash_1",
play_count=0,
is_normalized=False,
is_music=False,
is_deletable=True,
)
mock_get.return_value = [mock_sound]
response = await authenticated_client.get("/api/v1/sounds/?types=SDB")
assert response.status_code == 200
data = response.json()
assert "sounds" in data
assert len(data["sounds"]) == 1
# Verify the repository was called with the correct types
mock_get.assert_called_once_with(["SDB"])