Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions cmd/query/create_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
package query_test

import (
"bytes"
"context"
"encoding/json"
"errors"
"testing"

"github.com/duneanalytics/cli/cmd/query"
"github.com/duneanalytics/cli/cmdutil"
"github.com/duneanalytics/duneapi-client-go/dune"
"github.com/duneanalytics/duneapi-client-go/models"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// mockClient embeds the interface so unimplemented methods panic.
type mockClient struct {
dune.DuneClient
createQueryFn func(models.CreateQueryRequest) (*models.CreateQueryResponse, error)
}

func (m *mockClient) CreateQuery(req models.CreateQueryRequest) (*models.CreateQueryResponse, error) {
return m.createQueryFn(req)
}

// newTestRoot builds a root → query → create command tree with the mock injected.
func newTestRoot(mock dune.DuneClient) (*cobra.Command, *bytes.Buffer) {
root := &cobra.Command{
Use: "dune",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
cmdutil.SetClient(cmd, mock)
},
}
root.SetContext(context.Background())

root.AddCommand(query.NewQueryCmd())

var buf bytes.Buffer
root.SetOut(&buf)

return root, &buf
}

func TestCreateSuccess(t *testing.T) {
mock := &mockClient{
createQueryFn: func(req models.CreateQueryRequest) (*models.CreateQueryResponse, error) {
Expand Down
64 changes: 64 additions & 0 deletions cmd/query/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package query

import (
"fmt"
"strconv"
"strings"

"github.com/duneanalytics/cli/cmdutil"
"github.com/duneanalytics/cli/output"
"github.com/spf13/cobra"
)

func newGetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get <query-id>",
Short: "Get a saved query",
Args: cobra.ExactArgs(1),
RunE: runGet,
}

output.AddFormatFlag(cmd, "text")

return cmd
}

func runGet(cmd *cobra.Command, args []string) error {
queryID, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("invalid query ID %q: must be an integer", args[0])
}

client := cmdutil.ClientFromCmd(cmd)

resp, err := client.GetQuery(queryID)
if err != nil {
return err
}

w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
default:
fmt.Fprintf(w, "ID: %d\n", resp.QueryID)
fmt.Fprintf(w, "Name: %s\n", resp.Name)
if resp.Description != "" {
fmt.Fprintf(w, "Description: %s\n", resp.Description)
}
fmt.Fprintf(w, "Owner: %s\n", resp.Owner)
fmt.Fprintf(w, "Engine: %s\n", resp.QueryEngine)
fmt.Fprintf(w, "Version: %d\n", resp.Version)
fmt.Fprintf(w, "Private: %t\n", resp.IsPrivate)
fmt.Fprintf(w, "Archived: %t\n", resp.IsArchived)
if len(resp.Tags) > 0 {
fmt.Fprintf(w, "Tags: %s\n", strings.Join(resp.Tags, ", "))
}
fmt.Fprintln(w)
fmt.Fprintln(w, "SQL:")
for _, line := range strings.Split(resp.QuerySQL, "\n") {
fmt.Fprintf(w, " %s\n", line)
}
return nil
}
}
125 changes: 125 additions & 0 deletions cmd/query/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package query_test

import (
"encoding/json"
"errors"
"testing"

"github.com/duneanalytics/duneapi-client-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testQueryResponse = &models.GetQueryResponse{
QueryID: 4125432,
Name: "Test Query",
Description: "A test",
QuerySQL: "SELECT 1",
Owner: "user123",
IsPrivate: false,
IsArchived: false,
IsUnsaved: false,
Version: 3,
QueryEngine: "v2 DuneSQL",
Tags: []string{"defi", "test"},
Parameters: nil,
}

func TestGetSuccess(t *testing.T) {
mock := &mockClient{
getQueryFn: func(id int) (*models.GetQueryResponse, error) {
assert.Equal(t, 4125432, id)
return testQueryResponse, nil
},
}

root, buf := newTestRoot(mock)
root.SetArgs([]string{"query", "get", "4125432"})
require.NoError(t, root.Execute())

out := buf.String()
assert.Contains(t, out, "ID: 4125432")
assert.Contains(t, out, "Name: Test Query")
assert.Contains(t, out, "Description: A test")
assert.Contains(t, out, "Owner: user123")
assert.Contains(t, out, "Engine: v2 DuneSQL")
assert.Contains(t, out, "Version: 3")
assert.Contains(t, out, "Private: false")
assert.Contains(t, out, "Archived: false")
assert.Contains(t, out, "Tags: defi, test")
assert.Contains(t, out, "SQL:")
assert.Contains(t, out, " SELECT 1")
}

