feat: Add position shifting logic for adding sounds to playlists in repository
This commit is contained in:
@@ -480,6 +480,158 @@ class TestPlaylistRepository:
|
||||
|
||||
assert playlist_sound.position == TEST_POSITION
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_playlist_with_position_shifting(
|
||||
self,
|
||||
playlist_repository: PlaylistRepository,
|
||||
test_session: AsyncSession,
|
||||
ensure_plans: Any,
|
||||
) -> None:
|
||||
"""Test adding a sound to a playlist with position shifting when positions are occupied."""
|
||||
# Create test user
|
||||
user = User(
|
||||
email="test_shifting@example.com",
|
||||
name="Test User Shifting",
|
||||
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
|
||||
|
||||
# Create test playlist
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist Shifting",
|
||||
description="A test playlist for position shifting",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
# Create multiple sounds
|
||||
sounds = []
|
||||
for i in range(3):
|
||||
sound = Sound(
|
||||
name=f"Test Sound {i}",
|
||||
filename=f"test_{i}.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash=f"test_hash_{i}",
|
||||
play_count=0,
|
||||
)
|
||||
test_session.add(sound)
|
||||
sounds.append(sound)
|
||||
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
for sound in sounds:
|
||||
await test_session.refresh(sound)
|
||||
|
||||
playlist_id = playlist.id
|
||||
sound_ids = [s.id for s in sounds]
|
||||
|
||||
# Add first two sounds sequentially (positions 0, 1)
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[0]) # position 0
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[1]) # position 1
|
||||
|
||||
# Now insert third sound at position 1 - should shift existing sound at position 1 to position 2
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[2], position=1)
|
||||
|
||||
# Verify the final positions
|
||||
playlist_sounds = await playlist_repository.get_playlist_sound_entries(playlist_id)
|
||||
|
||||
assert len(playlist_sounds) == 3
|
||||
assert playlist_sounds[0].sound_id == sound_ids[0] # Original sound 0 stays at position 0
|
||||
assert playlist_sounds[0].position == 0
|
||||
assert playlist_sounds[1].sound_id == sound_ids[2] # New sound 2 inserted at position 1
|
||||
assert playlist_sounds[1].position == 1
|
||||
assert playlist_sounds[2].sound_id == sound_ids[1] # Original sound 1 shifted to position 2
|
||||
assert playlist_sounds[2].position == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_playlist_at_position_zero(
|
||||
self,
|
||||
playlist_repository: PlaylistRepository,
|
||||
test_session: AsyncSession,
|
||||
ensure_plans: Any,
|
||||
) -> None:
|
||||
"""Test adding a sound at position 0 when playlist already has sounds."""
|
||||
# Create test user
|
||||
user = User(
|
||||
email="test_position_zero@example.com",
|
||||
name="Test User Position Zero",
|
||||
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
|
||||
|
||||
# Create test playlist
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist Position Zero",
|
||||
description="A test playlist for position zero insertion",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
# Create multiple sounds
|
||||
sounds = []
|
||||
for i in range(3):
|
||||
sound = Sound(
|
||||
name=f"Test Sound {i}",
|
||||
filename=f"test_zero_{i}.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash=f"test_hash_zero_{i}",
|
||||
play_count=0,
|
||||
)
|
||||
test_session.add(sound)
|
||||
sounds.append(sound)
|
||||
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
for sound in sounds:
|
||||
await test_session.refresh(sound)
|
||||
|
||||
playlist_id = playlist.id
|
||||
sound_ids = [s.id for s in sounds]
|
||||
|
||||
# Add first two sounds sequentially (positions 0, 1)
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[0]) # position 0
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[1]) # position 1
|
||||
|
||||
# Now insert third sound at position 0 - should shift existing sounds to positions 1, 2
|
||||
await playlist_repository.add_sound_to_playlist(playlist_id, sound_ids[2], position=0)
|
||||
|
||||
# Verify the final positions
|
||||
playlist_sounds = await playlist_repository.get_playlist_sound_entries(playlist_id)
|
||||
|
||||
assert len(playlist_sounds) == 3
|
||||
assert playlist_sounds[0].sound_id == sound_ids[2] # New sound 2 inserted at position 0
|
||||
assert playlist_sounds[0].position == 0
|
||||
assert playlist_sounds[1].sound_id == sound_ids[0] # Original sound 0 shifted to position 1
|
||||
assert playlist_sounds[1].position == 1
|
||||
assert playlist_sounds[2].sound_id == sound_ids[1] # Original sound 1 shifted to position 2
|
||||
assert playlist_sounds[2].position == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_sound_from_playlist(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user