feat: Implement playlist reordering with position swapping and reload player on current playlist changes
This commit is contained in:
@@ -811,3 +811,78 @@ class TestPlaylistRepository:
|
||||
assert len(sounds) == TWO_SOUNDS
|
||||
assert sounds[0].id == sound2_id # sound2 now at position 5
|
||||
assert sounds[1].id == sound1_id # sound1 now at position 10
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reorder_playlist_sounds_position_swap(
|
||||
self,
|
||||
playlist_repository: PlaylistRepository,
|
||||
test_session: AsyncSession,
|
||||
ensure_plans: Any,
|
||||
) -> None:
|
||||
"""Test reordering sounds with position swapping (regression test)."""
|
||||
# Create objects within this test
|
||||
user = User(
|
||||
email="test@example.com",
|
||||
name="Test User",
|
||||
password_hash=PasswordUtils.hash_password("password123"),
|
||||
role="user",
|
||||
is_active=True,
|
||||
plan_id=ensure_plans[0].id,
|
||||
credits=100,
|
||||
)
|
||||
test_session.add(user)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(user)
|
||||
|
||||
user_id = user.id
|
||||
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
# Create multiple sounds
|
||||
sound1 = Sound(name="Sound 1", filename="sound1.mp3", type="SDB", hash="hash1")
|
||||
sound2 = Sound(name="Sound 2", filename="sound2.mp3", type="SDB", hash="hash2")
|
||||
test_session.add_all([playlist, sound1, sound2])
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound1)
|
||||
await test_session.refresh(sound2)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound1_id = sound1.id
|
||||
sound2_id = sound2.id
|
||||
|
||||
# Add sounds to playlist at positions 0 and 1
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound1_id, position=0,
|
||||
)
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound2_id, position=1,
|
||||
)
|
||||
|
||||
# Verify initial order
|
||||
sounds = await playlist_repository.get_playlist_sounds(playlist_id)
|
||||
assert len(sounds) == TWO_SOUNDS
|
||||
assert sounds[0].id == sound1_id # sound1 at position 0
|
||||
assert sounds[1].id == sound2_id # sound2 at position 1
|
||||
|
||||
# Swap positions - this used to cause unique constraint violation
|
||||
sound_positions = [(sound1_id, 1), (sound2_id, 0)]
|
||||
await playlist_repository.reorder_playlist_sounds(
|
||||
playlist_id, sound_positions,
|
||||
)
|
||||
|
||||
# Verify swapped order
|
||||
sounds = await playlist_repository.get_playlist_sounds(playlist_id)
|
||||
assert len(sounds) == TWO_SOUNDS
|
||||
assert sounds[0].id == sound2_id # sound2 now at position 0
|
||||
assert sounds[1].id == sound1_id # sound1 now at position 1
|
||||
|
||||
Reference in New Issue
Block a user