From 454b5849a989e7126997ae56c12c2bb0a9d273f4 Mon Sep 17 00:00:00 2001 From: dschlueter Date: Mon, 22 Jun 2026 13:26:20 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20Integrationstests=20isolieren=20(zerst?= =?UTF-8?q?=C3=B6rten=20die=20echte=20Datenbank)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Die Integrationstests mutierten das Modul-'app' und setzten erst in setUp die :memory:-URI. Die Engine war zu dem Zeitpunkt aber längst an die echte instance/typewriter.db gebunden, sodass db.create_all()/drop_all() gegen die echte Datei liefen -> tearDown löschte bei JEDEM Testlauf alle Tabellen der produktiven DB (Ursache der wiederkehrenden "FEHLENDE TABELLEN"-Fehler). Fix: setUp nutzt jetzt create_app({TESTING, :memory:, ...}) – eine isolierte App mit eigener Engine (wie test_routes.py). Die echte DB wird nicht mehr berührt; die Suite läuft zudem ~5x schneller (echte In-Memory statt Datei-I/O). Co-Authored-By: Claude Sonnet 4.6 --- tests/test_integration_progress.py | 21 ++++++++++++--------- tests/test_integration_statistics.py | 11 ++++++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/test_integration_progress.py b/tests/test_integration_progress.py index 7aed9b4..c69786c 100644 --- a/tests/test_integration_progress.py +++ b/tests/test_integration_progress.py @@ -5,7 +5,7 @@ import unittest import os os.environ['FLASK_TESTING'] = 'true' # Set before importing app -from app import app, db +from app import create_app, db from models import Progress, LessonProgress from services.progress_service import ProgressService @@ -15,10 +15,11 @@ class TestProgressServiceIntegration(unittest.TestCase): def setUp(self): """Setup: Test-App und In-Memory-Datenbank""" - app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' - app.config['WTF_CSRF_ENABLED'] = False - self.app = app + self.app = create_app({ + 'TESTING': True, + 'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:', + 'WTF_CSRF_ENABLED': False, + }) self.app_context = self.app.app_context() self.app_context.push() db.create_all() @@ -259,10 +260,12 @@ class TestResolveCurrentLessonIndex(unittest.TestCase): """Tests für die zentrale Lektions-Auflösung (Resume + Validierung).""" def setUp(self): - app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' - app.config['WTF_CSRF_ENABLED'] = False - self.app_context = app.app_context() + self.app = create_app({ + 'TESTING': True, + 'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:', + 'WTF_CSRF_ENABLED': False, + }) + self.app_context = self.app.app_context() self.app_context.push() db.create_all() diff --git a/tests/test_integration_statistics.py b/tests/test_integration_statistics.py index 591ab2b..8c5c94b 100644 --- a/tests/test_integration_statistics.py +++ b/tests/test_integration_statistics.py @@ -6,7 +6,7 @@ import os os.environ['FLASK_TESTING'] = 'true' # Set before importing app from datetime import timedelta, date -from app import app, db +from app import create_app, db from models import LessonStatistic, DailyPractice from services.statistics_service import StatisticsService @@ -16,10 +16,11 @@ class TestStatisticsServiceIntegration(unittest.TestCase): def setUp(self): """Setup: Test-App und In-Memory-Datenbank""" - app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' - app.config['WTF_CSRF_ENABLED'] = False - self.app = app + self.app = create_app({ + 'TESTING': True, + 'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:', + 'WTF_CSRF_ENABLED': False, + }) self.app_context = self.app.app_context() self.app_context.push() db.create_all()