|
| 1 | +package query_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/duneanalytics/duneapi-client-go/models" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestArchiveSuccess(t *testing.T) { |
| 14 | + mock := &mockClient{ |
| 15 | + archiveQueryFn: func(id int) (*models.UpdateQueryResponse, error) { |
| 16 | + assert.Equal(t, 4125432, id) |
| 17 | + return &models.UpdateQueryResponse{QueryID: 4125432}, nil |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + root, buf := newTestRoot(mock) |
| 22 | + root.SetArgs([]string{"query", "archive", "4125432"}) |
| 23 | + require.NoError(t, root.Execute()) |
| 24 | + assert.Equal(t, "Archived query 4125432\n", buf.String()) |
| 25 | +} |
| 26 | + |
| 27 | +func TestArchiveJSONOutput(t *testing.T) { |
| 28 | + mock := &mockClient{ |
| 29 | + archiveQueryFn: func(_ int) (*models.UpdateQueryResponse, error) { |
| 30 | + return &models.UpdateQueryResponse{QueryID: 4125432}, nil |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + root, buf := newTestRoot(mock) |
| 35 | + root.SetArgs([]string{"query", "archive", "4125432", "-o", "json"}) |
| 36 | + require.NoError(t, root.Execute()) |
| 37 | + |
| 38 | + var got map[string]int |
| 39 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &got)) |
| 40 | + assert.Equal(t, 4125432, got["query_id"]) |
| 41 | +} |
| 42 | + |
| 43 | +func TestArchiveMissingArgument(t *testing.T) { |
| 44 | + root, _ := newTestRoot(&mockClient{}) |
| 45 | + root.SetArgs([]string{"query", "archive"}) |
| 46 | + err := root.Execute() |
| 47 | + require.Error(t, err) |
| 48 | + assert.Contains(t, err.Error(), "accepts 1 arg(s)") |
| 49 | +} |
| 50 | + |
| 51 | +func TestArchiveNonIntegerID(t *testing.T) { |
| 52 | + root, _ := newTestRoot(&mockClient{}) |
| 53 | + root.SetArgs([]string{"query", "archive", "abc"}) |
| 54 | + err := root.Execute() |
| 55 | + require.Error(t, err) |
| 56 | + assert.Contains(t, err.Error(), "invalid query ID") |
| 57 | +} |
| 58 | + |
| 59 | +func TestArchiveAPIError(t *testing.T) { |
| 60 | + mock := &mockClient{ |
| 61 | + archiveQueryFn: func(_ int) (*models.UpdateQueryResponse, error) { |
| 62 | + return nil, errors.New("api: not found") |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + root, _ := newTestRoot(mock) |
| 67 | + root.SetArgs([]string{"query", "archive", "999"}) |
| 68 | + err := root.Execute() |
| 69 | + require.Error(t, err) |
| 70 | + assert.Contains(t, err.Error(), "api: not found") |
| 71 | +} |
0 commit comments