From b5dd3ea02e43f718bab656f4e663ac6ce9bc4dd2 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Thu, 5 Jan 2023 18:34:01 -0600 Subject: [PATCH] Change JSON tag name on WireQueryResponse from execution-time to exec_time (#2394) * Change JSON response name from exec_time to execution-time Execution time stopped working in the CLI because it uses the latest json tag. * Wait, don't break the interface. * Add a test for the sql response json tags. This is to make sure that if someone like Travis just goes and changes a tag name to be more consistent, that we perhaps catch that before it gets to the end user. * Change exec_time to execution-time after all --- http_handler.go | 6 ++--- http_handler_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ server/server.go | 1 + 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/http_handler.go b/http_handler.go index fd0af0531..c17c2775d 100644 --- a/http_handler.go +++ b/http_handler.go @@ -1438,9 +1438,9 @@ func (h *Handler) handlePostSQL(w http.ResponseWriter, r *http.Request) { var value []byte value, err = json.Marshal(execTime) if err != nil { - w.Write([]byte(`,"exec_time": 0`)) + w.Write([]byte(`,"execution-time": 0`)) } else { - w.Write([]byte(`,"exec_time":`)) + w.Write([]byte(`,"execution-time":`)) w.Write(value) } w.Write([]byte("}")) @@ -1488,7 +1488,7 @@ func (h *Handler) handlePostSQL(w http.ResponseWriter, r *http.Request) { if err != nil { planBytes = []byte(`"PROBLEM ENCODING QUERY PLAN"`) } - w.Write([]byte(`,"queryPlan":`)) + w.Write([]byte(`,"query-plan":`)) w.Write(planBytes) } } diff --git a/http_handler_test.go b/http_handler_test.go index 81bdc34e6..2dd8eaae1 100644 --- a/http_handler_test.go +++ b/http_handler_test.go @@ -20,6 +20,7 @@ import ( "github.com/molecula/featurebase/v3/encoding/proto" "github.com/molecula/featurebase/v3/server" "github.com/molecula/featurebase/v3/test" + "github.com/stretchr/testify/assert" ) func TestHandlerOptions(t *testing.T) { @@ -541,6 +542,64 @@ func TestGetViewAndDelete(t *testing.T) { } } +// TestHandlerSQL tests that the json coming back from a POST /sql request has +// the expected json tags. +func TestHandlerSQL(t *testing.T) { + cfg := server.NewConfig() + cfg.SQL.EndpointEnabled = true + c := test.MustRunCluster(t, 1, []server.CommandOption{ + server.OptCommandConfig(cfg), + }) + defer c.Close() + + m := c.GetPrimary() + + tests := []struct { + name string + url string + sql string + expKeys []string + }{ + { + name: "sql", + url: "/sql", + sql: "show tables", + expKeys: []string{"schema", "data", "execution-time"}, + }, + { + name: "sql-with-plan", + url: "/sql?plan=1", + sql: "show tables", + expKeys: []string{"schema", "data", "query-plan", "execution-time"}, + }, + { + name: "invalid-sql", + url: "/sql", + sql: "invalid sql", + expKeys: []string{"error", "execution-time"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sqlURL := fmt.Sprintf("%s%s", m.URL(), tt.url) + resp := test.Do(t, "POST", sqlURL, tt.sql) + if resp.StatusCode != http.StatusOK { + t.Errorf("post sql, status: %d, body=%s", resp.StatusCode, resp.Body) + } + + out := make(map[string]interface{}) + assert.NoError(t, json.Unmarshal([]byte(resp.Body), &out)) + + keys := make([]string, 0, len(out)) + for k := range out { + keys = append(keys, k) + } + + assert.ElementsMatch(t, tt.expKeys, keys) + }) + } +} + func TestTranslationHandlers(t *testing.T) { // reusable data for the tests nameBytes, err := json.Marshal([]string{"a", "b", "c"}) diff --git a/server/server.go b/server/server.go index c765f9007..f535b5f0a 100644 --- a/server/server.go +++ b/server/server.go @@ -128,6 +128,7 @@ func OptCommandConfig(config *Config) CommandOption { c.Config.TLS = config.TLS c.Config.MDSAddress = config.MDSAddress c.Config.WriteLogger = config.WriteLogger + c.Config.SQL.EndpointEnabled = config.SQL.EndpointEnabled return nil } c.Config = config