tracks/test_api.sh
Claude 9b0428638c
Fix polymorphic tagging and add test script
This commit fixes the tag loading issue and adds comprehensive testing.

## Bug Fix: Polymorphic Tag Loading

Fixed issue with many-to-many tag relationships not working correctly with
polymorphic associations. The problem was that GORM doesn't support using
`many2many` with polymorphic relationships directly.

**Changes:**
- Modified `internal/models/todo.go`: Changed Tags field to use `gorm:"-"`
  to skip GORM handling
- Modified `internal/models/recurring_todo.go`: Same fix for recurring todos
- Modified `internal/services/todo_service.go`: Added `loadTodoTags()` helper
  function to manually load tags through the taggings join table

**How it works now:**
1. Tags are no longer automatically loaded by GORM
2. Manual loading via JOIN query: `tags JOIN taggings ON tag_id WHERE taggable_id AND taggable_type`
3. Called after loading todos in both `GetTodo()` and `GetTodos()`

## Testing

Added `test_api.sh` - comprehensive integration test script that tests:
1. Health check
2. User registration
3. Authentication
4. Context creation
5. Project creation
6. Todo creation with tags
7. Listing todos with filters
8. Completing todos
9. Project statistics

All tests pass successfully!

## Files Changed

- `internal/models/todo.go`: Fix tag relationship
- `internal/models/recurring_todo.go`: Fix tag relationship
- `internal/services/todo_service.go`: Add manual tag loading
- `test_api.sh`: New integration test script
- `go.sum`: Updated with exact dependency versions
2025-11-05 10:59:26 +00:00

78 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
BASE_URL="http://localhost:3000"
echo "=== Testing Tracks Go API ==="
echo
# Start the server in the background
echo "Starting server..."
rm -f tracks.db
./tracks &
SERVER_PID=$!
sleep 2
echo "1. Health Check"
curl -s "$BASE_URL/api/health" | jq .
echo
echo "2. Register User"
TOKEN=$(curl -s -X POST "$BASE_URL/api/auth/register" \
-H "Content-Type: application/json" \
-d '{"login": "testuser", "password": "testpass123", "first_name": "Test", "last_name": "User"}' | jq -r '.token')
echo "Token: ${TOKEN:0:50}..."
echo
echo "3. Get Current User"
curl -s -X GET "$BASE_URL/api/me" \
-H "Authorization: Bearer $TOKEN" | jq '.login, .first_name'
echo
echo "4. Create Context"
CONTEXT_ID=$(curl -s -X POST "$BASE_URL/api/contexts" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "@home"}' | jq -r '.id')
echo "Created context ID: $CONTEXT_ID"
echo
echo "5. Create Project"
PROJECT_ID=$(curl -s -X POST "$BASE_URL/api/projects" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "Home Renovation", "description": "Renovate the kitchen"}' | jq -r '.id')
echo "Created project ID: $PROJECT_ID"
echo
echo "6. Create Todo with Tags"
TODO_ID=$(curl -s -X POST "$BASE_URL/api/todos" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"description\": \"Buy paint and brushes\", \"notes\": \"Need white paint\", \"context_id\": $CONTEXT_ID, \"project_id\": $PROJECT_ID, \"starred\": true, \"tags\": [\"shopping\", \"urgent\"]}" | jq -r '.id')
echo "Created todo ID: $TODO_ID"
echo
echo "7. List Active Todos"
curl -s -X GET "$BASE_URL/api/todos?state=active&include_tags=true" \
-H "Authorization: Bearer $TOKEN" | jq '.[0] | {id, description, state, tags: [.tags[].name]}'
echo
echo "8. Complete Todo"
curl -s -X POST "$BASE_URL/api/todos/$TODO_ID/complete" \
-H "Authorization: Bearer $TOKEN" | jq '{id, description, state, completed_at}'
echo
echo "9. List Completed Todos"
curl -s -X GET "$BASE_URL/api/todos?state=completed" \
-H "Authorization: Bearer $TOKEN" | jq 'length as $count | "Found \($count) completed todos"'
echo
echo "10. Project Stats"
curl -s -X GET "$BASE_URL/api/projects/$PROJECT_ID/stats" \
-H "Authorization: Bearer $TOKEN" | jq '{name: .project.name, active_todos, completed_todos}'
echo
echo "=== All Tests Passed! ==="
# Clean up
kill $SERVER_PID 2>/dev/null