|
| 1 | +package branch |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + qt "github.com/frankban/quicktest" |
| 16 | + |
| 17 | + "github.com/planetscale/cli/internal/cmdutil" |
| 18 | + "github.com/planetscale/cli/internal/config" |
| 19 | + "github.com/planetscale/cli/internal/printer" |
| 20 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 21 | +) |
| 22 | + |
| 23 | +const queryPatternsCSV = "normalized_sql,query_count\nselect ?,10\n" |
| 24 | + |
| 25 | +func queryPatternsTestHelper(org, baseURL string, format printer.Format, buf *bytes.Buffer) *cmdutil.Helper { |
| 26 | + p := printer.NewPrinter(&format) |
| 27 | + p.SetResourceOutput(buf) |
| 28 | + p.SetHumanOutput(bytes.NewBuffer(nil)) |
| 29 | + |
| 30 | + return &cmdutil.Helper{ |
| 31 | + Printer: p, |
| 32 | + Config: &config.Config{AccessToken: "token", Organization: org, BaseURL: baseURL}, |
| 33 | + Client: func() (*ps.Client, error) { |
| 34 | + return ps.NewClient(ps.WithBaseURL(baseURL), ps.WithAccessToken("token")) |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func queryPatternsServer(t *testing.T, c *qt.C, showStates []string) *httptest.Server { |
| 40 | + t.Helper() |
| 41 | + |
| 42 | + base := "/v1/organizations/my-org/databases/my-db/branches/my-branch/query-patterns" |
| 43 | + shows := 0 |
| 44 | + mux := http.NewServeMux() |
| 45 | + |
| 46 | + mux.HandleFunc(base, func(w http.ResponseWriter, r *http.Request) { |
| 47 | + c.Assert(r.Method, qt.Equals, http.MethodPost) |
| 48 | + c.Assert(r.Header.Get("Authorization"), qt.Equals, "Bearer token") |
| 49 | + w.Header().Set("Content-Type", "application/json") |
| 50 | + w.WriteHeader(http.StatusCreated) |
| 51 | + _ = json.NewEncoder(w).Encode(map[string]string{"id": "report1", "state": "pending"}) |
| 52 | + }) |
| 53 | + |
| 54 | + mux.HandleFunc(base+"/report1", func(w http.ResponseWriter, r *http.Request) { |
| 55 | + state := showStates[min(shows, len(showStates)-1)] |
| 56 | + shows++ |
| 57 | + w.Header().Set("Content-Type", "application/json") |
| 58 | + _ = json.NewEncoder(w).Encode(map[string]string{"id": "report1", "state": state}) |
| 59 | + }) |
| 60 | + |
| 61 | + mux.HandleFunc(base+"/report1/download", func(w http.ResponseWriter, r *http.Request) { |
| 62 | + http.Redirect(w, r, "/blob", http.StatusFound) |
| 63 | + }) |
| 64 | + |
| 65 | + mux.HandleFunc("/blob", func(w http.ResponseWriter, r *http.Request) { |
| 66 | + w.Header().Set("Content-Type", "application/gzip") |
| 67 | + gz := gzip.NewWriter(w) |
| 68 | + _, _ = gz.Write([]byte(queryPatternsCSV)) |
| 69 | + _ = gz.Close() |
| 70 | + }) |
| 71 | + |
| 72 | + server := httptest.NewServer(mux) |
| 73 | + t.Cleanup(server.Close) |
| 74 | + return server |
| 75 | +} |
| 76 | + |
| 77 | +func TestBranch_QueryPatternsDownloadCmd(t *testing.T) { |
| 78 | + c := qt.New(t) |
| 79 | + |
| 80 | + prevInterval := queryPatternsPollInterval |
| 81 | + queryPatternsPollInterval = 10 * time.Millisecond |
| 82 | + t.Cleanup(func() { queryPatternsPollInterval = prevInterval }) |
| 83 | + |
| 84 | + server := queryPatternsServer(t, c, []string{"pending", "completed"}) |
| 85 | + |
| 86 | + var buf bytes.Buffer |
| 87 | + ch := queryPatternsTestHelper("my-org", server.URL, printer.JSON, &buf) |
| 88 | + |
| 89 | + output := filepath.Join(t.TempDir(), "report.csv") |
| 90 | + |
| 91 | + cmd := QueryPatternsCmd(ch) |
| 92 | + cmd.SetArgs([]string{"download", "my-db", "my-branch", "--output", output}) |
| 93 | + err := cmd.Execute() |
| 94 | + |
| 95 | + c.Assert(err, qt.IsNil) |
| 96 | + |
| 97 | + data, err := os.ReadFile(output) |
| 98 | + c.Assert(err, qt.IsNil) |
| 99 | + c.Assert(string(data), qt.Equals, queryPatternsCSV) |
| 100 | + |
| 101 | + c.Assert(buf.String(), qt.JSONEquals, &QueryPatternsDownload{ |
| 102 | + ID: "report1", |
| 103 | + State: "completed", |
| 104 | + File: output, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func TestBranch_QueryPatternsDownloadCmd_Failed(t *testing.T) { |
| 109 | + c := qt.New(t) |
| 110 | + |
| 111 | + prevInterval := queryPatternsPollInterval |
| 112 | + queryPatternsPollInterval = 10 * time.Millisecond |
| 113 | + t.Cleanup(func() { queryPatternsPollInterval = prevInterval }) |
| 114 | + |
| 115 | + server := queryPatternsServer(t, c, []string{"failed"}) |
| 116 | + |
| 117 | + var buf bytes.Buffer |
| 118 | + ch := queryPatternsTestHelper("my-org", server.URL, printer.JSON, &buf) |
| 119 | + |
| 120 | + cmd := QueryPatternsCmd(ch) |
| 121 | + cmd.SetArgs([]string{"download", "my-db", "my-branch", "--output", filepath.Join(t.TempDir(), "report.csv")}) |
| 122 | + err := cmd.Execute() |
| 123 | + |
| 124 | + c.Assert(err, qt.IsNotNil) |
| 125 | + c.Assert(err.Error(), qt.Contains, "failed to generate") |
| 126 | +} |
| 127 | + |
| 128 | +func TestBranch_QueryPatternsDownloadCmd_NotFound(t *testing.T) { |
| 129 | + c := qt.New(t) |
| 130 | + |
| 131 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 | + w.Header().Set("Content-Type", "application/json") |
| 133 | + w.WriteHeader(http.StatusNotFound) |
| 134 | + fmt.Fprint(w, `{"code":"not_found","message":"Not Found"}`) |
| 135 | + })) |
| 136 | + t.Cleanup(server.Close) |
| 137 | + |
| 138 | + var buf bytes.Buffer |
| 139 | + ch := queryPatternsTestHelper("my-org", server.URL, printer.JSON, &buf) |
| 140 | + |
| 141 | + cmd := QueryPatternsCmd(ch) |
| 142 | + cmd.SetArgs([]string{"download", "my-db", "my-branch"}) |
| 143 | + err := cmd.Execute() |
| 144 | + |
| 145 | + c.Assert(err, qt.IsNotNil) |
| 146 | + c.Assert(err.Error(), qt.Contains, "query insights is not enabled") |
| 147 | +} |
0 commit comments