67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/danielgtaylor/huma/v2/adapters/humaecho"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"completed/internal/db"
|
|
)
|
|
|
|
func TestListGames(t *testing.T) {
|
|
// 1. Setup Mock
|
|
mockDB := &MockQuerier{
|
|
ListGamesFunc: func(ctx context.Context) ([]db.Game, error) {
|
|
return []db.Game{
|
|
{ID: 1, Name: "Test Game", Score: 90},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
// 2. Setup API
|
|
e := echo.New()
|
|
api := humaecho.New(e, huma.DefaultConfig("Test API", "1.0.0"))
|
|
RegisterRoutes(api, mockDB)
|
|
|
|
// 3. Execute Request
|
|
req := httptest.NewRequest(http.MethodGet, "/games", nil)
|
|
rec := httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
|
|
// 4. Assert
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Contains(t, rec.Body.String(), "Test Game")
|
|
}
|
|
|
|
func TestCreatePlatform(t *testing.T) {
|
|
// 1. Setup Mock
|
|
mockDB := &MockQuerier{
|
|
CreatePlatformFunc: func(ctx context.Context, name string) (db.Platform, error) {
|
|
return db.Platform{ID: 1, Name: name}, nil
|
|
},
|
|
}
|
|
|
|
// 2. Setup API
|
|
e := echo.New()
|
|
api := humaecho.New(e, huma.DefaultConfig("Test API", "1.0.0"))
|
|
RegisterRoutes(api, mockDB)
|
|
|
|
// 3. Execute Request
|
|
req := httptest.NewRequest(http.MethodPost, "/platforms", strings.NewReader(`{"name": "Sega Genesis"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
|
|
// 4. Assert
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Contains(t, rec.Body.String(), "Sega Genesis")
|
|
assert.Contains(t, rec.Body.String(), `"id":1`)
|
|
}
|