diff --git a/tests/unit/db.test.ts b/tests/unit/db.test.ts index f50d05b..8f94cfd 100644 --- a/tests/unit/db.test.ts +++ b/tests/unit/db.test.ts @@ -21,16 +21,31 @@ describe('Database', () => { }); beforeEach(() => { - // Reset database schema between tests by dropping tables and re-initializing (respect FKs) + // Reset del esquema entre tests. + // Desactivar FKs para poder dropear en cualquier orden, incluyendo tablas nuevas con FKs (p.ej., calendar_tokens). + testDb.exec('PRAGMA foreign_keys = OFF;'); + + // Tablas añadidas en migraciones posteriores (limpieza preventiva) + testDb.exec('DROP TABLE IF EXISTS calendar_tokens'); + testDb.exec('DROP TABLE IF EXISTS web_sessions'); + testDb.exec('DROP TABLE IF EXISTS web_tokens'); + testDb.exec('DROP TABLE IF EXISTS allowed_groups'); + testDb.exec('DROP TABLE IF EXISTS user_aliases'); + testDb.exec('DROP TABLE IF EXISTS user_preferences'); + + // Tablas base (dependientes primero) testDb.exec('DROP TABLE IF EXISTS task_assignments'); // Drop dependent tables first testDb.exec('DROP TABLE IF EXISTS tasks'); testDb.exec('DROP TABLE IF EXISTS response_queue'); testDb.exec('DROP TABLE IF EXISTS group_members'); testDb.exec('DROP TABLE IF EXISTS groups'); testDb.exec('DROP TABLE IF EXISTS users'); - // También reiniciar histórico de migraciones para forzar recreación de tablas + + // Reiniciar histórico de migraciones para forzar recreación íntegra testDb.exec('DROP TABLE IF EXISTS schema_migrations'); - // Initialize schema on the test database instance + + // Re-activar FKs y re-inicializar el esquema + testDb.exec('PRAGMA foreign_keys = ON;'); initializeDatabase(testDb); });