#!/usr/bin/env python3 """ Test-Skript fรผr MYP Backend-Setup รœberprรผft, ob die neue Produktions-Konfiguration korrekt funktioniert """ import os import sys import subprocess import tempfile import importlib.util def test_python_environment(): """Teste Python-Umgebung und Dependencies""" print("๐Ÿ Teste Python-Umgebung...") # Python-Version prรผfen python_version = sys.version_info print(f" Python-Version: {python_version.major}.{python_version.minor}.{python_version.micro}") if python_version < (3, 8): print(" โŒ Python-Version ist zu alt! Benรถtigt wird mindestens Python 3.8") return False print(" โœ… Python-Version ist kompatibel") return True def test_dependencies(): """Teste erforderliche Python-Pakete""" print("๐Ÿ“ฆ Teste Python-Dependencies...") required_packages = [ 'flask', 'flask_cors', 'werkzeug', 'pyjwt', 'python_dotenv', 'gunicorn' ] missing_packages = [] for package in required_packages: try: __import__(package) print(f" โœ… {package}") except ImportError: print(f" โŒ {package} fehlt") missing_packages.append(package) if missing_packages: print(f" Fehlende Pakete: {', '.join(missing_packages)}") print(" Installiere mit: pip install -r requirements.txt") return False return True def test_configuration(): """Teste Konfigurationsklassen""" print("โš™๏ธ Teste Konfiguration...") try: # Importiere Konfiguration from config import config, DevelopmentConfig, ProductionConfig, TestingConfig print(" โœ… Konfigurationsklassen importiert") # Teste verschiedene Konfigurationen dev_config = DevelopmentConfig() prod_config = ProductionConfig() test_config = TestingConfig() print(f" โœ… Development-Config: DEBUG={dev_config.DEBUG}") print(f" โœ… Production-Config: DEBUG={prod_config.DEBUG}") print(f" โœ… Testing-Config: TESTING={test_config.TESTING}") return True except Exception as e: print(f" โŒ Konfigurationsfehler: {e}") return False def test_app_factory(): """Teste Application Factory Pattern""" print("๐Ÿญ Teste Application Factory...") try: # Temporรคre Umgebungsvariablen setzen os.environ['SECRET_KEY'] = 'test_secret_key' os.environ['DATABASE_PATH'] = ':memory:' from app import create_app # Teste verschiedene Konfigurationen dev_app = create_app('development') prod_app = create_app('production') test_app = create_app('testing') print(f" โœ… Development-App: {dev_app.config['FLASK_ENV']}") print(f" โœ… Production-App: {prod_app.config['FLASK_ENV']}") print(f" โœ… Testing-App: {test_app.config['FLASK_ENV']}") return True except Exception as e: print(f" โŒ Application Factory Fehler: {e}") return False def test_database_functions(): """Teste Datenbankfunktionen""" print("๐Ÿ—„๏ธ Teste Datenbankfunktionen...") try: os.environ['SECRET_KEY'] = 'test_secret_key' os.environ['DATABASE_PATH'] = ':memory:' from app import create_app, init_db, get_db app = create_app('testing') with app.app_context(): # Initialisiere Test-Datenbank init_db() # Teste Datenbankverbindung db = get_db() result = db.execute('SELECT 1').fetchone() if result: print(" โœ… Datenbankverbindung funktioniert") print(" โœ… Tabellen wurden erstellt") return True else: print(" โŒ Datenbankverbindung fehlgeschlagen") return False except Exception as e: print(f" โŒ Datenbankfehler: {e}") return False def test_environment_variables(): """Teste Umgebungsvariablen""" print("๐ŸŒ Teste Umgebungsvariablen...") # Lade env.backend falls vorhanden if os.path.exists('env.backend'): print(" โœ… env.backend gefunden") with open('env.backend', 'r') as f: lines = f.readlines() required_vars = [ 'FLASK_APP', 'FLASK_ENV', 'SECRET_KEY', 'DATABASE_PATH' ] found_vars = [] for line in lines: if '=' in line and not line.strip().startswith('#'): var_name = line.split('=')[0].strip() if var_name in required_vars: found_vars.append(var_name) missing_vars = set(required_vars) - set(found_vars) if missing_vars: print(f" โŒ Fehlende Umgebungsvariablen: {', '.join(missing_vars)}") return False else: print(f" โœ… Alle erforderlichen Variablen gefunden: {', '.join(found_vars)}") return True else: print(" โŒ env.backend nicht gefunden") return False def test_wsgi(): """Teste WSGI-Konfiguration""" print("๐Ÿ”ง Teste WSGI-Setup...") try: from wsgi import application if application: print(" โœ… WSGI-Application erfolgreich importiert") print(f" โœ… App-Name: {application.name}") return True else: print(" โŒ WSGI-Application ist None") return False except Exception as e: print(f" โŒ WSGI-Fehler: {e}") return False def test_health_endpoint(): """Teste Health-Check-Endpoint""" print("๐Ÿฅ Teste Health-Check...") try: os.environ['SECRET_KEY'] = 'test_secret_key' os.environ['DATABASE_PATH'] = ':memory:' from app import create_app app = create_app('testing') with app.test_client() as client: response = client.get('/health') if response.status_code == 200: data = response.get_json() if data and data.get('status') == 'healthy': print(" โœ… Health-Check funktioniert") print(f" โœ… Service: {data.get('service')}") return True else: print(f" โŒ Health-Check-Antwort fehlerhaft: {data}") return False else: print(f" โŒ Health-Check fehlgeschlagen: {response.status_code}") return False except Exception as e: print(f" โŒ Health-Check-Fehler: {e}") return False def main(): """Haupttest-Funktion""" print("=" * 50) print("๐Ÿงช MYP Backend - Konfigurationstest") print("=" * 50) print() tests = [ test_python_environment, test_dependencies, test_configuration, test_app_factory, test_database_functions, test_environment_variables, test_wsgi, test_health_endpoint ] passed = 0 failed = 0 for test in tests: try: if test(): passed += 1 else: failed += 1 except Exception as e: print(f" โŒ Test-Fehler: {e}") failed += 1 print() print("=" * 50) print(f"๐Ÿ“Š Test-Ergebnisse: {passed} โœ… | {failed} โŒ") print("=" * 50) if failed == 0: print("๐ŸŽ‰ Alle Tests bestanden! Backend ist bereit.") return True else: print("โš ๏ธ Einige Tests fehlgeschlagen. Bitte Konfiguration prรผfen.") return False if __name__ == '__main__': success = main() sys.exit(0 if success else 1)