mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-14 01:17:52 +00:00
test: add comprehensive unit tests for provider, middleware, and sync service
- Add TESTING.md documentation explaining unit test conventions and integration testing setup - Add miniredis and sqlite dependencies to go.mod for in-memory testing - Add provider_handler_test.go ensuring Vertex providers default google_location to "global" - Add request_id_test.go verifying request ID generation and header preservation - Add sync_test.go validating Redis snapshot writes and routing key generation - Update README.md with testing section referencing new documentation
This commit is contained in:
105
internal/api/provider_handler_test.go
Normal file
105
internal/api/provider_handler_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/ez-api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func newTestHandler(t *testing.T) (*Handler, *gorm.DB) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Use a unique in-memory DB per test to avoid cross-test interference.
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Provider{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
sync := service.NewSyncService(rdb)
|
||||
|
||||
return NewHandler(db, sync, nil), db
|
||||
}
|
||||
|
||||
func TestCreateProvider_DefaultsVertexLocationGlobal(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers", h.CreateProvider)
|
||||
|
||||
reqBody := dto.ProviderDTO{
|
||||
Name: "g1",
|
||||
Type: "vertex-express",
|
||||
Group: "default",
|
||||
Models: []string{"gemini-3-pro-preview"},
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("expected 201, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var got model.Provider
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if got.GoogleLocation != "global" {
|
||||
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProvider_DefaultsVertexLocationGlobalWhenMissing(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
existing := &model.Provider{
|
||||
Name: "g2",
|
||||
Type: "vertex",
|
||||
Group: "default",
|
||||
Models: "gemini-3-pro-preview",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(existing).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.PUT("/admin/providers/:id", h.UpdateProvider)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/admin/providers/%d", existing.ID), bytes.NewReader([]byte(`{}`)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var got model.Provider
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if got.GoogleLocation != "global" {
|
||||
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
||||
}
|
||||
}
|
||||
57
internal/middleware/request_id_test.go
Normal file
57
internal/middleware/request_id_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestRequestID_GeneratesAndEchoes(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
r := gin.New()
|
||||
r.Use(RequestID())
|
||||
r.GET("/x", func(c *gin.Context) {
|
||||
c.String(200, c.GetHeader("X-Request-ID"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != 200 {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
id := rr.Header().Get("X-Request-ID")
|
||||
if id == "" {
|
||||
t.Fatalf("expected X-Request-ID response header")
|
||||
}
|
||||
if rr.Body.String() != id {
|
||||
t.Fatalf("expected body to echo X-Request-ID, got %q want %q", rr.Body.String(), id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestID_PreservesIncomingHeader(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
r := gin.New()
|
||||
r.Use(RequestID())
|
||||
r.GET("/x", func(c *gin.Context) {
|
||||
c.String(200, c.GetHeader("X-Request-ID"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req.Header.Set("X-Request-ID", "req-abc")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != 200 {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
if got := rr.Header().Get("X-Request-ID"); got != "req-abc" {
|
||||
t.Fatalf("expected preserved X-Request-ID, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
58
internal/service/sync_test.go
Normal file
58
internal/service/sync_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func TestSyncProvider_WritesSnapshotAndRouting(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
|
||||
svc := NewSyncService(rdb)
|
||||
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "vertex-express",
|
||||
Group: "default",
|
||||
Models: "gemini-3-pro-preview",
|
||||
Status: "active",
|
||||
AutoBan: true,
|
||||
GoogleProject: "",
|
||||
GoogleLocation: "global",
|
||||
}
|
||||
p.ID = 42
|
||||
|
||||
if err := svc.SyncProvider(p); err != nil {
|
||||
t.Fatalf("SyncProvider: %v", err)
|
||||
}
|
||||
|
||||
raw := mr.HGet("config:providers", "42")
|
||||
if raw == "" {
|
||||
t.Fatalf("expected config:providers hash entry")
|
||||
}
|
||||
|
||||
var snap map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &snap); err != nil {
|
||||
t.Fatalf("invalid snapshot json: %v", err)
|
||||
}
|
||||
if snap["google_location"] != "global" {
|
||||
t.Fatalf("expected google_location=global, got %v", snap["google_location"])
|
||||
}
|
||||
|
||||
routeKey := "route:group:default:gemini-3-pro-preview"
|
||||
if !mr.Exists(routeKey) {
|
||||
t.Fatalf("expected routing key %q to exist", routeKey)
|
||||
}
|
||||
ok, err := mr.SIsMember(routeKey, "42")
|
||||
if err != nil {
|
||||
t.Fatalf("SIsMember: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatalf("expected provider id 42 in routing set %q", routeKey)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user