← back to Watches
test/api-tests.sh
188 lines
#!/bin/bash
###############################################################################
# Omega Watch API Test Suite
# Tests all endpoints for functionality and performance
###############################################################################
API_BASE="http://localhost:7600/api"
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
PASSED=0
FAILED=0
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ OMEGA WATCH API TEST SUITE ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
# Test function
test_endpoint() {
local name=$1
local url=$2
local method=${3:-GET}
local data=$4
local expected_status=${5:-200}
echo -n "Testing: $name ... "
if [ "$method" = "POST" ]; then
response=$(curl -s -w "\n%{http_code}" -X POST "$url" -H "Content-Type: application/json" -d "$data")
else
response=$(curl -s -w "\n%{http_code}" "$url")
fi
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "$expected_status" ]; then
echo -e "${GREEN}✓ PASS${NC} (${http_code})"
((PASSED++))
return 0
else
echo -e "${RED}✗ FAIL${NC} (Expected: ${expected_status}, Got: ${http_code})"
((FAILED++))
return 1
fi
}
# Performance test
test_performance() {
local name=$1
local url=$2
local max_time=$3
echo -n "Performance: $name ... "
start_time=$(date +%s%N)
response=$(curl -s "$url")
end_time=$(date +%s%N)
duration=$(( (end_time - start_time) / 1000000 ))
if [ $duration -lt $max_time ]; then
echo -e "${GREEN}✓ PASS${NC} (${duration}ms < ${max_time}ms)"
((PASSED++))
else
echo -e "${RED}✗ FAIL${NC} (${duration}ms > ${max_time}ms)"
((FAILED++))
fi
}
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "BASIC ENDPOINTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Health Check" "${API_BASE}/health"
test_endpoint "API Documentation" "${API_BASE}/docs"
test_endpoint "All Watches" "${API_BASE}/watches"
test_endpoint "Statistics" "${API_BASE}/statistics"
test_endpoint "Collections" "${API_BASE}/collections"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "SEARCH & FILTERING"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Search: Moonwatch" "${API_BASE}/search?q=moonwatch"
test_endpoint "Filter by Series" "${API_BASE}/watches?series=Speedmaster"
test_endpoint "Filter by Price Range" "${API_BASE}/watches?minPrice=5000&maxPrice=10000"
test_endpoint "Filter by Year Range" "${API_BASE}/watches?minYear=1960&maxYear=1970"
test_endpoint "Pagination" "${API_BASE}/watches?page=2&limit=10"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "WATCH-SPECIFIC ENDPOINTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Watch History" "${API_BASE}/watches/speedmaster-moonwatch-1969/history"
test_endpoint "Price Predictions" "${API_BASE}/price-predictions/speedmaster-moonwatch-1969"
test_endpoint "Trending Watches" "${API_BASE}/watches/trending?limit=5"
test_endpoint "Invalid Watch ID" "${API_BASE}/watches/invalid-id/history" "GET" "" "404"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "POST ENDPOINTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Compare Watches" "${API_BASE}/compare" "POST" '{"watchIds":["speedmaster-moonwatch-1969","seamaster-300-1957"]}'
test_endpoint "Add to Watchlist" "${API_BASE}/watchlist" "POST" '{"userId":"test-user","watchId":"speedmaster-moonwatch-1969","action":"add"}'
test_endpoint "Get Watchlist" "${API_BASE}/watchlist/test-user"
test_endpoint "Remove from Watchlist" "${API_BASE}/watchlist" "POST" '{"userId":"test-user","watchId":"speedmaster-moonwatch-1969","action":"remove"}'
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "EXPORT ENDPOINTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Export JSON" "${API_BASE}/export/json"
test_endpoint "Export CSV" "${API_BASE}/export/csv"
test_endpoint "Invalid Export Format" "${API_BASE}/export/xml" "GET" "" "400"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "ADMIN ENDPOINTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_endpoint "Create Backup" "${API_BASE}/admin/backup"
test_endpoint "Clear Cache" "${API_BASE}/admin/clear-cache" "POST" '{}'
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "PERFORMANCE TESTS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
test_performance "Health Check Speed" "${API_BASE}/health" 100
test_performance "Statistics Speed" "${API_BASE}/statistics" 500
test_performance "Collections Speed" "${API_BASE}/collections" 500
test_performance "Search Speed" "${API_BASE}/search?q=speedmaster" 300
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "CACHE VERIFICATION"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -n "Testing cache functionality ... "
# First call (not cached)
start1=$(date +%s%N)
curl -s "${API_BASE}/statistics" > /dev/null
duration1=$(( ($(date +%s%N) - start1) / 1000000 ))
# Second call (should be cached)
start2=$(date +%s%N)
curl -s "${API_BASE}/statistics" > /dev/null
duration2=$(( ($(date +%s%N) - start2) / 1000000 ))
if [ $duration2 -lt $duration1 ]; then
echo -e "${GREEN}✓ PASS${NC} (Cached: ${duration2}ms < Uncached: ${duration1}ms)"
((PASSED++))
else
echo -e "${YELLOW}⚠ WARNING${NC} (Cached: ${duration2}ms, Uncached: ${duration1}ms)"
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "SUMMARY"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
TOTAL=$((PASSED + FAILED))
PASS_RATE=$((PASSED * 100 / TOTAL))
echo ""
echo "Total Tests: $TOTAL"
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo "Pass Rate: ${PASS_RATE}%"
echo ""
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}✓ ALL TESTS PASSED!${NC}"
exit 0
else
echo -e "${RED}✗ SOME TESTS FAILED${NC}"
exit 1
fi