274 lines
7.9 KiB
Python
274 lines
7.9 KiB
Python
#!/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) |