File size: 45,849 Bytes
b8277c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
"""
Comprehensive End-to-End API Test Suite for Data Sources API
==============================================================

This test file covers ALL API endpoints from tenant registration through SQL execution.
It uses only the `requests` library to perform real HTTP calls against the running API.

TEST COVERAGE:
--------------
1. Tenant Management (API Keys)
   - Create API keys
   - List API keys
   - Revoke API keys
   
2. Data Source Configuration
   - Add tenant data sources (POST)
   - List tenant data sources (GET)
   - Update data sources (PUT)
   - Delete data sources (DELETE)
   
3. Schema Discovery
   - List available sources
   - Get source schema
   - Search schema with keywords
   
4. SQL Execution
   - Synchronous SQL execution
   - Asynchronous job submission
   - Job result retrieval
   - Paginated results
   
5. Job Management
   - Job cancellation
   - Job deletion
   - Dead Letter Queue (DLQ) operations
   
6. Metrics & Monitoring
   - Get metrics summary
   - Cleanup old metrics
   
7. Plan Caching (Natural Language)
   - Create NL job with caching
   - Cache statistics

PREREQUISITES:
--------------
1. API server must be running: `python -m uvicorn backend.data_sources.main:app --reload`
2. Redis must be running: `redis-server`
3. Set SIRUS_ADMIN_API_KEY environment variable for admin operations

USAGE:
------
# Run all tests
python test_api_e2e.py

# Run with custom configuration
python test_api_e2e.py --api-url http://localhost:8000 --admin-key YOUR_KEY --verbose

# Skip cleanup (useful for debugging)
python test_api_e2e.py --no-cleanup

# Run specific test category
python test_api_e2e.py --category tenant-mgmt
"""

import sys
import argparse
import json
import time
import logging
from typing import Dict, List, Any, Optional, Tuple
from datetime import datetime, timedelta
from dataclasses import dataclass, field

import requests


# ============================================================================
# CONFIGURATION & SETUP
# ============================================================================

class Colors:
    """ANSI color codes for terminal output."""
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    CYAN = '\033[96m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


@dataclass
class TestResult:
    """Represents the result of a single test."""
    name: str
    passed: bool
    duration: float
    message: str = ""
    error: Optional[str] = None
    response_data: Optional[Dict] = None


@dataclass
class TestContext:
    """Shared context across all tests."""
    # Configuration
    api_url: str
    admin_api_key: str
    verbose: bool
    
    # Test data
    tenant_id: str = "test_e2e_tenant"
    api_key: Optional[str] = None
    api_key_id: Optional[str] = None
    
    # Created resources (for cleanup)
    created_api_keys: List[str] = field(default_factory=list)
    created_sources: List[str] = field(default_factory=list)
    created_jobs: List[str] = field(default_factory=list)
    
    # Test results
    results: List[TestResult] = field(default_factory=list)
    category_stats: Dict[str, Dict] = field(default_factory=dict)


