package slurm import ( "context" "fmt" "net/http" "net/http/httptest" "net/url" "strings" "testing" ) func TestPartitionsService_GetPartitions(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurm/v0.0.40/partitions", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") fmt.Fprint(w, `{"partitions": [], "last_update": {}}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) resp, _, err := client.Partitions.GetPartitions(context.Background(), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestPartitionsService_GetPartitions_WithOptions(t *testing.T) { var capturedQuery url.Values mux := http.NewServeMux() mux.HandleFunc("/slurm/v0.0.40/partitions", func(w http.ResponseWriter, r *http.Request) { capturedQuery = r.URL.Query() testMethod(t, r, "GET") fmt.Fprint(w, `{"partitions": [], "last_update": {}}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetPartitionsOptions{ UpdateTime: Ptr(int64(1700000000)), } _, _, err := client.Partitions.GetPartitions(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if capturedQuery.Get("update_time") != "1700000000" { t.Errorf("expected update_time=1700000000, got %s", capturedQuery.Get("update_time")) } } func TestPartitionsService_GetPartition(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurm/v0.0.40/partition/gpu", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") fmt.Fprint(w, `{"partitions": [{"name": "gpu"}], "last_update": {}}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) resp, _, err := client.Partitions.GetPartition(context.Background(), "gpu", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } if resp.Partitions == nil || len(*resp.Partitions) != 1 { t.Fatalf("expected 1 partition, got %v", resp.Partitions) } } func TestPartitionsService_GetPartitions_Error(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurm/v0.0.40/partitions", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) fmt.Fprint(w, `{"errors": [{"error": "internal error"}]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) _, _, err := client.Partitions.GetPartitions(context.Background(), nil) if err == nil { t.Fatal("expected error for 500 response") } if !strings.Contains(err.Error(), "500") { t.Errorf("expected error to contain 500, got %v", err) } } func TestPartitionsService_GetPartition_Error(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurm/v0.0.40/partition/nonexistent", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprint(w, `{"errors": [{"error": "partition not found"}]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) _, _, err := client.Partitions.GetPartition(context.Background(), "nonexistent", nil) if err == nil { t.Fatal("expected error for 404 response") } }