func TestGetJSONOutput(t *testing.T) {
mock := &mockClient{
getQueryFn: func(_ int) (*models.GetQueryResponse, error) {
return testQueryResponse, nil
},
}

root, buf := newTestRoot(mock)
root.SetArgs([]string{"query", "get", "4125432", "-o", "json"})
require.NoError(t, root.Execute())

var got models.GetQueryResponse
require.NoError(t, json.Unmarshal(buf.Bytes(), &got))
assert.Equal(t, 4125432, got.QueryID)
assert.Equal(t, "Test Query", got.Name)
assert.Equal(t, "SELECT 1", got.QuerySQL)
}

func TestGetMissingArgument(t *testing.T) {
root, _ := newTestRoot(&mockClient{})
root.SetArgs([]string{"query", "get"})
err := root.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
}

func TestGetNonIntegerID(t *testing.T) {
root, _ := newTestRoot(&mockClient{})
root.SetArgs([]string{"query", "get", "abc"})
err := root.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid query ID")
}

func TestGetAPIError(t *testing.T) {
mock := &mockClient{
getQueryFn: func(_ int) (*models.GetQueryResponse, error) {
return nil, errors.New("api: not found")
},
}

root, _ := newTestRoot(mock)
root.SetArgs([]string{"query", "get", "999"})
err := root.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "api: not found")
}

func TestGetEmptyDescriptionAndTags(t *testing.T) {
mock := &mockClient{
getQueryFn: func(_ int) (*models.GetQueryResponse, error) {
return &models.GetQueryResponse{
QueryID: 1,
Name: "Minimal",
QuerySQL: "SELECT 1",
Owner: "user",
QueryEngine: "v2 DuneSQL",
Version: 1,
}, nil
},
}

root, buf := newTestRoot(mock)
root.SetArgs([]string{"query", "get", "1"})
require.NoError(t, root.Execute())

out := buf.String()
assert.NotContains(t, out, "Description:")
assert.NotContains(t, out, "Tags:")
assert.Contains(t, out, "ID: 1")
assert.Contains(t, out, "Name: Minimal")
}
1 change: 1 addition & 0 deletions cmd/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ func NewQueryCmd() *cobra.Command {
Short: "Manage Dune queries",
}
cmd.AddCommand(newCreateCmd())
cmd.AddCommand(newGetCmd())
return cmd
}
44 changes: 44 additions & 0 deletions cmd/query/testutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package query_test

import (
"bytes"
"context"

"github.com/duneanalytics/cli/cmd/query"
"github.com/duneanalytics/cli/cmdutil"
"github.com/duneanalytics/duneapi-client-go/dune"
"github.com/duneanalytics/duneapi-client-go/models"
"github.com/spf13/cobra"
)

// mockClient embeds the interface so unimplemented methods panic.
type mockClient struct {
dune.DuneClient
createQueryFn func(models.CreateQueryRequest) (*models.CreateQueryResponse, error)
getQueryFn func(int) (*models.GetQueryResponse, error)
}

func (m *mockClient) CreateQuery(req models.CreateQueryRequest) (*models.CreateQueryResponse, error) {
return m.createQueryFn(req)
}

func (m *mockClient) GetQuery(queryID int) (*models.GetQueryResponse, error) {
return m.getQueryFn(queryID)
}

// newTestRoot builds a root → query command tree with the mock injected.
func newTestRoot(mock dune.DuneClient) (*cobra.Command, *bytes.Buffer) {
root := &cobra.Command{
Use: "dune",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
cmdutil.SetClient(cmd, mock)
},
}
root.SetContext(context.Background())
root.AddCommand(query.NewQueryCmd())

var buf bytes.Buffer
root.SetOut(&buf)

return root, &buf
}