#!/bin/bash # Script para ejecutar tests de integración de ServiceManagerWeb # Este script configura el ambiente de testing y ejecuta la suite completa set -e # Exit on error echo "🧪 ServiceManagerWeb - Test Runner" echo "==================================" echo "" # Colores para output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Verificar que estamos en el directorio correcto if [ ! -f "requirements.txt" ]; then echo -e "${RED}❌ Error: Debe ejecutar este script desde el directorio backend/${NC}" exit 1 fi # Verificar que existe la BD de test echo "📦 Verificando base de datos de testing..." if ! docker-compose exec -T postgres psql -U servicemanager -lqt | cut -d \| -f 1 | grep -qw servicemanager_test; then echo "⚙️ Creando base de datos de testing..." docker-compose exec -T postgres psql -U servicemanager -c "CREATE DATABASE servicemanager_test;" 2>/dev/null || true fi echo -e "${GREEN}✓ Base de datos lista${NC}" echo "" # Verificar que los servicios estén corriendo echo "🐳 Verificando servicios Docker..." if ! docker-compose ps | grep -q "Up"; then echo -e "${YELLOW}⚠️ Servicios no están corriendo. Iniciando...${NC}" docker-compose up -d postgres redis sleep 5 fi echo -e "${GREEN}✓ Servicios activos${NC}" echo "" # Configuración de tests export TESTING=true export DATABASE_URL="postgresql+asyncpg://servicemanager:servicemanager123@localhost:5432/servicemanager_test" # Opciones de pytest PYTEST_ARGS="-v --tb=short --color=yes" # Parsear argumentos case "${1:-all}" in auth) echo "🔐 Ejecutando tests de autenticación..." pytest $PYTEST_ARGS tests/integration/test_auth_integration.py ;; multitenant) echo "🏢 Ejecutando tests de multi-tenancy..." pytest $PYTEST_ARGS tests/integration/test_multitenant_integration.py ;; tickets) echo "🎫 Ejecutando tests de tickets..." pytest $PYTEST_ARGS tests/integration/test_tickets_integration.py ;; integration) echo "🔗 Ejecutando todos los tests de integración..." pytest $PYTEST_ARGS tests/integration/ ;; unit) echo "⚡ Ejecutando tests unitarios..." pytest $PYTEST_ARGS tests/unit/ ;; coverage) echo "📊 Ejecutando tests con cobertura..." pytest $PYTEST_ARGS --cov=app --cov-report=html --cov-report=term tests/integration/ tests/unit/ echo "" echo -e "${GREEN}✓ Reporte de cobertura generado en htmlcov/index.html${NC}" ;; all) echo "🎯 Ejecutando suite completa de tests..." pytest $PYTEST_ARGS tests/unit/ tests/integration/ ;; clean) echo "🧹 Limpiando base de datos de testing..." docker-compose exec -T postgres psql -U servicemanager -c "DROP DATABASE IF EXISTS servicemanager_test;" docker-compose exec -T postgres psql -U servicemanager -c "CREATE DATABASE servicemanager_test;" echo -e "${GREEN}✓ Base de datos limpia${NC}" ;; *) echo "Uso: $0 [auth|multitenant|tickets|integration|unit|coverage|all|clean]" echo "" echo "Opciones:" echo " auth - Tests de autenticación" echo " multitenant - Tests de aislamiento multi-tenant" echo " tickets - Tests CRUD de tickets" echo " integration - Todos los tests de integración" echo " unit - Tests unitarios" echo " coverage - Tests con reporte de cobertura" echo " all - Todos los tests (default)" echo " clean - Limpiar base de datos de testing" exit 1 ;; esac # Mostrar resultado if [ $? -eq 0 ]; then echo "" echo -e "${GREEN}✅ Tests completados exitosamente${NC}" exit 0 else echo "" echo -e "${RED}❌ Algunos tests fallaron${NC}" exit 1 fi