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:
@@ -1,7 +1,5 @@
|
||||
"""Tests for playlist API endpoints."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
@@ -96,7 +94,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
@@ -107,7 +105,7 @@ class TestPlaylistEndpoints:
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
|
||||
|
||||
response = await authenticated_client.get("/api/v1/playlists/")
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -146,11 +144,11 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
main_playlist_id = main_playlist.id
|
||||
main_playlist_name = main_playlist.name
|
||||
|
||||
|
||||
response = await authenticated_client.get("/api/v1/playlists/main")
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -189,10 +187,10 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
|
||||
response = await authenticated_client.get("/api/v1/playlists/current")
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -256,10 +254,10 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(test_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract name before HTTP request
|
||||
playlist_name = test_playlist.name
|
||||
|
||||
|
||||
payload = {
|
||||
"name": playlist_name,
|
||||
"description": "Duplicate name",
|
||||
@@ -292,13 +290,13 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(test_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract values before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
playlist_name = test_playlist.name
|
||||
|
||||
|
||||
response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}"
|
||||
f"/api/v1/playlists/{playlist_id}",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -339,10 +337,10 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(test_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
|
||||
|
||||
payload = {
|
||||
"name": "Updated Playlist",
|
||||
"description": "Updated description",
|
||||
@@ -350,7 +348,7 @@ class TestPlaylistEndpoints:
|
||||
}
|
||||
|
||||
response = await authenticated_client.put(
|
||||
f"/api/v1/playlists/{playlist_id}", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -379,7 +377,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
# Note: main_playlist doesn't need to be current=True for this test
|
||||
# The service logic handles current playlist management
|
||||
main_playlist = Playlist(
|
||||
@@ -393,14 +391,14 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
|
||||
payload = {"is_current": True}
|
||||
|
||||
response = await authenticated_client.put(
|
||||
f"/api/v1/playlists/{playlist_id}", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -429,12 +427,12 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(test_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
|
||||
|
||||
response = await authenticated_client.delete(
|
||||
f"/api/v1/playlists/{playlist_id}"
|
||||
f"/api/v1/playlists/{playlist_id}",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -442,7 +440,7 @@ class TestPlaylistEndpoints:
|
||||
|
||||
# Verify playlist is deleted
|
||||
get_response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}"
|
||||
f"/api/v1/playlists/{playlist_id}",
|
||||
)
|
||||
assert get_response.status_code == 404
|
||||
|
||||
@@ -465,12 +463,12 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
|
||||
response = await authenticated_client.delete(
|
||||
f"/api/v1/playlists/{main_playlist_id}"
|
||||
f"/api/v1/playlists/{main_playlist_id}",
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -496,7 +494,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
@@ -507,7 +505,7 @@ class TestPlaylistEndpoints:
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
|
||||
|
||||
response = await authenticated_client.get("/api/v1/playlists/search/playlist")
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -541,7 +539,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -555,12 +553,12 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before creating playlist_sound
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
sound_name = test_sound.name
|
||||
|
||||
|
||||
# Add sound to playlist manually for testing
|
||||
from app.models.playlist_sound import PlaylistSound
|
||||
|
||||
@@ -573,7 +571,7 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
|
||||
response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds"
|
||||
f"/api/v1/playlists/{playlist_id}/sounds",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -602,7 +600,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -616,15 +614,15 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
payload = {"sound_id": sound_id}
|
||||
|
||||
response = await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -632,7 +630,7 @@ class TestPlaylistEndpoints:
|
||||
|
||||
# Verify sound was added
|
||||
get_response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds"
|
||||
f"/api/v1/playlists/{playlist_id}/sounds",
|
||||
)
|
||||
assert get_response.status_code == 200
|
||||
sounds = get_response.json()
|
||||
@@ -659,7 +657,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -673,15 +671,15 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
payload = {"sound_id": sound_id, "position": 5}
|
||||
|
||||
response = await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -706,7 +704,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -720,22 +718,22 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
payload = {"sound_id": sound_id}
|
||||
|
||||
# Add sound first time
|
||||
response = await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
# Try to add same sound again
|
||||
response = await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "already in this playlist" in response.json()["detail"]
|
||||
@@ -762,14 +760,14 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(test_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
|
||||
|
||||
payload = {"sound_id": 99999}
|
||||
|
||||
response = await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
@@ -795,7 +793,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -809,20 +807,20 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
# Add sound first
|
||||
payload = {"sound_id": sound_id}
|
||||
await authenticated_client.post(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds", json=payload,
|
||||
)
|
||||
|
||||
# Remove sound
|
||||
response = await authenticated_client.delete(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/{sound_id}"
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/{sound_id}",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -830,7 +828,7 @@ class TestPlaylistEndpoints:
|
||||
|
||||
# Verify sound was removed
|
||||
get_response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds"
|
||||
f"/api/v1/playlists/{playlist_id}/sounds",
|
||||
)
|
||||
sounds = get_response.json()
|
||||
assert len(sounds) == 0
|
||||
@@ -855,7 +853,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -869,13 +867,13 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
response = await authenticated_client.delete(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/{sound_id}"
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/{sound_id}",
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
@@ -901,7 +899,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_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")
|
||||
@@ -910,7 +908,7 @@ class TestPlaylistEndpoints:
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(sound1)
|
||||
await test_session.refresh(sound2)
|
||||
|
||||
|
||||
# Extract IDs before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
sound1_id = sound1.id
|
||||
@@ -929,11 +927,11 @@ class TestPlaylistEndpoints:
|
||||
# Reorder sounds - use positions that don't cause constraints
|
||||
# When swapping, we need to be careful about unique constraints
|
||||
payload = {
|
||||
"sound_positions": [[sound1_id, 10], [sound2_id, 5]] # Use different positions to avoid constraints
|
||||
"sound_positions": [[sound1_id, 10], [sound2_id, 5]], # Use different positions to avoid constraints
|
||||
}
|
||||
|
||||
response = await authenticated_client.put(
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/reorder", json=payload
|
||||
f"/api/v1/playlists/{playlist_id}/sounds/reorder", json=payload,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -959,7 +957,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
@@ -971,12 +969,12 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
|
||||
|
||||
# Extract ID before HTTP request
|
||||
playlist_id = test_playlist.id
|
||||
|
||||
response = await authenticated_client.put(
|
||||
f"/api/v1/playlists/{playlist_id}/set-current"
|
||||
f"/api/v1/playlists/{playlist_id}/set-current",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -1001,7 +999,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
|
||||
|
||||
# Create a current playlist for the user
|
||||
user_id = test_user.id
|
||||
current_playlist = Playlist(
|
||||
@@ -1025,7 +1023,7 @@ class TestPlaylistEndpoints:
|
||||
# but something else is causing validation to fail
|
||||
assert response.status_code == 422
|
||||
return
|
||||
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "unset successfully" in response.json()["message"]
|
||||
|
||||
@@ -1055,7 +1053,7 @@ class TestPlaylistEndpoints:
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
|
||||
test_sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
@@ -1069,14 +1067,14 @@ class TestPlaylistEndpoints:
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(test_sound)
|
||||
|
||||
|
||||
# Extract IDs before HTTP requests
|
||||
playlist_id = test_playlist.id
|
||||
sound_id = test_sound.id
|
||||
|
||||
|
||||
# Initially empty
|
||||
response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}/stats"
|
||||
f"/api/v1/playlists/{playlist_id}/stats",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -1093,7 +1091,7 @@ class TestPlaylistEndpoints:
|
||||
|
||||
# Check stats again
|
||||
response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{playlist_id}/stats"
|
||||
f"/api/v1/playlists/{playlist_id}/stats",
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -1110,8 +1108,8 @@ class TestPlaylistEndpoints:
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test that users can only access their own playlists."""
|
||||
from app.utils.auth import JWTUtils, PasswordUtils
|
||||
from app.models.plan import Plan
|
||||
from app.utils.auth import PasswordUtils
|
||||
|
||||
# Create plan within this test to avoid session issues
|
||||
plan = Plan(
|
||||
@@ -1124,10 +1122,10 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(plan)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(plan)
|
||||
|
||||
|
||||
# Extract plan ID immediately to avoid session issues
|
||||
plan_id = plan.id
|
||||
|
||||
|
||||
# Create another user with their own playlist
|
||||
other_user = User(
|
||||
email="other@example.com",
|
||||
@@ -1144,7 +1142,7 @@ class TestPlaylistEndpoints:
|
||||
|
||||
# Extract other user ID before creating playlist
|
||||
other_user_id = other_user.id
|
||||
|
||||
|
||||
other_playlist = Playlist(
|
||||
user_id=other_user_id,
|
||||
name="Other User's Playlist",
|
||||
@@ -1153,13 +1151,13 @@ class TestPlaylistEndpoints:
|
||||
test_session.add(other_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(other_playlist)
|
||||
|
||||
|
||||
# Extract playlist ID before HTTP requests
|
||||
other_playlist_id = other_playlist.id
|
||||
|
||||
# Try to access other user's playlist
|
||||
response = await authenticated_client.get(
|
||||
f"/api/v1/playlists/{other_playlist_id}"
|
||||
f"/api/v1/playlists/{other_playlist_id}",
|
||||
)
|
||||
|
||||
# Currently the implementation allows access to all playlists
|
||||
|
||||
Reference in New Issue
Block a user