Skip to content

Commit 79cffaf

Browse files
committed
cleanup
1 parent c6b1e8f commit 79cffaf

4 files changed

Lines changed: 126 additions & 3 deletions

File tree

cmd/query/archive.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package query
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/duneanalytics/cli/cmdutil"
8+
"github.com/duneanalytics/cli/output"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func newArchiveCmd() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "archive <query-id>",
15+
Short: "Archive a saved query",
16+
Args: cobra.ExactArgs(1),
17+
RunE: runArchive,
18+
}
19+
20+
output.AddFormatFlag(cmd, "text")
21+
22+
return cmd
23+
}
24+
25+
func runArchive(cmd *cobra.Command, args []string) error {
26+
queryID, err := strconv.Atoi(args[0])
27+
if err != nil {
28+
return fmt.Errorf("invalid query ID %q: must be an integer", args[0])
29+
}
30+
31+
client := cmdutil.ClientFromCmd(cmd)
32+
33+
resp, err := client.ArchiveQuery(queryID)
34+
if err != nil {
35+
return err
36+
}
37+
38+
w := cmd.OutOrStdout()
39+
switch output.FormatFromCmd(cmd) {
40+
case output.FormatJSON:
41+
return output.PrintJSON(w, resp)
42+
default:
43+
fmt.Fprintf(w, "Archived query %d\n", resp.QueryID)
44+
return nil
45+
}
46+
}

cmd/query/archive_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

cmd/query/query.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ func NewQueryCmd() *cobra.Command {
1111
cmd.AddCommand(newCreateCmd())
1212
cmd.AddCommand(newGetCmd())
1313
cmd.AddCommand(newUpdateCmd())
14+
cmd.AddCommand(newArchiveCmd())
1415
return cmd
1516
}

cmd/query/testutil_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
// mockClient embeds the interface so unimplemented methods panic.
1515
type mockClient struct {
1616
dune.DuneClient
17-
createQueryFn func(models.CreateQueryRequest) (*models.CreateQueryResponse, error)
18-
getQueryFn func(int) (*models.GetQueryResponse, error)
19-
updateQueryFn func(int, models.UpdateQueryRequest) (*models.UpdateQueryResponse, error)
17+
createQueryFn func(models.CreateQueryRequest) (*models.CreateQueryResponse, error)
18+
getQueryFn func(int) (*models.GetQueryResponse, error)
19+
updateQueryFn func(int, models.UpdateQueryRequest) (*models.UpdateQueryResponse, error)
20+
archiveQueryFn func(int) (*models.UpdateQueryResponse, error)
2021
}
2122

2223
func (m *mockClient) CreateQuery(req models.CreateQueryRequest) (*models.CreateQueryResponse, error) {
@@ -31,6 +32,10 @@ func (m *mockClient) UpdateQuery(queryID int, req models.UpdateQueryRequest) (*m
3132
return m.updateQueryFn(queryID, req)
3233
}
3334

35+
func (m *mockClient) ArchiveQuery(queryID int) (*models.UpdateQueryResponse, error) {
36+
return m.archiveQueryFn(queryID)
37+
}
38+
3439
// newTestRoot builds a root → query command tree with the mock injected.
3540
func newTestRoot(mock dune.DuneClient) (*cobra.Command, *bytes.Buffer) {
3641
root := &cobra.Command{

0 commit comments

Comments
 (0)