#!/bin/bash # SQLite Test Script # This script sets up SQLite environment and runs the test suite set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration SQLITE_DB_DEV="showcase_dev.db" SQLITE_DB_TEST="showcase_test.db" BACKUP_DIR="./db_backups" # Function to print colored output print_info() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to check if .env.dev exists check_env_file() { if [[ ! -f ".env.dev" ]]; then print_warning ".env.dev not found. Creating default development environment file..." cat > .env.dev << EOF DATABASE_URL=sqlite://showcase_dev.db EXTERNAL_BASE=http://localhost:8080 BADGE_ISSUERS=did:plc:test1;did:plc:test2 HTTP_PORT=8080 BADGE_IMAGE_STORAGE=./badges PLC_HOSTNAME=plc.directory HTTP_CLIENT_TIMEOUT=10s RUST_LOG=showcase=info,debug EOF print_success "Created .env.dev file" fi } # Function to backup existing databases backup_databases() { local backup_needed=false if [[ -f "$SQLITE_DB_DEV" ]] || [[ -f "$SQLITE_DB_TEST" ]]; then backup_needed=true fi if [[ "$backup_needed" == "true" ]]; then print_info "Backing up existing databases..." mkdir -p "$BACKUP_DIR" local timestamp=$(date +"%Y%m%d_%H%M%S") if [[ -f "$SQLITE_DB_DEV" ]]; then cp "$SQLITE_DB_DEV" "$BACKUP_DIR/showcase_dev_${timestamp}.db" print_success "Backed up $SQLITE_DB_DEV" fi if [[ -f "$SQLITE_DB_TEST" ]]; then cp "$SQLITE_DB_TEST" "$BACKUP_DIR/showcase_test_${timestamp}.db" print_success "Backed up $SQLITE_DB_TEST" fi fi } # Function to clean up test databases cleanup_databases() { local force_clean=$1 if [[ "$force_clean" == "true" ]] || [[ "$CLEAN_START" == "true" ]]; then print_info "Cleaning up test databases..." # Remove test databases rm -f "$SQLITE_DB_DEV" "$SQLITE_DB_TEST" # Remove test directories rm -rf ./badges ./test_badges print_success "Database cleanup completed" fi } # Function to load environment variables load_environment() { print_info "Loading SQLite environment variables..." if [[ -f ".env.dev" ]]; then export $(cat .env.dev | grep -v '^#' | xargs) print_success "Environment variables loaded" # Create badge storage directory mkdir -p "$(dirname "${BADGE_IMAGE_STORAGE:-./badges}")" else print_error ".env.dev file not found" exit 1 fi } # Function to verify SQLite setup verify_sqlite() { print_info "Verifying SQLite setup..." # Check if sqlite3 is available (optional) if command -v sqlite3 &> /dev/null; then local sqlite_version=$(sqlite3 --version | cut -d' ' -f1) print_success "SQLite3 CLI available: version $sqlite_version" else print_info "SQLite3 CLI not available (not required for tests)" fi # Verify we can create a test database if cargo check > /dev/null 2>&1; then print_success "Cargo build verification passed" else print_error "Cargo build verification failed" return 1 fi } # Function to run tests run_tests() { print_info "Running tests with SQLite backend..." # Ensure badge storage directory exists mkdir -p "${BADGE_IMAGE_STORAGE:-./badges}" # Run the tests if cargo test "$@"; then print_success "All tests passed!" return 0 else print_error "Some tests failed" return 1 fi } # Function to show database statistics show_db_stats() { if [[ -f "$SQLITE_DB_DEV" ]] && command -v sqlite3 &> /dev/null; then print_info "Database statistics:" local db_size=$(du -h "$SQLITE_DB_DEV" | cut -f1) print_info "Database file size: $db_size" # Show table information if database exists and has tables local table_count=$(sqlite3 "$SQLITE_DB_DEV" "SELECT COUNT(*) FROM sqlite_master WHERE type='table';" 2>/dev/null || echo "0") if [[ "$table_count" -gt 0 ]]; then print_info "Number of tables: $table_count" # Show table names and row counts sqlite3 "$SQLITE_DB_DEV" ".tables" 2>/dev/null | while read table; do if [[ -n "$table" ]]; then local row_count=$(sqlite3 "$SQLITE_DB_DEV" "SELECT COUNT(*) FROM $table;" 2>/dev/null || echo "N/A") print_info " Table '$table': $row_count rows" fi done else print_info "No tables found in database" fi fi } # Function to optimize SQLite database optimize_database() { if [[ -f "$SQLITE_DB_DEV" ]] && command -v sqlite3 &> /dev/null; then print_info "Optimizing SQLite database..." # Run VACUUM to optimize database sqlite3 "$SQLITE_DB_DEV" "VACUUM;" 2>/dev/null || print_warning "Could not vacuum database" # Analyze database for query optimizer sqlite3 "$SQLITE_DB_DEV" "ANALYZE;" 2>/dev/null || print_warning "Could not analyze database" print_success "Database optimization completed" fi } # Function to show usage usage() { echo "Usage: $0 [OPTIONS] [TEST_ARGS]" echo "" echo "Options:" echo " -h, --help Show this help message" echo " -c, --clean Clean up databases before starting" echo " -b, --backup Backup existing databases before cleaning" echo " -s, --stats Show database statistics after tests" echo " -o, --optimize Optimize database after tests" echo " -v, --verbose Enable verbose output" echo "" echo "Examples:" echo " $0 # Run all tests" echo " $0 storage::tests # Run only storage tests" echo " $0 --clean --stats # Clean start and show stats" echo " $0 --backup --clean # Backup before clean start" echo " $0 -- --nocapture # Pass --nocapture to cargo test" } # Parse command line arguments CLEAN_START=false BACKUP_FIRST=false SHOW_STATS=false OPTIMIZE_DB=false VERBOSE=false TEST_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -h|--help) usage exit 0 ;; -c|--clean) CLEAN_START=true shift ;; -b|--backup) BACKUP_FIRST=true shift ;; -s|--stats) SHOW_STATS=true shift ;; -o|--optimize) OPTIMIZE_DB=true shift ;; -v|--verbose) VERBOSE=true shift ;; --) shift TEST_ARGS+=("$@") break ;; *) TEST_ARGS+=("$1") shift ;; esac done # Enable verbose output if requested if [[ "$VERBOSE" == "true" ]]; then set -x fi # Main execution main() { print_info "Starting SQLite tests..." # Pre-flight checks check_env_file # Backup if requested if [[ "$BACKUP_FIRST" == "true" ]]; then backup_databases fi # Clean up if requested if [[ "$CLEAN_START" == "true" ]]; then cleanup_databases true fi # Setup load_environment verify_sqlite # Run tests local test_result=0 run_tests "${TEST_ARGS[@]}" || test_result=$? # Post-test operations if [[ "$SHOW_STATS" == "true" ]]; then show_db_stats fi if [[ "$OPTIMIZE_DB" == "true" ]]; then optimize_database fi # Final cleanup of test artifacts rm -f "$SQLITE_DB_TEST" if [[ $test_result -eq 0 ]]; then print_success "SQLite tests completed successfully!" else print_error "SQLite tests failed!" exit $test_result fi } # Run main function main "$@"