|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestInitListCmd(t *testing.T) { |
| 9 | + parent := &cobra.Command{} |
| 10 | + listCmd.ResetFlags() |
| 11 | + initListCmd(parent) |
| 12 | + flags := listCmd.HasAvailableLocalFlags() |
| 13 | + if flags == true { |
| 14 | + t.Fatalf("expected that command doesn't have flags") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestRunListCmd(t *testing.T) { |
| 19 | + testCases := map[string]struct { |
| 20 | + dataType string |
| 21 | + url string |
| 22 | + expectedErr interface{} |
| 23 | + }{ |
| 24 | + "Countries": {"countries", "/analytics/dns/countries", nil}, |
| 25 | + "Cities": {"cities", "/analytics/dns/city", nil}, |
| 26 | + "Wrong type": {"country", "/analytics/dns/country", "no data with type 'country'"}, |
| 27 | + } |
| 28 | + |
| 29 | + for name, tc := range testCases { |
| 30 | + t.Run(name, func(t *testing.T) { |
| 31 | + tr := &recordingTransport{} |
| 32 | + c, err := newTestPerfopsClient(tr) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("unexpected error %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + err = runListCmd(c, tc.dataType) |
| 38 | + |
| 39 | + if tc.expectedErr != nil { |
| 40 | + if tc.expectedErr != err.Error() { |
| 41 | + t.Fatalf("expected %v error; got %v", tc.expectedErr, err) |
| 42 | + } |
| 43 | + |
| 44 | + // No reason to continue if we check for error case |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if got, exp := tr.req.URL.Path, tc.url; got != exp { |
| 49 | + t.Fatalf("expected %v; got %v", exp, got) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
0 commit comments