class E2ETestRunner:
    """Main test runner for end-to-end API tests."""
    
    def __init__(self, context: TestContext):
        self.ctx = context
        self.session = requests.Session()
        self.logger = self._setup_logger()
        
        # API Header constants
        self.ADMIN_HEADER = {"X-Sirus-Admin-Key": self.ctx.admin_api_key}
        self.TENANT_HEADER = {}  # Will be set after API key creation
        
    def _setup_logger(self) -> logging.Logger:
        """Setup logging configuration."""
        logger = logging.getLogger("E2ETestRunner")
        logger.setLevel(logging.DEBUG if self.ctx.verbose else logging.INFO)
        
        handler = logging.StreamHandler()
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        
        return logger
    
    def _make_request(
        self, 
        method: str, 
        endpoint: str, 
        headers: Optional[Dict] = None,
        json_data: Optional[Dict] = None,
        params: Optional[Dict] = None,
        expect_status: int = 200
    ) -> Tuple[bool, Optional[Dict], Optional[str]]:
        """Make HTTP request and return (success, data, error)."""
        url = f"{self.ctx.api_url}{endpoint}"
        
        try:
            if self.ctx.verbose:
                self.logger.debug(f"{method} {url}")
                if json_data:
                    self.logger.debug(f"Body: {json.dumps(json_data, indent=2)}")
            
            response = self.session.request(
                method=method,
                url=url,
                headers=headers,
                json=json_data,
                params=params,
                timeout=30
            )
            
            if self.ctx.verbose:
                self.logger.debug(f"Status: {response.status_code}")
                self.logger.debug(f"Response: {response.text[:500]}")
            
            # Check expected status
            if response.status_code != expect_status:
                return False, None, f"Expected {expect_status}, got {response.status_code}: {response.text}"
            
            # Try to parse JSON
            try:
                data = response.json()
                return True, data, None
            except:
                # Some endpoints might not return JSON
                if response.status_code == 204:
                    return True, None, None
                return True, {"text": response.text}, None
                
        except requests.exceptions.Timeout:
            return False, None, "Request timeout"
        except requests.exceptions.ConnectionError:
            return False, None, f"Connection error - is the API running at {self.ctx.api_url}?"
        except Exception as e:
            return False, None, f"Request failed: {str(e)}"
    
    def _run_test(self, test_name: str, test_func) -> TestResult:
        """Run a single test and return result."""
        print(f"\n{Colors.CYAN}▶ Running: {test_name}{Colors.ENDC}")
        
        start_time = time.time()
        
        try:
            test_func()
            duration = time.time() - start_time
            
            result = TestResult(
                name=test_name,
                passed=True,
                duration=duration,
                message="✓ PASSED"
            )
            print(f"{Colors.GREEN}  ✓ PASSED in {duration:.2f}s{Colors.ENDC}")
            
        except AssertionError as e:
            duration = time.time() - start_time
            result = TestResult(
                name=test_name,
                passed=False,
                duration=duration,
                message="✗ FAILED",
                error=str(e)
            )
            print(f"{Colors.RED}  ✗ FAILED in {duration:.2f}s: {e}{Colors.ENDC}")
            
        except Exception as e:
            duration = time.time() - start_time
            result = TestResult(
                name=test_name,
                passed=False,
                duration=duration,
                message="✗ ERROR",
                error=f"Unexpected error: {str(e)}"
            )
            print(f"{Colors.RED}  ✗ ERROR in {duration:.2f}s: {e}{Colors.ENDC}")
        
        self.ctx.results.append(result)
        return result
    
    # ========================================================================
    # CATEGORY 1: TENANT MANAGEMENT (API KEYS)
    # ========================================================================
    
    def test_01_create_api_key(self):
        """Test creating a new API key for tenant."""
        headers = self.ADMIN_HEADER
        
        success, data, error = self._make_request(
            method="POST",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/api-keys",
            headers=headers,
            json_data={
                "description": "E2E Test API Key",
                "expires_in_days": 30
            },
            expect_status=201
        )
        
        assert success, f"Failed to create API key: {error}"
        assert data is not None, "No response data"
        assert "api_key" in data, "Missing api_key in response"
        assert "key_info" in data, "Missing key_info in response"
        assert "key_id" in data["key_info"], "Missing key_id in key_info"
        assert len(data["api_key"]) == 64, "API key should be 64 characters"
        
        # Store for later tests
        self.ctx.api_key = data["api_key"]
        self.ctx.api_key_id = data["key_info"]["key_id"]
        self.ctx.created_api_keys.append(data["key_info"]["key_id"])
        
        # Set the tenant header now that we have the API key
        self.TENANT_HEADER = {"X-Sirus-Api-Key": self.ctx.api_key}
        
        self.logger.info(f"Created API key: {data['key_info']['key_id'][:8]}... (expires: {data['key_info'].get('expires_at')})")
    
    def test_02_list_api_keys(self):
        """Test listing all API keys for tenant."""
        headers = self.ADMIN_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/api-keys",
            headers=headers
        )
        
        assert success, f"Failed to list API keys: {error}"
        assert data is not None, "No response data"
        assert "keys" in data, "Missing keys array in response"
        assert "total_count" in data, "Missing total_count in response"
        assert isinstance(data["keys"], list), "keys should be a list"
        
        # Verify our created key is in the list
        key_ids = [k["key_id"] for k in data["keys"]]
        assert self.ctx.api_key_id in key_ids, "Created key not found in list"
        
        self.logger.info(f"Listed {data['total_count']} API keys")
    
    def test_03_create_api_key_without_admin_auth(self):
        """Test that creating API key without admin auth fails."""
        success, data, error = self._make_request(
            method="POST",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/api-keys",
            json_data={"description": "Unauthorized attempt"},
            expect_status=401
        )
        
        assert success, "Should return 401 for unauthorized request"
        self.logger.info("Correctly rejected unauthorized API key creation")
    
    # ========================================================================
    # CATEGORY 2: DATA SOURCE CONFIGURATION
    # ========================================================================
    
    def test_04_add_data_source(self):
        """Test adding a data source to tenant configuration."""
        headers = self.TENANT_HEADER
        
        source_config = {
            "sources": [
                {
                    "source_name": "test_duckdb",
                    "source_type": "ibis",
                    "config": {
                        "uri": "duckdb:///:memory:",
                        "table_fetch_example_limit": 3
                    }
                }
            ],
            "validate_connection": False  # Skip validation for in-memory DuckDB
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/sources",
            headers=headers,
            json_data=source_config,
            expect_status=201
        )
        
        assert success, f"Failed to add data source: {error}"
        assert data is not None, "No response data"
        assert data.get("persisted") is True, "Data source was not persisted"
        assert len(data.get("sources", [])) == 1, "Should have 1 source in response"
        
        self.ctx.created_sources.append("test_duckdb")
        self.logger.info(f"Added data source: test_duckdb")
    
    def test_05_list_data_sources(self):
        """Test listing all data sources for tenant."""
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/sources",
            headers=headers
        )
        
        assert success, f"Failed to list data sources: {error}"
        assert data is not None, "No response data"
        assert "sources" in data, "Missing sources array"
        assert len(data["sources"]) > 0, "Should have at least one source"
        
        # Verify our source is in the list
        source_names = [s["source_name"] for s in data["sources"]]
        assert "test_duckdb" in source_names, "test_duckdb not found in sources"
        
        self.logger.info(f"Listed {len(data['sources'])} data sources")
    
    def test_06_update_data_source(self):
        """Test updating an existing data source."""
        headers = self.TENANT_HEADER
        
        updated_config = {
            "source_name": "test_duckdb",
            "source_type": "ibis",
            "config": {
                "uri": "duckdb:///:memory:",
                "table_fetch_example_limit": 5  # Changed from 3 to 5
            }
        }
        
        # Add query parameter for validation
        success, data, error = self._make_request(
            method="PUT",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/sources/test_duckdb",
            headers=headers,
            json_data=updated_config,
            params={"validate_connection": False}  # Skip validation
        )
        
        assert success, f"Failed to update data source: {error}"
        assert data is not None, "No response data"
        assert data.get("persisted") is True, "Data source update was not persisted"
        assert len(data.get("sources", [])) == 1, "Should have 1 source in response"
        
        self.logger.info("Updated data source: test_duckdb")
    
    def test_07_add_second_data_source(self):
        """Test adding a second data source with real PostgreSQL."""
        headers = self.TENANT_HEADER
        
        source_config = {
            "sources": [
                {
                    "source_name": "test_postgres",
                    "source_type": "ibis",
                    "config": {
                        "uri": "postgresql://neondb_owner:npg_dfWNsn2ZGk7c@ep-cool-poetry-a1puamly-pooler.ap-southeast-1.aws.neon.tech:5432/scv-sample?sslmode=require",
                        "table_fetch_example_limit": 3
                    }
                }
            ],
            "validate_connection": True  # Validate PostgreSQL connection
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/sources",
            headers=headers,
            json_data=source_config,
            expect_status=201
        )
        
        assert success, f"Failed to add PostgreSQL source: {error}"
        self.ctx.created_sources.append("test_postgres")
        self.logger.info("Added PostgreSQL data source: test_postgres")
    
    # ========================================================================
    # CATEGORY 3: SCHEMA DISCOVERY
    # ========================================================================
    
    def test_08_list_available_sources(self):
        """Test listing available data sources via /list endpoint."""
        params = {"tenant_id": self.ctx.tenant_id}
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/list",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to list sources: {error}"
        assert data is not None, "No response data"
        assert "available_sources" in data, "Missing available_sources array"
        assert len(data["available_sources"]) >= 2, "Should have at least 2 sources"
        
        source_names = data["available_sources"]
        assert "test_duckdb" in source_names, "test_duckdb not found"
        assert "test_postgres" in source_names, "test_postgres not found"
        
        self.logger.info(f"Listed {len(data['available_sources'])} available sources")
    
    def test_09_get_source_schema(self):
        """Test fetching schema for a specific source."""
        params = {"tenant_id": self.ctx.tenant_id}
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/schema/test_postgres",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to get schema: {error}"
        assert data is not None, "No response data"
        assert "schema" in data, "Missing schema in response"
        
        # Schema should be a JSON array
        schema = data["schema"]
        if isinstance(schema, str):
            schema = json.loads(schema)
        
        assert isinstance(schema, list), "Schema should be a list"
        assert len(schema) > 0, "Schema should not be empty"
        assert "tables" in schema[0], "Schema should contain tables"
        
        self.logger.info(f"Retrieved schema with {len(schema[0].get('tables', []))} tables")
    
    def test_10_search_schema_with_keywords(self):
        """Test searching schema with semantic keywords."""
        headers = self.TENANT_HEADER
        
        search_request = {
            "tenant_id": self.ctx.tenant_id,
            "keywords": ["customer", "order", "product"],
            "max_tables_per_source": 10
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/schema/search",
            headers=headers,
            json_data=search_request
        )
        
        assert success, f"Failed to search schema: {error}"
        assert data is not None, "No response data"
        assert "matches" in data, "Missing matches"
        assert isinstance(data["matches"], list), "matches should be a list"
        
        self.logger.info(f"Found {len(data['matches'])} matching tables")
    
    # ========================================================================
    # CATEGORY 4: SQL EXECUTION (SYNCHRONOUS)
    # ========================================================================
    
    def test_11_execute_sql_sync_simple_query(self):
        """Test synchronous SQL execution with simple query."""
        headers = self.TENANT_HEADER
        
        sql_request = {
            "tenant_id": self.ctx.tenant_id,
            "source_name": "test_postgres",
            "sql_query": "SELECT 1 as test_value, 'hello' as test_string",
            "async_mode": False
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/execute-raw-sql",
            headers=headers,
            json_data=sql_request
        )
        
        assert success, f"Failed to execute SQL: {error}"
        assert data is not None, "No response data"
        assert "results" in data, "Missing results in response"
        assert len(data["results"]) > 0, "Result should not be empty"
        assert data["results"][0]["test_value"] == 1, "test_value should be 1"
        
        self.logger.info("Successfully executed simple SQL query")
    
    def test_12_execute_sql_sync_table_query(self):
        """Test synchronous SQL execution querying real table."""
        headers = self.TENANT_HEADER
        
        # First, get the schema to find an existing table
        success, schema_data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/schema/test_postgres",
            headers=headers,
            params={"tenant_id": self.ctx.tenant_id}
        )
        
        # Extract table name from schema
        table_name = None
        if success and schema_data and "schema" in schema_data:
            schema = schema_data["schema"]
            if isinstance(schema, str):
                schema = json.loads(schema)
            if isinstance(schema, list) and len(schema) > 0 and "tables" in schema[0]:
                tables = schema[0]["tables"]
                if tables:
                    table_name = tables[0].get("name") or tables[0].get("table_name")
        
        # If we couldn't get a table name, use a simple query instead
        if not table_name:
            self.logger.warning("Could not determine table name from schema, using simple query")
            sql_query = "SELECT 1 as id, 'test' as name"
        else:
            sql_query = f"SELECT * FROM {table_name} LIMIT 5"
        
        sql_request = {
            "tenant_id": self.ctx.tenant_id,
            "source_name": "test_postgres",
            "sql_query": sql_query,
            "async_mode": False
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/execute-raw-sql",
            headers=headers,
            json_data=sql_request
        )
        
        assert success, f"Failed to execute table query: {error}"
        assert data is not None, "No response data"
        assert "results" in data, "Missing results"
        assert isinstance(data["results"], list), "Results should be a list"
        
        if len(data["results"]) > 0:
            self.logger.info(f"Retrieved {len(data['results'])} rows from {table_name or 'query'}")
        else:
            self.logger.info(f"Query returned no rows (table {table_name or 'query'} might be empty)")
    
    def test_13_execute_sql_sync_invalid_sql(self):
        """Test that invalid SQL returns proper error."""
        headers = self.TENANT_HEADER
        
        sql_request = {
            "tenant_id": self.ctx.tenant_id,
            "source_name": "test_postgres",
            "sql_query": "SELECT * FROM nonexistent_table_12345",
            "async_mode": False
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/execute-raw-sql",
            headers=headers,
            json_data=sql_request,
            expect_status=400  # Should return error (bad request for invalid SQL)
        )
        
        assert success, "Should return 400 for invalid SQL"
        self.logger.info("Correctly handled invalid SQL query")
    
    def test_14_execute_sql_blocked_dangerous_query(self):
        """Test that dangerous SQL operations are blocked."""
        headers = self.TENANT_HEADER
        
        # Try DROP statement
        sql_request = {
            "tenant_id": self.ctx.tenant_id,
            "source_name": "test_postgres",
            "sql_query": "DROP TABLE customers",
            "async_mode": False
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/execute-raw-sql",
            headers=headers,
            json_data=sql_request,
            expect_status=400  # Should be blocked
        )
        
        assert success, "Should return 400 for dangerous SQL"
        self.logger.info("Correctly blocked DROP statement")
    
    # ========================================================================
    # CATEGORY 5: SQL EXECUTION (ASYNCHRONOUS)
    # ========================================================================
    
    def test_15_execute_sql_async_job_submission(self):
        """Test asynchronous SQL execution via job submission."""
        headers = self.TENANT_HEADER
        
        # Use a simple query that will always work
        sql_request = {
            "tenant_id": self.ctx.tenant_id,
            "source_name": "test_postgres",
            "sql_query": "SELECT 1 as id, 'async_test' as test_type, NOW() as timestamp LIMIT 10",
            "async_mode": True
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/execute-raw-sql",
            headers=headers,
            json_data=sql_request
        )
        
        assert success, f"Failed to submit async job: {error}"
        assert data is not None, "No response data"
        assert "job_id" in data, "Missing job_id"
        assert data.get("status") in ["accepted", "pending", "running", "completed"], "Invalid job status"
        
        # Store job ID for later tests
        job_id = data["job_id"]
        self.ctx.created_jobs.append(job_id)
        
        self.logger.info(f"Submitted async job: {job_id}")
        
        # Wait for job to complete
        time.sleep(2)
    
    def test_16_get_job_results(self):
        """Test retrieving job results."""
        if not self.ctx.created_jobs:
            raise AssertionError("No jobs created in previous tests")
        
        job_id = self.ctx.created_jobs[0]
        headers = self.TENANT_HEADER
        params = {"tenant_id": self.ctx.tenant_id}
        
        success, data, error = self._make_request(
            method="GET",
            endpoint=f"/api/v1/data-sources/results/{job_id}",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to get job results: {error}"
        assert data is not None, "No response data"
        assert "status" in data, "Missing status"
        
        if data["status"] == "completed":
            assert "results" in data, "Missing results for completed job"
            self.logger.info(f"Job {job_id} completed with {len(data.get('results', []))} rows")
        else:
            self.logger.warning(f"Job {job_id} status: {data['status']}")
    
    def test_17_get_paginated_results(self):
        """Test retrieving paginated job results."""
        if not self.ctx.created_jobs:
            raise AssertionError("No jobs created")
        
        job_id = self.ctx.created_jobs[0]
        headers = self.TENANT_HEADER
        params = {
            "tenant_id": self.ctx.tenant_id,
            "page": 1,
            "page_size": 5
        }
        
        success, data, error = self._make_request(
            method="GET",
            endpoint=f"/api/v1/data-sources/results/{job_id}/paginated",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to get paginated results: {error}"
        assert data is not None, "No response data"
        
        if data.get("status") == "completed":
            assert "results" in data, "Missing results"
            assert "pagination" in data, "Missing pagination info"
            pagination = data["pagination"]
            assert "page" in pagination, "Missing page number"
            assert "page_size" in pagination, "Missing page_size"
            
            self.logger.info(f"Retrieved page {pagination['page']} with {len(data['results'])} rows")
    
    # ========================================================================
    # CATEGORY 6: JOB MANAGEMENT
    # ========================================================================
    
    def test_18_submit_job_via_jobs_endpoint(self):
        """Test submitting job via dedicated /jobs endpoint."""
        headers = self.TENANT_HEADER
        
        # Get an existing table name from schema
        table_name = "action_items"  # Default to known table
        
        # Try to get schema to find a valid table
        success_schema, schema_data, _ = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/schema/test_postgres",
            headers=headers,
            params={"tenant_id": self.ctx.tenant_id}
        )
        
        if success_schema and schema_data and "schema" in schema_data:
            schema = schema_data["schema"]
            if isinstance(schema, str):
                schema = json.loads(schema)
            if isinstance(schema, list) and len(schema) > 0 and "tables" in schema[0]:
                tables = schema[0]["tables"]
                if tables:
                    table_name = tables[0].get("name") or tables[0].get("table_name") or "action_items"
        
        # Use proper AST query structure with table operation
        job_request = {
            "payload": {
                "tenant_id": self.ctx.tenant_id,
                "plan": [
                    {
                        "source": "test_postgres",
                        "query": {
                            "operation": "table",
                            "name": table_name
                        }
                    }
                ],
                "priority": 5,
                "timeout_seconds": 300
            }
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/jobs",
            headers=headers,
            json_data=job_request,
            expect_status=201
        )
        
        assert success, f"Failed to submit job: {error}"
        assert data is not None, "No response data"
        assert "job_id" in data, "Missing job_id"
        
        job_id = data["job_id"]
        self.ctx.created_jobs.append(job_id)
        self.logger.info(f"Submitted job via /jobs: {job_id} (table: {table_name})")
        
        time.sleep(1)
    
    def test_19_cancel_job(self):
        """Test cancelling a running/pending job."""
        if len(self.ctx.created_jobs) < 2:
            self.logger.warning("Not enough jobs to test cancellation")
            return
        
        job_id = self.ctx.created_jobs[-1]
        headers = self.TENANT_HEADER
        params = {"tenant_id": self.ctx.tenant_id}
        
        success, data, error = self._make_request(
            method="POST",
            endpoint=f"/api/v1/data-sources/jobs/{job_id}/cancel",
            headers=headers,
            params=params
        )
        
        # Job might already be completed, which is fine
        if success:
            self.logger.info(f"Cancelled job: {job_id}")
        else:
            self.logger.info(f"Job {job_id} might already be completed")
    
    def test_20_delete_job(self):
        """Test deleting a job."""
        if not self.ctx.created_jobs:
            raise AssertionError("No jobs to delete")
        
        job_id = self.ctx.created_jobs[-1]
        headers = self.TENANT_HEADER
        params = {"tenant_id": self.ctx.tenant_id}
        
        success, data, error = self._make_request(
            method="DELETE",
            endpoint=f"/api/v1/data-sources/jobs/{job_id}",
            headers=headers,
            params=params
        )
        
        # Job deletion might fail if job doesn't exist or is in DLQ
        if success:
            self.logger.info(f"Deleted job: {job_id}")
            self.ctx.created_jobs.remove(job_id)
        else:
            self.logger.warning(f"Failed to delete job {job_id}: {error}")
    
    # ========================================================================
    # CATEGORY 7: DEAD LETTER QUEUE (DLQ)
    # ========================================================================
    
    def test_21_list_dlq_jobs(self):
        """Test listing jobs in Dead Letter Queue."""
        params = {"tenant_id": self.ctx.tenant_id}
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/dlq",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to list DLQ jobs: {error}"
        assert data is not None, "No response data"
        assert "jobs" in data, "Missing jobs array"
        
        self.logger.info(f"DLQ contains {len(data['jobs'])} jobs")
    
    def test_22_get_dlq_job_details(self):
        """Test getting details of a DLQ job."""
        # First get list of DLQ jobs
        params = {"tenant_id": self.ctx.tenant_id}
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/dlq",
            headers=headers,
            params=params
        )
        
        if not success or not data.get("jobs"):
            self.logger.info("No DLQ jobs to test - skipping")
            return
        
        job_id = data["jobs"][0]["job_id"]
        
        success, data, error = self._make_request(
            method="GET",
            endpoint=f"/api/v1/data-sources/dlq/{job_id}",
            headers=headers,
            params=params
        )
        
        assert success, f"Failed to get DLQ job details: {error}"
        assert data is not None, "No response data"
        assert "job_id" in data, "Missing job_id"
        
        self.logger.info(f"Retrieved DLQ job details: {job_id}")
    
    # ========================================================================
    # CATEGORY 8: METRICS & MONITORING
    # ========================================================================
    
    def test_23_get_metrics_summary(self):
        """Test retrieving metrics summary."""
        success, data, error = self._make_request(
            method="GET",
            endpoint="/api/v1/data-sources/metrics"
        )
        
        assert success, f"Failed to get metrics: {error}"
        assert data is not None, "No response data"
        assert "jobs" in data, "Missing jobs metrics"
        assert "connections" in data, "Missing connections metrics"
        assert "api_keys" in data, "Missing api_keys metrics"
        
        jobs_metrics = data["jobs"]
        assert "total" in jobs_metrics, "Missing total in jobs metrics"
        assert "completed" in jobs_metrics, "Missing completed count"
        
        self.logger.info(f"Metrics - Total jobs: {jobs_metrics.get('total', 0)}")
        self.logger.info(f"Metrics - API keys created: {data['api_keys'].get('keys_created', 0)}")
    
    def test_24_cleanup_old_metrics(self):
        """Test cleaning up old metrics data."""
        params = {"days_to_keep": 7}
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/metrics/cleanup",
            params=params
        )
        
        assert success, f"Failed to cleanup metrics: {error}"
        assert data is not None, "No response data"
        assert "status" in data, "Missing status"
        assert data["status"] == "success", "Cleanup should succeed"
        
        self.logger.info(f"Cleaned up {data.get('deleted_records', 0)} old metric records")
    
    # ========================================================================
    # CATEGORY 9: PLAN CACHING (NATURAL LANGUAGE)
    # ========================================================================
    
    def test_25_create_natural_language_job(self):
        """Test creating job from natural language query with plan caching."""
        headers = self.TENANT_HEADER
        
        nl_request = {
            "payload": {
                "tenant_id": self.ctx.tenant_id,
                "user_query": "Show me a list of test data with numbers from 1 to 5",
                "use_cache": True,
                "priority": 5,
                "timeout_seconds": 300
            }
        }
        
        success, data, error = self._make_request(
            method="POST",
            endpoint="/api/v1/data-sources/jobs/natural-language",
            headers=headers,
            json_data=nl_request,
            expect_status=201
        )
        
        assert success, f"Failed to create NL job: {error}"
        assert data is not None, "No response data"
        assert "job_id" in data, "Missing job_id"
        
        self.ctx.created_jobs.append(data["job_id"])
        
        cache_status = "cache_info" in data or "cache_status" in data
        self.logger.info(f"Created NL job: {data['job_id']} (has cache info: {cache_status})")
    
    # ========================================================================
    # CLEANUP OPERATIONS
    # ========================================================================
    
    def test_99_delete_data_source(self):
        """Test deleting a data source."""
        if not self.ctx.created_sources:
            return
        
        source_name = self.ctx.created_sources[0]
        headers = self.TENANT_HEADER
        
        success, data, error = self._make_request(
            method="DELETE",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/sources/{source_name}",
            headers=headers
        )
        
        if success:
            self.logger.info(f"Deleted data source: {source_name}")
            self.ctx.created_sources.remove(source_name)
        else:
            self.logger.warning(f"Failed to delete source {source_name}: {error}")
    
    def test_100_revoke_api_key(self):
        """Test revoking the API key."""
        if not self.ctx.api_key_id:
            return
        
        headers = self.ADMIN_HEADER
        
        success, data, error = self._make_request(
            method="DELETE",
            endpoint=f"/api/v1/data-sources/tenants/{self.ctx.tenant_id}/api-keys/{self.ctx.api_key_id}",
            headers=headers
        )
        
        if success:
            self.logger.info(f"Revoked API key: {self.ctx.api_key_id}")
            self.ctx.created_api_keys.remove(self.ctx.api_key_id)
        else:
            self.logger.warning(f"Failed to revoke API key: {error}")
    
    # ========================================================================
    # TEST RUNNER
    # ========================================================================
    
    def run_all_tests(self):
        """Run all test cases in order."""
        print(f"\n{Colors.BOLD}{Colors.HEADER}{'='*70}{Colors.ENDC}")
        print(f"{Colors.BOLD}{Colors.HEADER}  Data Sources API - End-to-End Test Suite{Colors.ENDC}")
        print(f"{Colors.BOLD}{Colors.HEADER}{'='*70}{Colors.ENDC}\n")
        
        print(f"{Colors.CYAN}API URL: {self.ctx.api_url}{Colors.ENDC}")
        print(f"{Colors.CYAN}Tenant ID: {self.ctx.tenant_id}{Colors.ENDC}\n")
        
        # Collect all test methods
        test_methods = [
            (name, getattr(self, name)) 
            for name in dir(self) 
            if name.startswith('test_') and callable(getattr(self, name))
        ]
        
        # Sort by test number
        test_methods.sort()
        
        # Group tests by category
        categories = {
            "Tenant Management (API Keys)": [m for m in test_methods if m[0].startswith('test_0') and int(m[0].split('_')[1]) <= 3],
            "Data Source Configuration": [m for m in test_methods if 4 <= int(m[0].split('_')[1]) <= 7],
            "Schema Discovery": [m for m in test_methods if 8 <= int(m[0].split('_')[1]) <= 10],
            "SQL Execution (Sync)": [m for m in test_methods if 11 <= int(m[0].split('_')[1]) <= 14],
            "SQL Execution (Async)": [m for m in test_methods if 15 <= int(m[0].split('_')[1]) <= 17],
            "Job Management": [m for m in test_methods if 18 <= int(m[0].split('_')[1]) <= 20],
            "Dead Letter Queue": [m for m in test_methods if 21 <= int(m[0].split('_')[1]) <= 22],
            "Metrics & Monitoring": [m for m in test_methods if 23 <= int(m[0].split('_')[1]) <= 24],
            "Plan Caching": [m for m in test_methods if m[0].startswith('test_25')],
            "Cleanup": [m for m in test_methods if int(m[0].split('_')[1]) >= 99],
        }
        
        # Run tests by category
        for category_name, tests in categories.items():
            if not tests:
                continue
                
            print(f"\n{Colors.BOLD}{Colors.BLUE}{'─'*70}{Colors.ENDC}")
            print(f"{Colors.BOLD}{Colors.BLUE}  {category_name}{Colors.ENDC}")
            print(f"{Colors.BOLD}{Colors.BLUE}{'─'*70}{Colors.ENDC}")
            
            category_results = []
            for test_name, test_func in tests:
                result = self._run_test(test_name, test_func)
                category_results.append(result)
            
            # Store category stats
            passed = sum(1 for r in category_results if r.passed)
            failed = sum(1 for r in category_results if not r.passed)
            total_duration = sum(r.duration for r in category_results)
            
            self.ctx.category_stats[category_name] = {
                "passed": passed,
                "failed": failed,
                "total": len(category_results),
                "duration": total_duration
            }
        
        self._print_summary()
    
    def _print_summary(self):
        """Print test execution summary."""
        print(f"\n\n{Colors.BOLD}{Colors.HEADER}{'='*70}{Colors.ENDC}")
        print(f"{Colors.BOLD}{Colors.HEADER}  Test Execution Summary{Colors.ENDC}")
        print(f"{Colors.BOLD}{Colors.HEADER}{'='*70}{Colors.ENDC}\n")
        
        total_passed = sum(1 for r in self.ctx.results if r.passed)
        total_failed = sum(1 for r in self.ctx.results if not r.passed)
        total_tests = len(self.ctx.results)
        total_duration = sum(r.duration for r in self.ctx.results)
        
        print(f"{Colors.BOLD}Overall Results:{Colors.ENDC}")
        print(f"  Total Tests: {total_tests}")
        print(f"  {Colors.GREEN}✓ Passed: {total_passed}{Colors.ENDC}")
        print(f"  {Colors.RED}✗ Failed: {total_failed}{Colors.ENDC}")
        print(f"  Total Duration: {total_duration:.2f}s")
        print(f"  Success Rate: {(total_passed/total_tests*100):.1f}%\n")
        
        print(f"{Colors.BOLD}Category Breakdown:{Colors.ENDC}")
        for category, stats in self.ctx.category_stats.items():
            status_color = Colors.GREEN if stats["failed"] == 0 else Colors.YELLOW
            print(f"  {status_color}{category}:{Colors.ENDC}")
            print(f"    {stats['passed']}/{stats['total']} passed ({stats['duration']:.2f}s)")
        
        # Show failed tests
        failed_tests = [r for r in self.ctx.results if not r.passed]
        if failed_tests:
            print(f"\n{Colors.BOLD}{Colors.RED}Failed Tests:{Colors.ENDC}")
            for result in failed_tests:
                print(f"  {Colors.RED}{result.name}{Colors.ENDC}")
                if result.error:
                    print(f"    Error: {result.error}")
        
        # Show created resources
        print(f"\n{Colors.BOLD}Created Resources:{Colors.ENDC}")
        print(f"  API Keys: {len(self.ctx.created_api_keys)}")
        print(f"  Data Sources: {len(self.ctx.created_sources)}")
        print(f"  Jobs: {len(self.ctx.created_jobs)}")
        
        print(f"\n{Colors.BOLD}{Colors.HEADER}{'='*70}{Colors.ENDC}\n")
        
        # Exit with proper code
        if total_failed > 0:
            sys.exit(1)
        else:
            print(f"{Colors.GREEN}{Colors.BOLD}✓ All tests passed!{Colors.ENDC}\n")
            sys.exit(0)


