File size: 1,964 Bytes
1638b2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash

# PharmaShield Backend Test Suite

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo "🧪 Starting Backend Integration Tests..."

# 1. Setup Environment
if [ -d "backend/.venv" ]; then
    source backend/.venv/bin/activate || source backend/.venv/Scripts/activate
else
    echo "⚠️ .venv not found. Running with system python..."
fi

# 2. Start Uvicorn in Background
cd backend
uvicorn app.main:app --host 127.0.0.1 --port 8080 > /dev/null 2>&1 &
UVICORN_PID=$!
cd ..

# Ensure uvicorn is killed on script exit
cleanup() {
    echo "🧹 Cleaning up..."
    kill $UVICORN_PID 2>/dev/null || true
}
trap cleanup EXIT

# 3. Wait for Startup
echo "⏳ Waiting for server to spin up (3s)..."
sleep 3

EXIT_CODE=0

# 4. Run Tests
test_endpoint() {
    local url=$1
    local expected_status=$2
    local name=$3
    local check_json=$4

    echo -n "Checking $name ($url)... "
    
    RESPONSE=$(curl -s -w "%{http_code}" "$url")
    STATUS=${RESPONSE: -3}
    BODY=${RESPONSE:0:${#RESPONSE}-3}

    if [ "$STATUS" -eq "$expected_status" ]; then
        if [ -n "$check_json" ]; then
            if echo "$BODY" | grep -q "$check_json"; then
                echo -e "${GREEN}PASS${NC}"
            else
                echo -e "${RED}FAIL (JSON mismatch)${NC}"
                EXIT_CODE=1
            fi
        else
            echo -e "${GREEN}PASS${NC}"
        fi
    else
        echo -e "${RED}FAIL (Status: $STATUS)${NC}"
        EXIT_CODE=1
    fi
}

# Test 1: Health Check
test_endpoint "http://localhost:8080/healthz" 200 "Health Check" '"status":"ok"'

# Test 2: Root Info
test_endpoint "http://localhost:8080/" 200 "Root Info"

# Test 3: API Documentation
test_endpoint "http://localhost:8080/docs" 200 "Swagger Docs"

# 5. Final Report
if [ $EXIT_CODE -eq 0 ]; then
    echo -e "\n${GREEN}✅ All backend tests passed!${NC}"
else
    echo -e "\n${RED}❌ Some tests failed. Check logs.${NC}"
fi

exit $EXIT_CODE