feat: train proxy and correct staffing peak fixture

This commit is contained in:
Noel Sabia committed 2026-11-14 04:45:18 +01:00
1 parent f284969a4b
commit 4b9c2da9f7
7 files changed
+575 -10

No files matched your search

+66
View File
@@ -3,8 +3,10 @@ package ml
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
@@ -125,3 +127,67 @@ func TestNewUsesDefaultTimeout(t *testing.T) {
t.Fatalf("timeout=%s, want %s", client.http.Timeout, DefaultTimeout)
}
}
func TestTrainForwardsBodyContentTypeAndStatus(t *testing.T) {
var gotMethod, gotPath, gotType, gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
gotType = r.Header.Get("Content-Type")
raw, _ := io.ReadAll(r.Body)
gotBody = string(raw)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small"}`))
}))
defer srv.Close()
status, body, err := New(srv.URL, time.Second).Train(context.Background(), "application/json",
strings.NewReader(`{"path":"/tmp/new.csv","model":"small"}`))
if err != nil {
t.Fatalf("Train: %v", err)
}
if status != http.StatusAccepted {
t.Fatalf("status=%d, want 202", status)
}
if !strings.Contains(string(body), `"job_id":"job-1"`) {
t.Fatalf("body=%s", body)
}
if gotMethod != http.MethodPost || gotPath != "/train" {
t.Fatalf("request=%s %s, want POST /train", gotMethod, gotPath)
}
if gotType != "application/json" || !strings.Contains(gotBody, "/tmp/new.csv") {
t.Fatalf("content-type=%q body=%q", gotType, gotBody)
}
}
func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method=%s, want GET", r.Method)
}
if r.URL.Path == "/train/job-1" {
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","error":null}`))
return
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
}))
defer srv.Close()
client := New(srv.URL, time.Second)
status, body, err := client.TrainStatus(context.Background(), "job-1")
if err != nil {
t.Fatalf("TrainStatus: %v", err)
}
if status != http.StatusOK || !strings.Contains(string(body), `"succeeded"`) {
t.Fatalf("status=%d body=%s", status, body)
}
status, body, err = client.TrainStatus(context.Background(), "missing")
if err != nil {
t.Fatalf("TrainStatus missing: %v", err)
}
if status != http.StatusNotFound || !strings.Contains(string(body), "job not found") {
t.Fatalf("status=%d body=%s, want forwarded 404", status, body)
}
}