fix: improve test infrastructure and add ID validation

- Add TestMain to set test env vars and testdb.TruncateTables for test
  isolation
- Add chi routing context to test helpers for path parameter extraction
- Fix SQL error handling to use errors.Is() instead of ==
- Add validators package with ID validation
- Fix admin test middleware chain (RequireAdmin wrapper)
- Update test user inserts to include phone and date_of_birth fields
- Update service delete test to check soft-delete (is_active=false)
- Update holiday hours test to use new schema (weekday, is_open)
- Add phone number validation tests for UK mobile numbers
This commit is contained in:
2026-02-23 00:59:32 +00:00
parent 355e8a26c1
commit df3439bd70
30 changed files with 1081 additions and 360 deletions
+38 -22
View File
@@ -132,8 +132,8 @@ tmux set-environment -t $SESSION_NAME AWS_REGION "$AWS_REGION"
tmux set-environment -t $SESSION_NAME VITE_BACKEND_URL "$VITE_BACKEND_URL"
# Pane 0: Database
# Start interactive shell only. Stats will be shown after seeding.
tmux send-keys -t $SESSION_NAME "docker exec -it postgres psql -U myuser -d mydb" Enter
# Start with tables + row count query
tmux send-keys -t $SESSION_NAME 'docker exec -it postgres psql -U myuser -d mydb -c "SELECT relname AS table_name, n_live_tup AS row_count FROM pg_stat_user_tables ORDER BY table_name;"'
tmux select-pane -t $SESSION_NAME:0.0 -T "DB"
# Pane 1: Backend (Split Horizontally)
@@ -470,12 +470,12 @@ for ((i=0; i<${#UPCOMING_BOOKING_IDS[@]}; i++)); do
done
echo "${C_GREEN}✅ Confirmed $confirmed_count/$attempted_count Bookings${C_RESET}"
if [ "$rejected_count" -gt 0 ]; then
echo "${C_YELLOW}⚠️ Rejected $rejected_count/$attempted_count (deposit/time restrictions)${C_RESET}"
echo "${C_YELLOW}⚠️ Rejected $rejected_count/$attempted_count (deposit/time restrictions)${C_RESET}"
fi
# 6. Exceptional Groups (2 Total)
echo -e "\n${C_BLUE}🗓️ Creating Exceptional Groups...${C_RESET}"
echo -e "\n${C_BLUE}🗓️ Creating Exceptional Groups...${C_RESET}"
NOV_BREAK='{"name":"November Break","description":"Short break period in November","hours":[{"weekday":0,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":1,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":2,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":3,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":4,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":5,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":6,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false}],"weekStarts":["2025-11-10"]}'
XMAS_BREAK='{"name":"Christmas Holiday Period","description":"Reduced hours for Christmas and New Year","hours":[{"weekday":0,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":1,"startTime":"10:00:00","endTime":"15:00:00","isOpen":true},{"weekday":2,"startTime":"10:00:00","endTime":"15:00:00","isOpen":true},{"weekday":3,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":4,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":5,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false},{"weekday":6,"startTime":"00:00:00","endTime":"00:00:00","isOpen":false}],"weekStarts":["2025-12-22","2025-12-29"]}'
@@ -492,34 +492,50 @@ if [ -n "$SESSION_NAME" ]; then
fi
echo -e "\n${C_GREEN}🎉 Seeding Complete!${C_RESET}"
read -n1 -s -p "Press any key to close this window..."
echo -e "${C_YELLOW}Press ENTER to run tests...${C_RESET}"
read -r
echo -e "${C_GREEN}⏳ Running tests...${C_RESET}"
# Run tests in current pane with full output
cd /home/popertots/Crussell/backend
export POSTGRES_USER POSTGRES_PASSWORD POSTGRES_HOST POSTGRES_DB GO_TESTING=1
TEST_OUTPUT=$(go test -tags test -v -p 1 -count=1 ./... 2>&1 || true)
cd ..
# --- Main Database Seeding Complete ---
echo -e "\n${C_GREEN}🎉 Main Database Seeding Complete!${C_RESET}"
# --- Run Tests ---
echo -e "\n${C_BLUE}🧪 Running Backend Tests...${C_RESET}"
cd backend
TEST_OUTPUT=$(go test -tags test -v ./... 2>&1 || true)
TOTAL_TESTS=$(echo "$TEST_OUTPUT" | grep -c "^=== RUN" || echo "0")
PASSED_TESTS=$(echo "$TEST_OUTPUT" | grep -c "^--- PASS" || echo "0")
# Show test summary - sanitize grep output to handle edge cases
TOTAL_TESTS=$(echo "$TEST_OUTPUT" | grep "^=== RUN" | grep -cv "/" || echo "0")
FAILED_TESTS=$(echo "$TEST_OUTPUT" | grep -c "^--- FAIL" || echo "0")
SKIPPED_TESTS=$(echo "$TEST_OUTPUT" | grep -c "^--- SKIP" || echo "0")
PASSED_TESTS=$(echo "$TEST_OUTPUT" | grep -c "^--- PASS" || echo "0")
if [ "$FAILED_TESTS" -gt 0 ]; then
echo -e "${C_RED}❌ Tests Failed: $PASSED_TESTS/$TOTAL_TESTS passed${C_RESET}"
# Fallback: if counts are empty or invalid, default to 0
TOTAL_TESTS=${TOTAL_TESTS:-0}
PASSED_TESTS=${PASSED_TESTS:-0}
FAILED_TESTS=${FAILED_TESTS:-0}
SKIPPED_TESTS=${SKIPPED_TESTS:-0}
echo ""
if [ "$SKIPPED_TESTS" -gt 0 ] 2>/dev/null; then
echo -e "${C_YELLOW}⚠️ Tests Skipped: $SKIPPED_TESTS${C_RESET}"
fi
if [ "$FAILED_TESTS" -gt 0 ] 2>/dev/null; then
echo -e "${C_RED}❌ Tests Failed: $FAILED_TESTS/$TOTAL_TESTS failed${C_RESET}"
echo ""
echo -e "${C_RED}--- Failed Test Details ---${C_RESET}"
echo "$TEST_OUTPUT" | grep -A 3 "^--- FAIL" | head -20
echo ""
echo -e "${C_YELLOW}⚠️ Continuing anyway (tests are non-blocking)${C_RESET}"
echo -e "${C_RED}--- Failed Tests ---${C_RESET}"
echo "$TEST_OUTPUT" | grep "^--- FAIL" | head -20
else
echo -e "${C_GREEN}✅ All Tests Passed: $PASSED_TESTS/$TOTAL_TESTS${C_RESET}"
fi
cd ..
echo ""
echo -e "\n${C_YELLOW}Press ENTER to close this seeding pane...${C_RESET}"
echo -e "${C_YELLOW}Press ENTER to restore 4-pane layout...${C_RESET}"
read -r
# Switch back to the original workspace window (window 0)
# The original 4 panes (DB, Backend, Frontend, Rustfs) are still there
tmux select-window -t $SESSION_NAME:0
tmux select-pane -t $SESSION_NAME:0.0
SEED_EOF
chmod +x $SEED_SCRIPT