#!/bin/bash echo "๐Ÿงช Testing Training with Actual Sales Data" echo "========================================" # Create user and tenant echo "1. Creating user and tenant..." EMAIL="training.withdata.$(date +%s)@bakery.com" REGISTER_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/auth/register" \ -H "Content-Type: application/json" \ -d "{\"email\": \"$EMAIL\", \"password\": \"TestPassword123!\", \"full_name\": \"Training Test User\", \"role\": \"admin\"}") ACCESS_TOKEN=$(echo "$REGISTER_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('access_token', '')) except: pass " 2>/dev/null) if [ -z "$ACCESS_TOKEN" ]; then echo "โŒ User registration failed" echo "Response: $REGISTER_RESPONSE" exit 1 fi echo "โœ… User registered successfully" # Create tenant TENANT_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/register" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -d '{"name": "Training Test Bakery", "business_type": "bakery", "address": "Test Address", "city": "Madrid", "postal_code": "28001", "phone": "+34600123456"}') TENANT_ID=$(echo "$TENANT_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('id', '')) except: pass " 2>/dev/null) if [ -z "$TENANT_ID" ]; then echo "โŒ Tenant creation failed" echo "Response: $TENANT_RESPONSE" exit 1 fi echo "โœ… Tenant created: $TENANT_ID" # 2. Import sales data using file upload echo "" echo "2. Importing sales data using file upload..." IMPORT_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/sales/import" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -F "file=@test_sales_data.csv" \ -F "file_format=csv") HTTP_CODE=$(echo "$IMPORT_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2) IMPORT_RESPONSE=$(echo "$IMPORT_RESPONSE" | sed '/HTTP_CODE:/d') echo "Import HTTP Status Code: $HTTP_CODE" echo "Import Response:" echo "$IMPORT_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$IMPORT_RESPONSE" if [ "$HTTP_CODE" = "200" ]; then echo "โœ… Data import successful!" else echo "โŒ Data import failed" exit 1 fi # 3. Test training with data echo "" echo "3. Testing training WITH imported sales data..." TRAINING_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -d '{}') echo "Training Response:" echo "$TRAINING_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$TRAINING_RESPONSE" JOB_ID=$(echo "$TRAINING_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('job_id', '')) except: pass " 2>/dev/null) if [ -n "$JOB_ID" ]; then echo "โœ… Training job created with data: $JOB_ID" echo "โณ Monitoring progress for 60 seconds..." for i in {1..12}; do sleep 5 STATUS_RESPONSE=$(curl -s "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs/$JOB_ID/status" \ -H "Authorization: Bearer $ACCESS_TOKEN" 2>/dev/null) STATUS=$(echo "$STATUS_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('status', 'unknown')) except: print('error') " 2>/dev/null) PROGRESS=$(echo "$STATUS_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('progress', 0)) except: print(0) " 2>/dev/null) CURRENT_STEP=$(echo "$STATUS_RESPONSE" | python3 -c " import json, sys try: data = json.load(sys.stdin) print(data.get('current_step', 'unknown')) except: print('unknown') " 2>/dev/null) echo "[$i/12] Status: $STATUS | Progress: $PROGRESS% | Step: $CURRENT_STEP" if [ "$STATUS" = "completed" ] || [ "$STATUS" = "failed" ]; then echo "" echo "Final Status Response:" echo "$STATUS_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$STATUS_RESPONSE" if [ "$STATUS" = "completed" ]; then echo "โœ… Training completed successfully!" # Test forecast endpoint echo "" echo "4. Testing forecast endpoint..." FORECAST_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/forecasts" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -d '{"product_name": "bread", "days_ahead": 7}') echo "Forecast Response:" echo "$FORECAST_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$FORECAST_RESPONSE" elif [ "$STATUS" = "failed" ]; then echo "โŒ Training failed" fi break fi done if [ "$i" -eq 12 ]; then echo "โฐ Training still in progress after 60 seconds" echo "Final status check:" echo "$STATUS_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$STATUS_RESPONSE" fi else echo "โŒ No training job ID returned" fi echo "" echo "๐Ÿ Training with data test completed!"