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")