youtube-summarizer/scripts/setup_test_env.sh

247 lines
6.8 KiB
Bash
Executable File

#!/bin/bash
# Test Environment Setup Script
# Sets up the complete testing environment for YouTube Summarizer
set -e
echo "🚀 Setting up YouTube Summarizer Test Environment..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_step() {
echo -e "${BLUE}${NC} $1"
}
print_success() {
echo -e "${GREEN}${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
print_error() {
echo -e "${RED}${NC} $1"
}
# Check if running from correct directory
if [[ ! -f "backend/main.py" ]]; then
print_error "Please run this script from the youtube-summarizer root directory"
exit 1
fi
# Step 1: Verify Python version
print_step "Checking Python version..."
PYTHON_VERSION=$(python3 --version 2>&1 | cut -d" " -f2)
REQUIRED_VERSION="3.11"
if [[ $(echo "${PYTHON_VERSION}" | cut -d"." -f1-2) != "${REQUIRED_VERSION}" ]]; then
print_warning "Python ${REQUIRED_VERSION} recommended (found ${PYTHON_VERSION})"
else
print_success "Python ${PYTHON_VERSION} detected"
fi
# Step 2: Create virtual environment if it doesn't exist
print_step "Setting up virtual environment..."
if [[ ! -d "venv" ]]; then
python3 -m venv venv
print_success "Virtual environment created"
else
print_success "Virtual environment already exists"
fi
# Step 3: Activate virtual environment and install dependencies
print_step "Installing dependencies..."
source venv/bin/activate
# Install core dependencies
pip install --upgrade pip
# Install backend dependencies
if [[ -f "backend/requirements.txt" ]]; then
pip install -r backend/requirements.txt
print_success "Backend dependencies installed"
else
print_warning "backend/requirements.txt not found - installing core packages"
pip install fastapi uvicorn python-multipart
fi
# Install test dependencies
pip install pytest pytest-asyncio pytest-cov pytest-xdist pytest-timeout pytest-mock
pip install httpx # For FastAPI testing
print_success "Test dependencies installed"
# Step 4: Install frontend dependencies
print_step "Installing frontend dependencies..."
if [[ -d "frontend" ]] && [[ -f "frontend/package.json" ]]; then
cd frontend
if command -v npm >/dev/null 2>&1; then
npm install
print_success "Frontend dependencies installed"
else
print_warning "npm not found - skipping frontend setup"
fi
cd ..
else
print_warning "Frontend directory not found - skipping frontend setup"
fi
# Step 5: Create test directories and files
print_step "Setting up test structure..."
# Create test report directories
mkdir -p test_reports/coverage_html
mkdir -p test_reports/junit
mkdir -p test_reports/json
print_success "Test report directories created"
# Create test data directories
mkdir -p test_data/fixtures
mkdir -p test_data/mock_videos
mkdir -p test_data/sample_transcripts
print_success "Test data directories created"
# Step 6: Create sample test data files
print_step "Creating sample test data..."
# Create sample transcript file
cat > test_data/sample_transcripts/sample_transcript.json << 'EOF'
{
"video_id": "test123",
"title": "Sample Video",
"transcript": [
{"text": "Welcome to our sample video.", "start": 0.0, "duration": 2.5},
{"text": "This is a test transcript.", "start": 2.5, "duration": 2.0},
{"text": "Thank you for watching.", "start": 4.5, "duration": 1.5}
]
}
EOF
# Create sample test fixture
cat > test_data/fixtures/sample_video_data.py << 'EOF'
"""Sample test data for video processing tests."""
SAMPLE_YOUTUBE_URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
SAMPLE_VIDEO_ID = "dQw4w9WgXcQ"
SAMPLE_TRANSCRIPT_DATA = {
"video_id": SAMPLE_VIDEO_ID,
"title": "Sample Video Title",
"transcript": "This is a sample transcript for testing purposes."
}
SAMPLE_SUMMARY_DATA = {
"summary": "This is a sample summary.",
"key_points": ["Point 1", "Point 2", "Point 3"],
"chapters": [
{"title": "Introduction", "start_time": 0, "end_time": 30},
{"title": "Main Content", "start_time": 30, "end_time": 120}
]
}
EOF
print_success "Sample test data created"
# Step 7: Validate test runner installation
print_step "Validating test runner installation..."
# Test the test runner module
if python3 -c "from backend.test_runner.cli import TestCLI" 2>/dev/null; then
print_success "Test runner module accessible"
else
print_error "Test runner module not found - check installation"
exit 1
fi
# Step 8: Run dependency check
print_step "Checking test dependencies..."
python3 -c "
from backend.test_runner.utils.environment import check_test_dependencies
deps = check_test_dependencies()
missing = [name for name, available in deps.items() if not available]
if missing:
print('Missing dependencies:', ', '.join(missing))
exit(1)
else:
print('All test dependencies available')
" || print_warning "Some dependencies may be missing"
# Step 9: Create test configuration
print_step "Creating test configuration..."
# Create test environment file
cat > .env.test << 'EOF'
# Test Environment Configuration
DATABASE_URL=sqlite:///:memory:
TESTING=true
TEST_MODE=true
LOG_LEVEL=DEBUG
MOCK_EXTERNAL_APIS=true
ANTHROPIC_API_KEY=test_key
OPENAI_API_KEY=test_key
JWT_SECRET_KEY=test_secret_key_for_testing_only
SECRET_KEY=test_secret_key
EOF
print_success "Test configuration created"
# Step 10: Run validation tests
print_step "Running validation tests..."
# Run a quick test to verify setup
if python3 -m pytest backend/tests/unit/test_youtube_service.py::TestYouTubeService::test_extract_video_id -v 2>/dev/null; then
print_success "Test execution validated"
else
print_warning "Test execution failed - check configuration"
fi
# Step 11: Create convenience scripts
print_step "Creating convenience scripts..."
# Create test runner shortcut
cat > run_tests.sh << 'EOF'
#!/bin/bash
# Convenience script to run tests
# Activate virtual environment
source venv/bin/activate
# Set test environment
export DATABASE_URL="sqlite:///:memory:"
export TESTING=true
# Run test runner
python3 -m backend.test_runner "$@"
EOF
chmod +x run_tests.sh
print_success "Convenience scripts created"
# Final summary
echo ""
echo "🎉 Test Environment Setup Complete!"
echo ""
echo "📋 Summary:"
echo " ✅ Virtual environment: venv/"
echo " ✅ Dependencies installed"
echo " ✅ Test directories created"
echo " ✅ Sample data generated"
echo " ✅ Configuration files created"
echo ""
echo "🚀 Quick Start:"
echo " 1. Activate environment: source venv/bin/activate"
echo " 2. Run all tests: ./run_tests.sh run-all"
echo " 3. Run specific tests: ./run_tests.sh run-unit"
echo " 4. Generate coverage: ./run_tests.sh run-coverage"
echo ""
echo "📖 For detailed usage, see docs/TEST_RUNNER_GUIDE.md"
echo ""
print_success "Setup completed successfully!"