# ============================================================================
# MAIN ENTRY POINT
# ============================================================================

def main():
    """Main entry point for E2E test suite."""
    parser = argparse.ArgumentParser(
        description="End-to-End API Test Suite for Data Sources API",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Run all tests with default settings
  python test_api_e2e.py
  
  # Run with custom API URL and admin key
  python test_api_e2e.py --api-url http://localhost:8000 --admin-key YOUR_KEY
  
  # Run with verbose output
  python test_api_e2e.py --verbose
  
  # Skip cleanup (useful for debugging)
  python test_api_e2e.py --no-cleanup
        """
    )
    
    parser.add_argument(
        "--api-url",
        default="http://localhost:8000",
        help="Base URL of the API server (default: http://localhost:8000)"
    )
    
    parser.add_argument(
        "--admin-key",
        default="phobosq1",
        help="Admin API key for authentication (default: phobosq1)"
    )
    
    parser.add_argument(
        "--tenant-id",
        default="test_e2e_tenant",
        help="Tenant ID to use for testing (default: test_e2e_tenant)"
    )
    
    parser.add_argument(
        "--verbose",
        action="store_true",
        help="Enable verbose output with detailed request/response logging"
    )
    
    parser.add_argument(
        "--no-cleanup",
        action="store_true",
        help="Skip cleanup operations (keep created resources)"
    )
    
    args = parser.parse_args()
    
    # Create test context
    context = TestContext(
        api_url=args.api_url,
        admin_api_key=args.admin_key,
        verbose=args.verbose,
        tenant_id=args.tenant_id
    )
    
    # Create and run test suite
    runner = E2ETestRunner(context)
    
    try:
        runner.run_all_tests()
    except KeyboardInterrupt:
        print(f"\n\n{Colors.YELLOW}Test execution interrupted by user{Colors.ENDC}\n")
        sys.exit(130)
    except Exception as e:
        print(f"\n\n{Colors.RED}Fatal error: {e}{Colors.ENDC}\n")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()