mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +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:
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