|
| 1 | +package dataset_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/duneanalytics/cli/cmd/dataset" |
| 11 | + "github.com/duneanalytics/cli/cmdutil" |
| 12 | + "github.com/duneanalytics/duneapi-client-go/dune" |
| 13 | + "github.com/duneanalytics/duneapi-client-go/models" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type mockClient struct { |
| 20 | + dune.DuneClient |
| 21 | + searchDatasetsFn func(models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockClient) SearchDatasets(req models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) { |
| 25 | + return m.searchDatasetsFn(req) |
| 26 | +} |
| 27 | + |
| 28 | +func newTestRoot(mock dune.DuneClient) (*cobra.Command, *bytes.Buffer) { |
| 29 | + root := &cobra.Command{ |
| 30 | + Use: "dune", |
| 31 | + PersistentPreRun: func(cmd *cobra.Command, _ []string) { |
| 32 | + cmdutil.SetClient(cmd, mock) |
| 33 | + }, |
| 34 | + } |
| 35 | + root.SetContext(context.Background()) |
| 36 | + root.AddCommand(dataset.NewDatasetCmd()) |
| 37 | + |
| 38 | + var buf bytes.Buffer |
| 39 | + root.SetOut(&buf) |
| 40 | + |
| 41 | + return root, &buf |
| 42 | +} |
| 43 | + |
| 44 | +func TestSearchSuccess(t *testing.T) { |
| 45 | + dt := "spell" |
| 46 | + var gotReq models.SearchDatasetsRequest |
| 47 | + |
| 48 | + mock := &mockClient{ |
| 49 | + searchDatasetsFn: func(req models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) { |
| 50 | + gotReq = req |
| 51 | + return &models.SearchDatasetsResponse{ |
| 52 | + Total: 1, |
| 53 | + Results: []models.SearchDatasetResult{ |
| 54 | + { |
| 55 | + FullName: "dex.trades", |
| 56 | + Category: "spell", |
| 57 | + DatasetType: &dt, |
| 58 | + Blockchains: []string{"ethereum", "arbitrum"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + Pagination: models.SearchDatasetsPagination{ |
| 62 | + Limit: 5, |
| 63 | + Offset: 0, |
| 64 | + HasMore: false, |
| 65 | + }, |
| 66 | + }, nil |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + root, buf := newTestRoot(mock) |
| 71 | + root.SetArgs([]string{ |
| 72 | + "dataset", "search", |
| 73 | + "--query", "dex trades", |
| 74 | + "--categories", "spell", |
| 75 | + "--blockchains", "ethereum", |
| 76 | + "--limit", "5", |
| 77 | + }) |
| 78 | + |
| 79 | + require.NoError(t, root.Execute()) |
| 80 | + |
| 81 | + // Verify flags mapped to request |
| 82 | + assert.Equal(t, "dex trades", *gotReq.Query) |
| 83 | + assert.Equal(t, []string{"spell"}, gotReq.Categories) |
| 84 | + assert.Equal(t, []string{"ethereum"}, gotReq.Blockchains) |
| 85 | + assert.Equal(t, int32(5), *gotReq.Limit) |
| 86 | + |
| 87 | + // Verify table output |
| 88 | + out := buf.String() |
| 89 | + assert.Contains(t, out, "dex.trades") |
| 90 | + assert.Contains(t, out, "spell") |
| 91 | + assert.Contains(t, out, "ethereum, arbitrum") |
| 92 | + assert.Contains(t, out, "1 of 1 results") |
| 93 | +} |
| 94 | + |
| 95 | +func TestSearchJSON(t *testing.T) { |
| 96 | + mock := &mockClient{ |
| 97 | + searchDatasetsFn: func(req models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) { |
| 98 | + return &models.SearchDatasetsResponse{ |
| 99 | + Total: 1, |
| 100 | + Results: []models.SearchDatasetResult{ |
| 101 | + { |
| 102 | + FullName: "ethereum.transactions", |
| 103 | + Category: "canonical", |
| 104 | + }, |
| 105 | + }, |
| 106 | + Pagination: models.SearchDatasetsPagination{ |
| 107 | + Limit: 20, |
| 108 | + Offset: 0, |
| 109 | + HasMore: false, |
| 110 | + }, |
| 111 | + }, nil |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + root, buf := newTestRoot(mock) |
| 116 | + root.SetArgs([]string{"dataset", "search", "--query", "ethereum", "-o", "json"}) |
| 117 | + |
| 118 | + require.NoError(t, root.Execute()) |
| 119 | + |
| 120 | + var resp models.SearchDatasetsResponse |
| 121 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) |
| 122 | + assert.Equal(t, int32(1), resp.Total) |
| 123 | + assert.Equal(t, "ethereum.transactions", resp.Results[0].FullName) |
| 124 | +} |
| 125 | + |
| 126 | +func TestSearchError(t *testing.T) { |
| 127 | + mock := &mockClient{ |
| 128 | + searchDatasetsFn: func(req models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) { |
| 129 | + return nil, fmt.Errorf("API error: unauthorized") |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + root, _ := newTestRoot(mock) |
| 134 | + root.SetArgs([]string{"dataset", "search", "--query", "test"}) |
| 135 | + |
| 136 | + err := root.Execute() |
| 137 | + require.Error(t, err) |
| 138 | + assert.Contains(t, err.Error(), "API error: unauthorized") |
| 139 | +} |
0 commit comments