File size: 12,303 Bytes
ab5bfcc
 
fe8cc5a
 
 
ab5bfcc
 
 
fe8cc5a
ab5bfcc
 
fe8cc5a
ab5bfcc
 
fe8cc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import pytest
import os
import sys
import time
from datetime import datetime, timedelta

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from src.supabase_integration import AdvancedSupabaseIntegration

class TestSupabaseIntegration:
    """Production Supabase integration tests with real database connections"""
    
    def setup_method(self):
        """Setup real Supabase connection"""
        self.supabase_url = os.environ['SUPABASE_URL']
        self.supabase_key = os.environ['SUPABASE_ANON_KEY']
        
        self.supabase = AdvancedSupabaseIntegration(
            supabase_url=self.supabase_url,
            supabase_key=self.supabase_key
        )
        
        # Wait for connection to establish
        time.sleep(1)
    
    def test_production_connection_established(self):
        """Test that we can establish real connection to production Supabase"""
        assert self.supabase.is_connected() == True
        
        # Test actual API endpoint connectivity
        import requests
        response = requests.get(
            f"{self.supabase_url}/rest/v1/",
            headers={"apikey": self.supabase_key},
            timeout=10
        )
        assert response.status_code == 200
    
    def test_real_database_schema_access(self):
        """Test access to real database tables and schema"""
        connection_test = self.supabase.test_connection()
        
        # Verify we can access all critical tables
        critical_tables = ['tracks', 'artists', 'profiles', 'courses', 'playlists']
        
        for table in critical_tables:
            assert table in connection_test['tables']
            assert connection_test['tables'][table]['accessible'] == True
            assert connection_test['tables'][table]['status_code'] == 200
    
    def test_real_data_retrieval_performance(self):
        """Test real data retrieval performance from production"""
        start_time = time.time()
        
        # Test multiple data retrieval operations
        tracks = self.supabase.get_popular_tracks(limit=10)
        artists = self.supabase.get_popular_artists(limit=10)
        courses = self.supabase.get_recent_courses(limit=10)
        stats = self.supabase.get_platform_stats()
        
        end_time = time.time()
        total_duration = end_time - start_time
        
        # Performance check - should complete within reasonable time
        assert total_duration < 10.0  # All operations within 10 seconds
        
        # Data validation
        assert len(tracks) <= 10
        assert len(artists) <= 10
        assert len(courses) <= 10
        assert isinstance(stats, dict)
    
    def test_real_context_generation_performance(self):
        """Test real music context generation performance"""
        test_queries = [
            "guitar lessons",
            "pop music",
            "electronic music production",
            "music theory basics",
            "how to create a playlist"
        ]
        
        for query in test_queries:
            start_time = time.time()
            context = self.supabase.get_music_context(query, "test_user")
            end_time = time.time()
            
            duration = end_time - start_time
            
            # Should generate context within 5 seconds
            assert duration < 5.0
            
            # Context should be relevant to query
            assert isinstance(context, dict)
            assert 'summary' in context
            assert len(context['summary']) > 0
    
    def test_real_user_context_retrieval(self):
        """Test real user context retrieval with actual user data"""
        # Test with a user that exists in your production database
        # Replace with actual test user IDs from your system
        test_users = [
            "existing_user_1",
            "existing_user_2", 
            "existing_user_3"
        ]
        
        for user_id in test_users:
            user_context = self.supabase.get_user_context(user_id)
            
            assert isinstance(user_context, dict)
            assert 'is_premium' in user_context
            assert 'favorite_genres' in user_context
            assert 'recent_activity' in user_context
            assert 'learning_progress' in user_context
            
            # Validate data types
            assert isinstance(user_context['is_premium'], bool)
            assert isinstance(user_context['favorite_genres'], list)
            assert isinstance(user_context['recent_activity'], list)
            assert isinstance(user_context['learning_progress'], dict)
    
    def test_real_cache_functionality(self):
        """Test real caching functionality with production data"""
        query = "test cache query"
        user_id = "cache_test_user"
        
        # First call - should fetch from database
        start_time = time.time()
        context1 = self.supabase.get_music_context(query, user_id)
        first_duration = time.time() - start_time
        
        # Second call - should be faster (cached)
        start_time = time.time()
        context2 = self.supabase.get_music_context(query, user_id)
        second_duration = time.time() - start_time
        
        # Contexts should be identical
        assert context1 == context2
        
        # Cached call should be faster
        assert second_duration < first_duration
    
    def test_real_error_handling_invalid_user(self):
        """Test real error handling with invalid user data"""
        invalid_user_id = "non_existent_user_999999"
        
        # Should handle gracefully without crashing
        user_context = self.supabase.get_user_context(invalid_user_id)
        
        assert isinstance(user_context, dict)
        assert user_context['is_premium'] == False
        assert user_context['favorite_genres'] == []
        assert user_context['recent_activity'] == []
        assert user_context['learning_progress'] == {}
    
    def test_real_data_consistency(self):
        """Test real data consistency across multiple retrievals"""
        # Test that multiple calls return consistent data
        contexts = []
        
        for i in range(3):
            context = self.supabase.get_music_context("music courses", f"user_{i}")
            contexts.append(context)
        
        # All contexts should have the same structure
        for context in contexts:
            assert 'tracks' in context
            assert 'artists' in context
            assert 'courses' in context
            assert 'stats' in context
            assert 'summary' in context
        
        # Stats should be consistent across calls
        stats_values = [context['stats']['track_count'] for context in contexts]
        assert len(set(stats_values)) == 1  # All should be the same
    
    def test_real_concurrent_requests(self):
        """Test real concurrent request handling"""
        import threading
        
        results = []
        errors = []
        
        def test_request(query, user_id):
            try:
                context = self.supabase.get_music_context(query, user_id)
                results.append((query, user_id, context))
            except Exception as e:
                errors.append((query, user_id, str(e)))
        
        # Create multiple concurrent requests
        threads = []
        test_cases = [
            ("rock music", "user_1"),
            ("jazz courses", "user_2"),
            ("hip hop artists", "user_3"),
            ("classical music", "user_4"),
            ("music production", "user_5")
        ]
        
        for query, user_id in test_cases:
            thread = threading.Thread(target=test_request, args=(query, user_id))
            threads.append(thread)
            thread.start()
        
        # Wait for all threads to complete
        for thread in threads:
            thread.join()
        
        # Verify all requests completed successfully
        assert len(errors) == 0
        assert len(results) == len(test_cases)
        
        # Verify all results are valid
        for query, user_id, context in results:
            assert isinstance(context, dict)
            assert 'summary' in context
            assert len(context['summary']) > 0
    
    def test_real_detailed_statistics(self):
        """Test real detailed statistics retrieval"""
        detailed_stats = self.supabase.get_detailed_stats()
        
        assert isinstance(detailed_stats, dict)
        assert 'basic' in detailed_stats
        assert 'content_breakdown' in detailed_stats
        assert 'performance' in detailed_stats
        
        # Validate basic stats
        basic_stats = detailed_stats['basic']
        assert basic_stats['track_count'] > 0
        assert basic_stats['artist_count'] > 0
        assert basic_stats['user_count'] > 0
        
        # Validate content breakdown
        breakdown = detailed_stats['content_breakdown']
        assert 'tracks_by_popularity' in breakdown
        assert 'artists_by_followers' in breakdown
        assert 'courses_by_rating' in breakdown
        
        # These should be real distributions from your database
        for distribution in breakdown.values():
            assert isinstance(distribution, list)
            if distribution:  # Might be empty in some cases
                for item in distribution:
                    assert 'range' in item
                    assert 'count' in item
    
    def test_real_query_intent_analysis(self):
        """Test real query intent analysis with production data"""
        test_queries = [
            ("How do I create a playlist?", "Instructional"),
            ("What is music theory?", "Explanatory"),
            ("I can't upload my track", "Support"),
            ("Recommend some jazz music", "Discovery"),
            ("How much does premium cost?", "Commercial"),
            ("Tell me about Saem's Tunes", "General")
        ]
        
        for query, expected_intent in test_queries:
            analyzed_intent = self.supabase.analyze_query_intent(query)
            
            # Should correctly identify intent patterns
            assert analyzed_intent in [
                "Instructional - seeking how-to information",
                "Explanatory - seeking information", 
                "Support - seeking technical help",
                "Discovery - seeking recommendations",
                "Commercial - seeking pricing information",
                "General inquiry about platform features"
            ]
    
    def test_real_fallback_mechanisms(self):
        """Test real fallback mechanisms when database is unavailable"""
        # Note: We can't easily test database downtime in automated tests
        # But we can verify fallback data structure
        
        # Clear cache to force fresh data
        self.supabase.clear_cache()
        
        # This should use real data, but verify structure matches fallback expectations
        context = self.supabase.get_music_context("test query", "test_user")
        
        # Even with real data, structure should match expected format
        assert 'tracks' in context
        assert 'artists' in context
        assert 'courses' in context
        assert 'stats' in context
        assert 'summary' in context
        assert 'timestamp' in context
    
    def test_real_security_integration(self):
        """Test real security integration with production data"""
        # Test that security measures don't interfere with legitimate data access
        legitimate_queries = [
            "music courses for beginners",
            "how to play guitar",
            "best pop songs 2024",
            "music production tutorials",
            "artist dashboard features"
        ]
        
        for query in legitimate_queries:
            context = self.supabase.get_music_context(query, "legitimate_user")
            
            # Should successfully retrieve context for legitimate queries
            assert isinstance(context, dict)
            assert 'summary' in context
            assert len(context['summary']) > 0
    
    def teardown_method(self):
        """Cleanup after tests"""
        if hasattr(self, 'supabase'):
            self.supabase.clear_cache()