Spaces:
Running
Running
File size: 7,431 Bytes
5c81fbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
#!/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") |