Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test runner for comprehensive user authentication tests | |
| This script runs all the user authentication tests in the correct order | |
| and provides a comprehensive report of the test results. | |
| """ | |
| import sys | |
| import os | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| # Load environment variables | |
| try: | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| print("β Environment variables loaded") | |
| except ImportError: | |
| print("β οΈ dotenv not available - continuing without .env file loading") | |
| import asyncio | |
| import time | |
| from datetime import datetime | |
| import traceback | |
| async def run_test_suite(test_name: str, test_function): | |
| """Run a test suite and capture results""" | |
| print(f"\n{'='*60}") | |
| print(f"π§ͺ RUNNING: {test_name}") | |
| print(f"{'='*60}") | |
| start_time = time.time() | |
| try: | |
| await test_function() | |
| end_time = time.time() | |
| duration = end_time - start_time | |
| print(f"\nβ {test_name} PASSED ({duration:.2f}s)") | |
| return True, duration, None | |
| except Exception as e: | |
| end_time = time.time() | |
| duration = end_time - start_time | |
| error_msg = str(e) | |
| print(f"\nβ {test_name} FAILED ({duration:.2f}s)") | |
| print(f"Error: {error_msg}") | |
| print("\nFull traceback:") | |
| traceback.print_exc() | |
| return False, duration, error_msg | |
| async def main(): | |
| """Run all user authentication tests""" | |
| print("π COMPREHENSIVE USER AUTHENTICATION TEST SUITE") | |
| print("=" * 60) | |
| print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| print("=" * 60) | |
| # Test suites to run | |
| test_suites = [] | |
| # 1. Unit tests for user_id validation | |
| try: | |
| from test_user_id_validation import run_validation_tests | |
| test_suites.append(("User ID Validation Tests", run_validation_tests)) | |
| except ImportError as e: | |
| print(f"β οΈ Could not import validation tests: {e}") | |
| # 2. Integration tests for chat requests | |
| try: | |
| from test_chat_integration_user_auth import run_integration_tests | |
| test_suites.append(("Chat Integration Tests", run_integration_tests)) | |
| except ImportError as e: | |
| print(f"β οΈ Could not import integration tests: {e}") | |
| # 3. Backward compatibility tests | |
| try: | |
| from test_backward_compatibility import run_compatibility_tests | |
| test_suites.append(("Backward Compatibility Tests", run_compatibility_tests)) | |
| except ImportError as e: | |
| print(f"β οΈ Could not import compatibility tests: {e}") | |
| # 4. Performance tests | |
| try: | |
| from test_performance_user_auth import run_performance_tests | |
| test_suites.append(("Performance Tests", run_performance_tests)) | |
| except ImportError as e: | |
| print(f"β οΈ Could not import performance tests: {e}") | |
| # 5. Comprehensive tests | |
| try: | |
| from test_user_authentication_comprehensive import ( | |
| run_unit_tests, | |
| run_analytics_tests, | |
| run_compatibility_tests as run_comp_tests | |
| ) | |
| test_suites.append(("Comprehensive Unit Tests", run_unit_tests)) | |
| test_suites.append(("Analytics Function Tests", run_analytics_tests)) | |
| test_suites.append(("Comprehensive Compatibility Tests", run_comp_tests)) | |
| except ImportError as e: | |
| print(f"β οΈ Could not import comprehensive tests: {e}") | |
| if not test_suites: | |
| print("β No test suites could be imported!") | |
| return False | |
| # Run all test suites | |
| results = [] | |
| total_start_time = time.time() | |
| for test_name, test_function in test_suites: | |
| # Convert sync functions to async if needed | |
| if asyncio.iscoroutinefunction(test_function): | |
| success, duration, error = await run_test_suite(test_name, test_function) | |
| else: | |
| # Wrap sync function in async | |
| async def async_wrapper(): | |
| test_function() | |
| success, duration, error = await run_test_suite(test_name, async_wrapper) | |
| results.append({ | |
| 'name': test_name, | |
| 'success': success, | |
| 'duration': duration, | |
| 'error': error | |
| }) | |
| total_duration = time.time() - total_start_time | |
| # Print summary report | |
| print("\n" + "="*60) | |
| print("π TEST SUMMARY REPORT") | |
| print("="*60) | |
| passed_tests = [r for r in results if r['success']] | |
| failed_tests = [r for r in results if not r['success']] | |
| print(f"Total test suites: {len(results)}") | |
| print(f"Passed: {len(passed_tests)}") | |
| print(f"Failed: {len(failed_tests)}") | |
| print(f"Total duration: {total_duration:.2f} seconds") | |
| print() | |
| # Detailed results | |
| for result in results: | |
| status = "β PASS" if result['success'] else "β FAIL" | |
| print(f"{status} {result['name']} ({result['duration']:.2f}s)") | |
| if result['error']: | |
| print(f" Error: {result['error']}") | |
| print("\n" + "="*60) | |
| if failed_tests: | |
| print("β SOME TESTS FAILED") | |
| print("\nFailed test suites:") | |
| for result in failed_tests: | |
| print(f" - {result['name']}: {result['error']}") | |
| print("\nπ§ TROUBLESHOOTING TIPS:") | |
| print("1. Ensure the server is running on localhost:7860 for integration tests") | |
| print("2. Check that MongoDB is accessible for database tests") | |
| print("3. Verify all dependencies are installed") | |
| print("4. Check that analytics modules are properly imported") | |
| return False | |
| else: | |
| print("π ALL TESTS PASSED!") | |
| print("\n⨠User authentication feature is working correctly!") | |
| print(" - User ID validation is robust") | |
| print(" - Chat integration works with and without user_id") | |
| print(" - Backward compatibility is maintained") | |
| print(" - Performance is acceptable") | |
| print(" - Analytics functions work correctly") | |
| return True | |
| def run_specific_test_suite(suite_name: str): | |
| """Run a specific test suite by name""" | |
| test_mapping = { | |
| 'validation': 'test_user_id_validation.run_validation_tests', | |
| 'integration': 'test_chat_integration_user_auth.run_integration_tests', | |
| 'compatibility': 'test_backward_compatibility.run_compatibility_tests', | |
| 'performance': 'test_performance_user_auth.run_performance_tests', | |
| 'comprehensive': 'test_user_authentication_comprehensive.main' | |
| } | |
| if suite_name not in test_mapping: | |
| print(f"β Unknown test suite: {suite_name}") | |
| print(f"Available suites: {', '.join(test_mapping.keys())}") | |
| return False | |
| module_path = test_mapping[suite_name] | |
| module_name, function_name = module_path.rsplit('.', 1) | |
| try: | |
| module = __import__(module_name, fromlist=[function_name]) | |
| test_function = getattr(module, function_name) | |
| if asyncio.iscoroutinefunction(test_function): | |
| return asyncio.run(test_function()) | |
| else: | |
| test_function() | |
| return True | |
| except Exception as e: | |
| print(f"β Failed to run {suite_name} tests: {e}") | |
| traceback.print_exc() | |
| return False | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| # Run specific test suite | |
| suite_name = sys.argv[1].lower() | |
| success = run_specific_test_suite(suite_name) | |
| else: | |
| # Run all tests | |
| success = asyncio.run(main()) | |
| sys.exit(0 if success else 1) |