A Rust application to showcase badge awards in the AT Protocol ecosystem.
1#!/bin/bash
2# SQLite Test Script
3# This script sets up SQLite environment and runs the test suite
4
5set -e
6
7# Colors for output
8RED='\033[0;31m'
9GREEN='\033[0;32m'
10YELLOW='\033[1;33m'
11BLUE='\033[0;34m'
12NC='\033[0m' # No Color
13
14# Configuration
15SQLITE_DB_DEV="showcase_dev.db"
16SQLITE_DB_TEST="showcase_test.db"
17BACKUP_DIR="./db_backups"
18
19# Function to print colored output
20print_info() {
21 echo -e "${BLUE}[INFO]${NC} $1"
22}
23
24print_success() {
25 echo -e "${GREEN}[SUCCESS]${NC} $1"
26}
27
28print_warning() {
29 echo -e "${YELLOW}[WARNING]${NC} $1"
30}
31
32print_error() {
33 echo -e "${RED}[ERROR]${NC} $1"
34}
35
36# Function to check if .env.dev exists
37check_env_file() {
38 if [[ ! -f ".env.dev" ]]; then
39 print_warning ".env.dev not found. Creating default development environment file..."
40 cat > .env.dev << EOF
41DATABASE_URL=sqlite://showcase_dev.db
42EXTERNAL_BASE=http://localhost:8080
43BADGE_ISSUERS=did:plc:test1;did:plc:test2
44HTTP_PORT=8080
45BADGE_IMAGE_STORAGE=./badges
46PLC_HOSTNAME=plc.directory
47HTTP_CLIENT_TIMEOUT=10s
48RUST_LOG=showcase=info,debug
49EOF
50 print_success "Created .env.dev file"
51 fi
52}
53
54# Function to backup existing databases
55backup_databases() {
56 local backup_needed=false
57
58 if [[ -f "$SQLITE_DB_DEV" ]] || [[ -f "$SQLITE_DB_TEST" ]]; then
59 backup_needed=true
60 fi
61
62 if [[ "$backup_needed" == "true" ]]; then
63 print_info "Backing up existing databases..."
64 mkdir -p "$BACKUP_DIR"
65
66 local timestamp=$(date +"%Y%m%d_%H%M%S")
67
68 if [[ -f "$SQLITE_DB_DEV" ]]; then
69 cp "$SQLITE_DB_DEV" "$BACKUP_DIR/showcase_dev_${timestamp}.db"
70 print_success "Backed up $SQLITE_DB_DEV"
71 fi
72
73 if [[ -f "$SQLITE_DB_TEST" ]]; then
74 cp "$SQLITE_DB_TEST" "$BACKUP_DIR/showcase_test_${timestamp}.db"
75 print_success "Backed up $SQLITE_DB_TEST"
76 fi
77 fi
78}
79
80# Function to clean up test databases
81cleanup_databases() {
82 local force_clean=$1
83
84 if [[ "$force_clean" == "true" ]] || [[ "$CLEAN_START" == "true" ]]; then
85 print_info "Cleaning up test databases..."
86
87 # Remove test databases
88 rm -f "$SQLITE_DB_DEV" "$SQLITE_DB_TEST"
89
90 # Remove test directories
91 rm -rf ./badges ./test_badges
92
93 print_success "Database cleanup completed"
94 fi
95}
96
97# Function to load environment variables
98load_environment() {
99 print_info "Loading SQLite environment variables..."
100
101 if [[ -f ".env.dev" ]]; then
102 export $(cat .env.dev | grep -v '^#' | xargs)
103 print_success "Environment variables loaded"
104
105 # Create badge storage directory
106 mkdir -p "$(dirname "${BADGE_IMAGE_STORAGE:-./badges}")"
107 else
108 print_error ".env.dev file not found"
109 exit 1
110 fi
111}
112
113# Function to verify SQLite setup
114verify_sqlite() {
115 print_info "Verifying SQLite setup..."
116
117 # Check if sqlite3 is available (optional)
118 if command -v sqlite3 &> /dev/null; then
119 local sqlite_version=$(sqlite3 --version | cut -d' ' -f1)
120 print_success "SQLite3 CLI available: version $sqlite_version"
121 else
122 print_info "SQLite3 CLI not available (not required for tests)"
123 fi
124
125 # Verify we can create a test database
126 if cargo check > /dev/null 2>&1; then
127 print_success "Cargo build verification passed"
128 else
129 print_error "Cargo build verification failed"
130 return 1
131 fi
132}
133
134# Function to run tests
135run_tests() {
136 print_info "Running tests with SQLite backend..."
137
138 # Ensure badge storage directory exists
139 mkdir -p "${BADGE_IMAGE_STORAGE:-./badges}"
140
141 # Run the tests
142 if cargo test "$@"; then
143 print_success "All tests passed!"
144 return 0
145 else
146 print_error "Some tests failed"
147 return 1
148 fi
149}
150
151# Function to show database statistics
152show_db_stats() {
153 if [[ -f "$SQLITE_DB_DEV" ]] && command -v sqlite3 &> /dev/null; then
154 print_info "Database statistics:"
155
156 local db_size=$(du -h "$SQLITE_DB_DEV" | cut -f1)
157 print_info "Database file size: $db_size"
158
159 # Show table information if database exists and has tables
160 local table_count=$(sqlite3 "$SQLITE_DB_DEV" "SELECT COUNT(*) FROM sqlite_master WHERE type='table';" 2>/dev/null || echo "0")
161 if [[ "$table_count" -gt 0 ]]; then
162 print_info "Number of tables: $table_count"
163
164 # Show table names and row counts
165 sqlite3 "$SQLITE_DB_DEV" ".tables" 2>/dev/null | while read table; do
166 if [[ -n "$table" ]]; then
167 local row_count=$(sqlite3 "$SQLITE_DB_DEV" "SELECT COUNT(*) FROM $table;" 2>/dev/null || echo "N/A")
168 print_info " Table '$table': $row_count rows"
169 fi
170 done
171 else
172 print_info "No tables found in database"
173 fi
174 fi
175}
176
177# Function to optimize SQLite database
178optimize_database() {
179 if [[ -f "$SQLITE_DB_DEV" ]] && command -v sqlite3 &> /dev/null; then
180 print_info "Optimizing SQLite database..."
181
182 # Run VACUUM to optimize database
183 sqlite3 "$SQLITE_DB_DEV" "VACUUM;" 2>/dev/null || print_warning "Could not vacuum database"
184
185 # Analyze database for query optimizer
186 sqlite3 "$SQLITE_DB_DEV" "ANALYZE;" 2>/dev/null || print_warning "Could not analyze database"
187
188 print_success "Database optimization completed"
189 fi
190}
191
192# Function to show usage
193usage() {
194 echo "Usage: $0 [OPTIONS] [TEST_ARGS]"
195 echo ""
196 echo "Options:"
197 echo " -h, --help Show this help message"
198 echo " -c, --clean Clean up databases before starting"
199 echo " -b, --backup Backup existing databases before cleaning"
200 echo " -s, --stats Show database statistics after tests"
201 echo " -o, --optimize Optimize database after tests"
202 echo " -v, --verbose Enable verbose output"
203 echo ""
204 echo "Examples:"
205 echo " $0 # Run all tests"
206 echo " $0 storage::tests # Run only storage tests"
207 echo " $0 --clean --stats # Clean start and show stats"
208 echo " $0 --backup --clean # Backup before clean start"
209 echo " $0 -- --nocapture # Pass --nocapture to cargo test"
210}
211
212# Parse command line arguments
213CLEAN_START=false
214BACKUP_FIRST=false
215SHOW_STATS=false
216OPTIMIZE_DB=false
217VERBOSE=false
218TEST_ARGS=()
219
220while [[ $# -gt 0 ]]; do
221 case $1 in
222 -h|--help)
223 usage
224 exit 0
225 ;;
226 -c|--clean)
227 CLEAN_START=true
228 shift
229 ;;
230 -b|--backup)
231 BACKUP_FIRST=true
232 shift
233 ;;
234 -s|--stats)
235 SHOW_STATS=true
236 shift
237 ;;
238 -o|--optimize)
239 OPTIMIZE_DB=true
240 shift
241 ;;
242 -v|--verbose)
243 VERBOSE=true
244 shift
245 ;;
246 --)
247 shift
248 TEST_ARGS+=("$@")
249 break
250 ;;
251 *)
252 TEST_ARGS+=("$1")
253 shift
254 ;;
255 esac
256done
257
258# Enable verbose output if requested
259if [[ "$VERBOSE" == "true" ]]; then
260 set -x
261fi
262
263# Main execution
264main() {
265 print_info "Starting SQLite tests..."
266
267 # Pre-flight checks
268 check_env_file
269
270 # Backup if requested
271 if [[ "$BACKUP_FIRST" == "true" ]]; then
272 backup_databases
273 fi
274
275 # Clean up if requested
276 if [[ "$CLEAN_START" == "true" ]]; then
277 cleanup_databases true
278 fi
279
280 # Setup
281 load_environment
282 verify_sqlite
283
284 # Run tests
285 local test_result=0
286 run_tests "${TEST_ARGS[@]}" || test_result=$?
287
288 # Post-test operations
289 if [[ "$SHOW_STATS" == "true" ]]; then
290 show_db_stats
291 fi
292
293 if [[ "$OPTIMIZE_DB" == "true" ]]; then
294 optimize_database
295 fi
296
297 # Final cleanup of test artifacts
298 rm -f "$SQLITE_DB_TEST"
299
300 if [[ $test_result -eq 0 ]]; then
301 print_success "SQLite tests completed successfully!"
302 else
303 print_error "SQLite tests failed!"
304 exit $test_result
305 fi
306}
307
308# Run main function
309main "$@"