File size: 9,335 Bytes
bc18e51
66e45b5
bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66e45b5
bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Master Test Runner for DanceDynamics
Runs all test suites and generates comprehensive reports
"""

import subprocess
import sys
import os
from pathlib import Path
import time
import json


class TestRunner:
    """Orchestrate all test suites"""
    
    def __init__(self):
        self.results = {}
        self.start_time = None
        self.end_time = None
    
    def print_header(self, title):
        """Print formatted header"""
        print("\n" + "="*70)
        print(f"  {title}")
        print("="*70 + "\n")
    
    def run_command(self, command, description):
        """Run a shell command and capture output"""
        print(f"πŸ”„ {description}...")
        
        try:
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                timeout=300  # 5 minute timeout
            )
            
            success = result.returncode == 0
            
            if success:
                print(f"βœ… {description} - PASSED\n")
            else:
                print(f"❌ {description} - FAILED\n")
                if result.stderr:
                    print(f"Error output:\n{result.stderr[:500]}\n")
            
            return {
                'success': success,
                'returncode': result.returncode,
                'stdout': result.stdout,
                'stderr': result.stderr
            }
            
        except subprocess.TimeoutExpired:
            print(f"⏱️ {description} - TIMEOUT\n")
            return {
                'success': False,
                'returncode': -1,
                'stdout': '',
                'stderr': 'Test timeout after 5 minutes'
            }
        except Exception as e:
            print(f"❌ {description} - ERROR: {str(e)}\n")
            return {
                'success': False,
                'returncode': -1,
                'stdout': '',
                'stderr': str(e)
            }
    
    def run_unit_tests(self):
        """Run unit tests from Phase 1 & 2"""
        self.print_header("PHASE 1 & 2: Unit Tests")
        
        # Pose analyzer tests
        result1 = self.run_command(
            "pytest tests/test_pose_analyzer.py -v --tb=short",
            "Pose Analyzer Tests"
        )
        self.results['pose_analyzer_tests'] = result1
        
        # Movement classifier tests
        result2 = self.run_command(
            "pytest tests/test_movement_classifier.py -v --tb=short",
            "Movement Classifier Tests"
        )
        self.results['movement_classifier_tests'] = result2
        
        return result1['success'] and result2['success']
    
    def run_api_tests(self):
        """Run API tests from Phase 3"""
        self.print_header("PHASE 3: API Tests")
        
        result = self.run_command(
            "pytest tests/test_api.py -v --tb=short",
            "API Endpoint Tests"
        )
        self.results['api_tests'] = result
        
        return result['success']
    
    def run_integration_tests(self):
        """Run integration tests from Phase 5"""
        self.print_header("PHASE 5: Integration Tests")
        
        result = self.run_command(
            "pytest tests/test_integration.py -v --tb=short",
            "Integration Tests"
        )
        self.results['integration_tests'] = result
        
        return result['success']
    
    def run_coverage_report(self):
        """Generate code coverage report"""
        self.print_header("Code Coverage Analysis")
        
        result = self.run_command(
            "pytest tests/ --cov=app --cov-report=html --cov-report=term",
            "Coverage Analysis"
        )
        self.results['coverage'] = result
        
        if result['success']:
            print("πŸ“Š Coverage report generated: htmlcov/index.html")
        
        return result['success']
    
    def check_code_quality(self):
        """Check code quality with flake8 (optional)"""
        self.print_header("Code Quality Check")
        
        # Check if flake8 is installed
        try:
            subprocess.run(['flake8', '--version'], capture_output=True, check=True)
            has_flake8 = True
        except:
            has_flake8 = False
        
        if has_flake8:
            result = self.run_command(
                "flake8 app/ --max-line-length=100 --ignore=E501,W503",
                "Code Quality (flake8)"
            )
            self.results['code_quality'] = result
        else:
            print("⚠️  flake8 not installed - skipping code quality check")
            print("   Install with: pip install flake8\n")
            self.results['code_quality'] = {'success': True, 'skipped': True}
    
    def generate_summary(self):
        """Generate test summary"""
        self.print_header("TEST SUMMARY")
        
        total_tests = len([k for k in self.results.keys() if not k.endswith('_skipped')])
        passed_tests = sum(1 for v in self.results.values() if v.get('success', False))
        failed_tests = total_tests - passed_tests
        
        print(f"πŸ“Š Total Test Suites: {total_tests}")
        print(f"βœ… Passed: {passed_tests}")
        print(f"❌ Failed: {failed_tests}")
        print(f"πŸ“ˆ Success Rate: {(passed_tests/total_tests*100):.1f}%\n")
        
        # Individual results
        status_emoji = {True: "βœ…", False: "❌"}
        
        print("Detailed Results:")
        print("-" * 50)
        for test_name, result in self.results.items():
            if result.get('skipped'):
                print(f"⚠️  {test_name}: SKIPPED")
            else:
                status = status_emoji.get(result.get('success', False), "❓")
                print(f"{status} {test_name}: {'PASSED' if result.get('success') else 'FAILED'}")
        
        print("\n" + "-" * 50)
        
        # Execution time
        if self.start_time and self.end_time:
            duration = self.end_time - self.start_time
            print(f"\n⏱️  Total Execution Time: {duration:.2f} seconds")
        
        # Overall status
        all_passed = failed_tests == 0
        print("\n" + "="*70)
        if all_passed:
            print("πŸŽ‰ ALL TESTS PASSED! System is production-ready.")
        else:
            print(f"⚠️  {failed_tests} test suite(s) failed. Review logs above.")
        print("="*70 + "\n")
        
        return all_passed
    
    def save_report(self, filename="test_report.json"):
        """Save detailed test report to JSON"""
        report = {
            'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
            'execution_time': self.end_time - self.start_time if self.start_time else 0,
            'results': {}
        }
        
        for test_name, result in self.results.items():
            report['results'][test_name] = {
                'success': result.get('success', False),
                'skipped': result.get('skipped', False),
                'returncode': result.get('returncode', 0)
            }
        
        with open(filename, 'w') as f:
            json.dump(report, f, indent=2)
        
        print(f"πŸ“„ Detailed report saved to {filename}\n")
    
    def run_all(self):
        """Run all test suites"""
        print("\n" + "="*70)
        print("  πŸ§ͺ DanceDynamics - COMPREHENSIVE TEST SUITE")
        print("="*70)
        
        self.start_time = time.time()
        
        # Check if we're in the right directory
        if not Path("app").exists():
            print("\n❌ Error: 'app' directory not found.")
            print("   Please run this script from the backend/ directory.\n")
            return False
        
        # Check if tests directory exists
        if not Path("tests").exists():
            print("\n❌ Error: 'tests' directory not found.")
            print("   Please ensure test files are in the tests/ directory.\n")
            return False
        
        print(f"\nπŸ” Test environment:")
        print(f"   Python: {sys.version.split()[0]}")
        print(f"   Working directory: {os.getcwd()}")
        print(f"   Test directory: {Path('tests').absolute()}")
        
        # Run all test suites
        results = []
        
        # Phase 1 & 2: Unit Tests
        results.append(self.run_unit_tests())
        
        # Phase 3: API Tests
        results.append(self.run_api_tests())
        
        # Phase 5: Integration Tests
        results.append(self.run_integration_tests())
        
        # Coverage Report
        self.run_coverage_report()
        
        # Code Quality (optional)
        self.check_code_quality()
        
        self.end_time = time.time()
        
        # Generate summary
        all_passed = self.generate_summary()
        
        # Save detailed report
        self.save_report()
        
        return all_passed


def main():
    """Main entry point"""
    
    # Change to backend directory if not already there
    if Path("backend").exists() and not Path("app").exists():
        os.chdir("backend")
        print("πŸ“ Changed directory to: backend/")
    
    runner = TestRunner()
    success = runner.run_all()
    
    # Exit with appropriate code
    sys.exit(0 if success else 1)


if __name__ == "__main__":
    main()