118 أسطر
2.6 KiB
Bash
ملف تنفيذي
118 أسطر
2.6 KiB
Bash
ملف تنفيذي
#!/bin/bash
|
|
|
|
# Configuration
|
|
API_URL="http://localhost:5000"
|
|
LOG_FILE="test_results.log"
|
|
|
|
# Clear previous log
|
|
> "$LOG_FILE"
|
|
|
|
echo "Starting API tests..."
|
|
echo "Results will be saved to: $LOG_FILE"
|
|
echo "========================================"
|
|
|
|
# Function to log results
|
|
log_result() {
|
|
echo "$1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Test 1: Home page
|
|
log_result ""
|
|
log_result "Test 1: GET / (Home page)"
|
|
log_result "----------------------------"
|
|
curl -s -X GET "$API_URL/" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 2: Get all products
|
|
log_result ""
|
|
log_result "Test 2: GET /products (All products)"
|
|
log_result "----------------------------"
|
|
curl -s -X GET "$API_URL/products" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 3: Get single product
|
|
log_result ""
|
|
log_result "Test 3: GET /products/1 (Single product)"
|
|
log_result "----------------------------"
|
|
curl -s -X GET "$API_URL/products/1" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 4: Add new product
|
|
log_result ""
|
|
log_result "Test 4: POST /products (Add new product)"
|
|
log_result "----------------------------"
|
|
curl -s -X POST "$API_URL/products" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"name": "Keyboard",
|
|
"price": 300,
|
|
"quantity": 20
|
|
}' | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 5: Update product
|
|
log_result ""
|
|
log_result "Test 5: PUT /products/1 (Update product)"
|
|
log_result "----------------------------"
|
|
curl -s -X PUT "$API_URL/products/1" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"price": 4500,
|
|
"quantity": 15
|
|
}' | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 6: Create order
|
|
log_result ""
|
|
log_result "Test 6: POST /orders (Create order)"
|
|
log_result "----------------------------"
|
|
curl -s -X POST "$API_URL/orders" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"product_id": 2,
|
|
"quantity": 5
|
|
}' | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 7: Get all orders
|
|
log_result ""
|
|
log_result "Test 7: GET /orders (All orders)"
|
|
log_result "----------------------------"
|
|
curl -s -X GET "$API_URL/orders" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 8: Delete product
|
|
log_result ""
|
|
log_result "Test 8: DELETE /products/2 (Delete product)"
|
|
log_result "----------------------------"
|
|
curl -s -X DELETE "$API_URL/products/2" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
sleep 1
|
|
|
|
# Test 9: Try to get deleted product (should fail)
|
|
log_result ""
|
|
log_result "Test 9: GET /products/2 (Should return 404)"
|
|
log_result "----------------------------"
|
|
curl -s -X GET "$API_URL/products/2" | tee -a "$LOG_FILE"
|
|
log_result ""
|
|
|
|
log_result ""
|
|
log_result "========================================"
|
|
log_result "All tests completed!"
|
|
log_result "Check $LOG_FILE for full results"
|