diff --git a/api.go b/api.go index f7d9001f4..3d65dba70 100644 --- a/api.go +++ b/api.go @@ -127,6 +127,17 @@ func NewAPI(opts ...apiOption) (*API, error) { return api, nil } +// Setter for API options. +func (api *API) SetAPIOpetions(opts ...apiOption) error { + for _, opt := range opts { + err := opt(api) + if err != nil { + return errors.Wrap(err, "applying option") + } + } + return nil +} + // validAPIMethods specifies the api methods that are valid for each // cluster state. var validAPIMethods = map[disco.ClusterState]map[apiMethod]struct{}{ diff --git a/server/handler_test.go b/server/handler_test.go index ea6c74e4f..e7212892d 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -32,7 +32,7 @@ import ( "testing" "time" - "github.com/molecula/featurebase/v2" + pilosa "github.com/molecula/featurebase/v2" "github.com/molecula/featurebase/v2/boltdb" "github.com/molecula/featurebase/v2/encoding/proto" "github.com/molecula/featurebase/v2/http" @@ -344,6 +344,30 @@ func TestHandler_Endpoints(t *testing.T) { } }) + t.Run("SchemaDetailsOff", func(t *testing.T) { + cmd.API.SetAPIOpetions(pilosa.OptAPISchemaDetailsOn(false)) + defer cmd.API.SetAPIOpetions(pilosa.OptAPISchemaDetailsOn(true)) + w := httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema/details", nil)) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + + var bodySchema pilosa.Schema + if err := json.Unmarshal(w.Body.Bytes(), + &bodySchema); err != nil { + t.Fatalf("unexpected unmarshalling error: %v", err) + } + + for _, i := range bodySchema.Indexes { + for _, f := range i.Fields { + if f.Cardinality != nil { + t.Fatalf("expected nil cardinality, got: %v", *f.Cardinality) + } + } + } + }) + t.Run("Import", func(t *testing.T) { indexInfo, err := cmd.API.Schema(context.Background(), false) if err != nil {