Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Test Supabase client functionality for music player | |
""" | |
import os | |
from supabase import create_client, Client | |
from dotenv import load_dotenv | |
load_dotenv() | |
def test_supabase_client(): | |
"""Test Supabase client functionality""" | |
print("π Testing Supabase Client for Music Player") | |
print("=" * 60) | |
try: | |
# Get Supabase client | |
url = os.environ.get("SUPABASE_URL") | |
key = os.environ.get("SUPABASE_KEY") | |
if not url or not key: | |
print("β SUPABASE_URL and SUPABASE_KEY environment variables are required") | |
return False | |
print(f"β Supabase URL: {url}") | |
print(f"β Supabase Key: {key[:10]}...") | |
supabase = create_client(url, key) | |
print("β Supabase client created successfully!") | |
# Test basic connection | |
print("\nπ Testing basic connection...") | |
response = supabase.table("playlist_songs").select("*").limit(1).execute() | |
print("β Basic query successful!") | |
# Test playlists table | |
print("\nπ Testing playlists table...") | |
try: | |
response = supabase.table("playlists").select("*").execute() | |
print(f"β Playlists table accessible! Found {len(response.data)} playlists") | |
except Exception as e: | |
print(f"β οΈ Playlists table not found or accessible: {e}") | |
# Test playlist_songs table | |
print("\nπ΅ Testing playlist_songs table...") | |
try: | |
response = supabase.table("playlist_songs").select("*").execute() | |
print(f"β Playlist_songs table accessible! Found {len(response.data)} songs") | |
except Exception as e: | |
print(f"β οΈ Playlist_songs table not found or accessible: {e}") | |
# Test user_preferences table | |
print("\nβοΈ Testing user_preferences table...") | |
try: | |
response = supabase.table("user_preferences").select("*").execute() | |
print(f"β User_preferences table accessible! Found {len(response.data)} preferences") | |
except Exception as e: | |
print(f"β οΈ User_preferences table not found or accessible: {e}") | |
return True | |
except Exception as e: | |
print(f"β Supabase client test failed: {e}") | |
return False | |
def test_playlist_operations(): | |
"""Test playlist CRUD operations""" | |
print("\nπ΅ Testing Playlist Operations") | |
print("=" * 60) | |
try: | |
supabase = create_client(os.environ.get("SUPABASE_URL"), os.environ.get("SUPABASE_KEY")) | |
# Test creating a playlist | |
print("π Creating test playlist...") | |
playlist_data = {"name": "Test Playlist"} | |
response = supabase.table("playlists").insert(playlist_data).execute() | |
playlist = response.data[0] | |
playlist_id = playlist['id'] | |
print(f"β Created playlist: {playlist['name']} (ID: {playlist_id})") | |
# Test adding songs to playlist | |
print("π΅ Adding songs to playlist...") | |
songs = ["Test Song 1.mp3", "Test Song 2.mp3"] | |
song_data = [{"playlist_id": playlist_id, "song_name": song, "song_order": i} | |
for i, song in enumerate(songs)] | |
response = supabase.table("playlist_songs").insert(song_data).execute() | |
print(f"β Added {len(response.data)} songs to playlist") | |
# Test reading playlist songs | |
print("π Reading playlist songs...") | |
response = supabase.table("playlist_songs").select("song_name, song_order").eq("playlist_id", playlist_id).order("song_order").execute() | |
songs = response.data | |
print(f"β Retrieved {len(songs)} songs from playlist") | |
for song in songs: | |
print(f" - {song['song_name']} (order: {song['song_order']})") | |
# Test updating preference | |
print("βοΈ Testing preference update...") | |
preference_data = {"preference_key": "test_preference", "preference_value": "test_value"} | |
response = supabase.table("user_preferences").upsert(preference_data).execute() | |
print("β Preference updated successfully") | |
# Clean up test data | |
print("π§Ή Cleaning up test data...") | |
supabase.table("playlists").delete().eq("id", playlist_id).execute() | |
supabase.table("user_preferences").delete().eq("preference_key", "test_preference").execute() | |
print("β Test data cleaned up") | |
return True | |
except Exception as e: | |
print(f"β Playlist operations test failed: {e}") | |
return False | |
def create_tables_if_needed(): | |
"""Create tables if they don't exist""" | |
print("\nπ§ Creating Tables (if needed)") | |
print("=" * 60) | |
try: | |
supabase = create_client(os.environ.get("SUPABASE_URL"), os.environ.get("SUPABASE_KEY")) | |
# Note: Supabase client doesn't have table creation methods | |
# Tables should be created manually in the Supabase dashboard | |
# or using the SQL editor | |
print("π Please create the following tables in your Supabase SQL Editor:") | |
print() | |
print("1. playlists table:") | |
print(""" | |
CREATE TABLE playlists ( | |
id SERIAL PRIMARY KEY, | |
name VARCHAR(255) NOT NULL, | |
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), | |
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | |
); | |
""") | |
print("2. playlist_songs table:") | |
print(""" | |
CREATE TABLE playlist_songs ( | |
id SERIAL PRIMARY KEY, | |
playlist_id INTEGER REFERENCES playlists(id) ON DELETE CASCADE, | |
song_name VARCHAR(500) NOT NULL, | |
song_order INTEGER DEFAULT 0, | |
added_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | |
); | |
""") | |
print("3. user_preferences table:") | |
print(""" | |
CREATE TABLE user_preferences ( | |
id SERIAL PRIMARY KEY, | |
preference_key VARCHAR(100) UNIQUE NOT NULL, | |
preference_value TEXT, | |
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), | |
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | |
); | |
""") | |
return True | |
except Exception as e: | |
print(f"β Table creation guidance failed: {e}") | |
return False | |
if __name__ == "__main__": | |
print("π Supabase Client Test for Music Player") | |
print("=" * 60) | |
# Test basic client functionality | |
if test_supabase_client(): | |
print("\nβ Basic Supabase client test passed!") | |
# Test playlist operations | |
if test_playlist_operations(): | |
print("\nβ Playlist operations test passed!") | |
else: | |
print("\nβ οΈ Playlist operations test failed - tables may not exist") | |
create_tables_if_needed() | |
print("\nπ Your Supabase client is working!") | |
print("Next steps:") | |
print("1. Create the required tables in Supabase SQL Editor") | |
print("2. Set SUPABASE_URL and SUPABASE_KEY in Hugging Face Spaces") | |
print("3. Deploy your music player!") | |
else: | |
print("\nβ Supabase client test failed") | |
print("Please check your SUPABASE_URL and SUPABASE_KEY environment variables") |