Files
bakery-ia/scripts/validate_demo_seeding.sh
2025-11-30 09:12:40 +01:00

298 lines
12 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# validate_demo_seeding.sh
# Comprehensive smoke test for demo seeding validation
# Tests both Professional and Enterprise demo templates
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Counters
TESTS_PASSED=0
TESTS_FAILED=0
TESTS_TOTAL=0
# Fixed Demo Tenant IDs
DEMO_TENANT_PROFESSIONAL="a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
DEMO_TENANT_ENTERPRISE_PARENT="c3d4e5f6-a7b8-49c0-d1e2-f3a4b5c6d7e8"
DEMO_TENANT_CHILD_1="d4e5f6a7-b8c9-40d1-e2f3-a4b5c6d7e8f9"
DEMO_TENANT_CHILD_2="e5f6a7b8-c9d0-41e2-f3a4-b5c6d7e8f9a0"
DEMO_TENANT_CHILD_3="f6a7b8c9-d0e1-42f3-a4b5-c6d7e8f9a0b1"
# Database connection strings (from Kubernetes secrets)
get_db_url() {
local service=$1
kubectl get secret database-secrets -n bakery-ia -o jsonpath="{.data.${service}_DATABASE_URL}" | base64 -d
}
# Test helper functions
test_start() {
TESTS_TOTAL=$((TESTS_TOTAL + 1))
echo -e "${BLUE}[TEST $TESTS_TOTAL]${NC} $1"
}
test_pass() {
TESTS_PASSED=$((TESTS_PASSED + 1))
echo -e " ${GREEN}✓ PASS${NC}: $1"
}
test_fail() {
TESTS_FAILED=$((TESTS_FAILED + 1))
echo -e " ${RED}✗ FAIL${NC}: $1"
}
test_warn() {
echo -e " ${YELLOW}⚠ WARN${NC}: $1"
}
# SQL query helper
query_db() {
local db_url=$1
local query=$2
kubectl run psql-temp-$RANDOM --rm -i --restart=Never --image=postgres:17-alpine -- \
psql "$db_url" -t -c "$query" 2>/dev/null | xargs
}
echo "========================================"
echo "🧪 Demo Seeding Validation Test Suite"
echo "========================================"
echo ""
echo "Testing Professional and Enterprise demo templates..."
echo ""
# =============================================================================
# PHASE 1: PROFESSIONAL TIER VALIDATION
# =============================================================================
echo "========================================"
echo "📦 Phase 1: Professional Tier (Single Bakery)"
echo "========================================"
echo ""
# Test 1: Tenant Service - Professional tenant exists
test_start "Professional tenant exists in tenant service"
TENANT_DB=$(get_db_url "TENANT")
TENANT_COUNT=$(query_db "$TENANT_DB" "SELECT COUNT(*) FROM tenants WHERE id='$DEMO_TENANT_PROFESSIONAL' AND business_model='individual_bakery'")
if [ "$TENANT_COUNT" -eq 1 ]; then
test_pass "Professional tenant found (Panadería Artesana Madrid)"
else
test_fail "Professional tenant not found or incorrect count: $TENANT_COUNT"
fi
# Test 2: Inventory - Professional has raw ingredients
test_start "Professional tenant has raw ingredients"
INVENTORY_DB=$(get_db_url "INVENTORY")
INGREDIENT_COUNT=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM ingredients WHERE tenant_id='$DEMO_TENANT_PROFESSIONAL' AND product_type='INGREDIENT'")
if [ "$INGREDIENT_COUNT" -ge 20 ]; then
test_pass "Found $INGREDIENT_COUNT raw ingredients (expected ~24)"
else
test_fail "Insufficient raw ingredients: $INGREDIENT_COUNT (expected >=20)"
fi
# Test 3: Inventory - Professional has finished products
test_start "Professional tenant has finished products"
PRODUCT_COUNT=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM ingredients WHERE tenant_id='$DEMO_TENANT_PROFESSIONAL' AND product_type='FINISHED_PRODUCT'")
if [ "$PRODUCT_COUNT" -ge 4 ]; then
test_pass "Found $PRODUCT_COUNT finished products (expected ~4)"
else
test_fail "Insufficient finished products: $PRODUCT_COUNT (expected >=4)"
fi
# Test 4: Recipes - Professional has recipes
test_start "Professional tenant has recipes"
RECIPES_DB=$(get_db_url "RECIPES")
RECIPE_COUNT=$(query_db "$RECIPES_DB" "SELECT COUNT(*) FROM recipes WHERE tenant_id='$DEMO_TENANT_PROFESSIONAL'")
if [ "$RECIPE_COUNT" -ge 4 ]; then
test_pass "Found $RECIPE_COUNT recipes (expected ~4-20)"
else
test_fail "Insufficient recipes: $RECIPE_COUNT (expected >=4)"
fi
# Test 5: Sales - Professional has sales history
test_start "Professional tenant has sales history"
SALES_DB=$(get_db_url "SALES")
SALES_COUNT=$(query_db "$SALES_DB" "SELECT COUNT(*) FROM sales_data WHERE tenant_id='$DEMO_TENANT_PROFESSIONAL'")
if [ "$SALES_COUNT" -ge 100 ]; then
test_pass "Found $SALES_COUNT sales records (expected ~360 for 90 days)"
else
test_warn "Lower than expected sales records: $SALES_COUNT (expected >=100)"
fi
# =============================================================================
# PHASE 2: ENTERPRISE PARENT VALIDATION
# =============================================================================
echo ""
echo "========================================"
echo "🏭 Phase 2: Enterprise Parent (Obrador)"
echo "========================================"
echo ""
# Test 6: Tenant Service - Enterprise parent exists
test_start "Enterprise parent tenant exists"
PARENT_COUNT=$(query_db "$TENANT_DB" "SELECT COUNT(*) FROM tenants WHERE id='$DEMO_TENANT_ENTERPRISE_PARENT' AND business_model='enterprise_chain'")
if [ "$PARENT_COUNT" -eq 1 ]; then
test_pass "Enterprise parent found (Obrador Madrid)"
else
test_fail "Enterprise parent not found or incorrect count: $PARENT_COUNT"
fi
# Test 7: Inventory - Parent has raw ingredients (scaled 10x)
test_start "Enterprise parent has raw ingredients"
PARENT_INGREDIENT_COUNT=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM ingredients WHERE tenant_id='$DEMO_TENANT_ENTERPRISE_PARENT' AND product_type='INGREDIENT'")
if [ "$PARENT_INGREDIENT_COUNT" -ge 20 ]; then
test_pass "Found $PARENT_INGREDIENT_COUNT raw ingredients (expected ~24)"
else
test_fail "Insufficient parent raw ingredients: $PARENT_INGREDIENT_COUNT (expected >=20)"
fi
# Test 8: Recipes - Parent has recipes
test_start "Enterprise parent has recipes"
PARENT_RECIPE_COUNT=$(query_db "$RECIPES_DB" "SELECT COUNT(*) FROM recipes WHERE tenant_id='$DEMO_TENANT_ENTERPRISE_PARENT'")
if [ "$PARENT_RECIPE_COUNT" -ge 4 ]; then
test_pass "Found $PARENT_RECIPE_COUNT recipes (expected ~4-20)"
else
test_fail "Insufficient parent recipes: $PARENT_RECIPE_COUNT (expected >=4)"
fi
# Test 9: Production - Parent has production batches
test_start "Enterprise parent has production batches"
PRODUCTION_DB=$(get_db_url "PRODUCTION")
BATCH_COUNT=$(query_db "$PRODUCTION_DB" "SELECT COUNT(*) FROM production_batches WHERE tenant_id='$DEMO_TENANT_ENTERPRISE_PARENT'")
if [ "$BATCH_COUNT" -ge 50 ]; then
test_pass "Found $BATCH_COUNT production batches (expected ~120)"
elif [ "$BATCH_COUNT" -ge 20 ]; then
test_warn "Lower production batches: $BATCH_COUNT (expected ~120)"
else
test_fail "Insufficient production batches: $BATCH_COUNT (expected >=50)"
fi
# =============================================================================
# PHASE 3: CHILD RETAIL OUTLETS VALIDATION
# =============================================================================
echo ""
echo "========================================"
echo "🏪 Phase 3: Child Retail Outlets"
echo "========================================"
echo ""
# Test each child tenant
for CHILD_ID in "$DEMO_TENANT_CHILD_1" "$DEMO_TENANT_CHILD_2" "$DEMO_TENANT_CHILD_3"; do
case "$CHILD_ID" in
"$DEMO_TENANT_CHILD_1") CHILD_NAME="Madrid Centro" ;;
"$DEMO_TENANT_CHILD_2") CHILD_NAME="Barcelona Gràcia" ;;
"$DEMO_TENANT_CHILD_3") CHILD_NAME="Valencia Ruzafa" ;;
esac
echo ""
echo "Testing: $CHILD_NAME"
echo "----------------------------------------"
# Test 10a: Child has finished products ONLY (no raw ingredients)
test_start "[$CHILD_NAME] Has finished products ONLY"
CHILD_PRODUCTS=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM ingredients WHERE tenant_id='$CHILD_ID' AND product_type='FINISHED_PRODUCT'")
CHILD_RAW=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM ingredients WHERE tenant_id='$CHILD_ID' AND product_type='INGREDIENT'")
if [ "$CHILD_PRODUCTS" -eq 4 ] && [ "$CHILD_RAW" -eq 0 ]; then
test_pass "Found $CHILD_PRODUCTS finished products, 0 raw ingredients (correct retail model)"
elif [ "$CHILD_RAW" -gt 0 ]; then
test_fail "Child has raw ingredients ($CHILD_RAW) - should only have finished products"
else
test_warn "Product count mismatch: $CHILD_PRODUCTS (expected 4)"
fi
# Test 10b: Child has stock batches
test_start "[$CHILD_NAME] Has stock batches"
CHILD_STOCK=$(query_db "$INVENTORY_DB" "SELECT COUNT(*) FROM stock WHERE tenant_id='$CHILD_ID'")
if [ "$CHILD_STOCK" -ge 10 ]; then
test_pass "Found $CHILD_STOCK stock batches (expected ~16)"
else
test_warn "Lower stock batches: $CHILD_STOCK (expected ~16)"
fi
# Test 10c: Child has sales history
test_start "[$CHILD_NAME] Has sales history"
CHILD_SALES=$(query_db "$SALES_DB" "SELECT COUNT(*) FROM sales_data WHERE tenant_id='$CHILD_ID'")
if [ "$CHILD_SALES" -ge 80 ]; then
test_pass "Found $CHILD_SALES sales records (expected ~120 for 30 days)"
else
test_warn "Lower sales records: $CHILD_SALES (expected ~120)"
fi
# Test 10d: Child has customers
test_start "[$CHILD_NAME] Has walk-in customers"
ORDERS_DB=$(get_db_url "ORDERS")
CHILD_CUSTOMERS=$(query_db "$ORDERS_DB" "SELECT COUNT(*) FROM customers WHERE tenant_id='$CHILD_ID'")
if [ "$CHILD_CUSTOMERS" -ge 40 ]; then
test_pass "Found $CHILD_CUSTOMERS customers (expected 60-100)"
else
test_warn "Lower customer count: $CHILD_CUSTOMERS (expected 60-100)"
fi
done
# =============================================================================
# PHASE 4: DISTRIBUTION VALIDATION
# =============================================================================
echo ""
echo "========================================"
echo "🚚 Phase 4: Distribution & Logistics"
echo "========================================"
echo ""
# Test 11: Distribution routes exist
test_start "Distribution routes created (Mon/Wed/Fri pattern)"
DISTRIBUTION_DB=$(get_db_url "DISTRIBUTION")
ROUTE_COUNT=$(query_db "$DISTRIBUTION_DB" "SELECT COUNT(*) FROM delivery_routes WHERE tenant_id='$DEMO_TENANT_ENTERPRISE_PARENT'")
if [ "$ROUTE_COUNT" -ge 10 ]; then
test_pass "Found $ROUTE_COUNT delivery routes (expected ~13 for 30 days, Mon/Wed/Fri)"
else
test_warn "Lower route count: $ROUTE_COUNT (expected ~13)"
fi
# Test 12: Shipments exist for all children
test_start "Shipments created for all retail outlets"
SHIPMENT_COUNT=$(query_db "$DISTRIBUTION_DB" "SELECT COUNT(*) FROM shipments WHERE parent_tenant_id='$DEMO_TENANT_ENTERPRISE_PARENT'")
if [ "$SHIPMENT_COUNT" -ge 30 ]; then
test_pass "Found $SHIPMENT_COUNT shipments (expected ~39: 13 routes × 3 children)"
else
test_warn "Lower shipment count: $SHIPMENT_COUNT (expected ~39)"
fi
# =============================================================================
# SUMMARY
# =============================================================================
echo ""
echo "========================================"
echo "📊 Test Summary"
echo "========================================"
echo ""
echo "Total Tests: $TESTS_TOTAL"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✅ ALL TESTS PASSED!${NC}"
echo ""
echo "Demo templates are ready for cloning:"
echo " ✓ Professional tier (single bakery): ~3,500 records"
echo " ✓ Enterprise parent (Obrador): ~3,000 records"
echo " ✓ 3 Child retail outlets: ~700 records"
echo " ✓ Distribution history: ~52 records"
echo " ✓ Total template data: ~4,200-4,800 records"
echo ""
exit 0
else
echo -e "${RED}❌ SOME TESTS FAILED${NC}"
echo ""
echo "Please review the failed tests above and:"
echo " 1. Check that all seed jobs completed successfully"
echo " 2. Verify database connections"
echo " 3. Check seed script logs for errors"
echo ""
exit 1
fi