|
| 1 | +package matview_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 TestCreateSuccess(t *testing.T) { |
| 14 | + var got models.UpsertMaterializedViewRequest |
| 15 | + mock := &mockClient{ |
| 16 | + upsertFn: func(req models.UpsertMaterializedViewRequest) (*models.UpsertMaterializedViewResponse, error) { |
| 17 | + got = req |
| 18 | + return &models.UpsertMaterializedViewResponse{ |
| 19 | + SQLID: "dune.my_team.result_token_summary", |
| 20 | + ExecutionID: "01HZ065", |
| 21 | + }, nil |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + root, buf := newTestRoot(mock) |
| 26 | + root.SetArgs([]string{ |
| 27 | + "matview", "create", |
| 28 | + "--name", "result_token_summary", |
| 29 | + "--query-id", "12345", |
| 30 | + "--performance", "medium", |
| 31 | + }) |
| 32 | + require.NoError(t, root.Execute()) |
| 33 | + |
| 34 | + assert.Equal(t, "result_token_summary", got.Name) |
| 35 | + assert.Equal(t, 12345, got.QueryID) |
| 36 | + assert.True(t, got.IsPrivate, "private should default to true") |
| 37 | + assert.Equal(t, "medium", got.Performance) |
| 38 | + assert.Nil(t, got.CronExpression) |
| 39 | + |
| 40 | + out := buf.String() |
| 41 | + assert.Contains(t, out, "Created materialized view dune.my_team.result_token_summary") |
| 42 | + assert.Contains(t, out, "Execution ID: 01HZ065") |
| 43 | +} |
| 44 | + |
| 45 | +func TestCreateWithCron(t *testing.T) { |
| 46 | + var got models.UpsertMaterializedViewRequest |
| 47 | + mock := &mockClient{ |
| 48 | + upsertFn: func(req models.UpsertMaterializedViewRequest) (*models.UpsertMaterializedViewResponse, error) { |
| 49 | + got = req |
| 50 | + return &models.UpsertMaterializedViewResponse{SQLID: "dune.t.result_x", ExecutionID: "e1"}, nil |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + root, _ := newTestRoot(mock) |
| 55 | + root.SetArgs([]string{ |
| 56 | + "matview", "create", |
| 57 | + "--name", "result_scheduled", |
| 58 | + "--query-id", "7", |
| 59 | + "--cron", "0 */6 * * *", |
| 60 | + "--private=false", |
| 61 | + }) |
| 62 | + require.NoError(t, root.Execute()) |
| 63 | + |
| 64 | + require.NotNil(t, got.CronExpression) |
| 65 | + assert.Equal(t, "0 */6 * * *", *got.CronExpression) |
| 66 | + assert.False(t, got.IsPrivate) |
| 67 | +} |
| 68 | + |
| 69 | +func TestCreateInvalidName(t *testing.T) { |
| 70 | + root, _ := newTestRoot(&mockClient{}) |
| 71 | + root.SetArgs([]string{"matview", "create", "--name", "not_result", "--query-id", "1"}) |
| 72 | + err := root.Execute() |
| 73 | + require.Error(t, err) |
| 74 | + assert.Contains(t, err.Error(), "invalid matview name") |
| 75 | +} |
| 76 | + |
| 77 | +func TestCreateExpiresAtRequiresCron(t *testing.T) { |
| 78 | + root, _ := newTestRoot(&mockClient{}) |
| 79 | + root.SetArgs([]string{ |
| 80 | + "matview", "create", |
| 81 | + "--name", "result_token_summary", |
| 82 | + "--query-id", "1", |
| 83 | + "--expires-at", "2026-09-11T23:59:59Z", |
| 84 | + }) |
| 85 | + err := root.Execute() |
| 86 | + require.Error(t, err) |
| 87 | + assert.Contains(t, err.Error(), "--expires-at requires --cron") |
| 88 | +} |
| 89 | + |
| 90 | +func TestCreateInvalidPerformance(t *testing.T) { |
| 91 | + root, _ := newTestRoot(&mockClient{}) |
| 92 | + root.SetArgs([]string{ |
| 93 | + "matview", "create", |
| 94 | + "--name", "result_token_summary", |
| 95 | + "--query-id", "1", |
| 96 | + "--performance", "huge", |
| 97 | + }) |
| 98 | + err := root.Execute() |
| 99 | + require.Error(t, err) |
| 100 | + assert.Contains(t, err.Error(), "invalid performance tier") |
| 101 | +} |
| 102 | + |
| 103 | +func TestCreateJSONOutput(t *testing.T) { |
| 104 | + mock := &mockClient{ |
| 105 | + upsertFn: func(models.UpsertMaterializedViewRequest) (*models.UpsertMaterializedViewResponse, error) { |
| 106 | + return &models.UpsertMaterializedViewResponse{SQLID: "dune.t.result_x", ExecutionID: "e1"}, nil |
| 107 | + }, |
| 108 | + } |
| 109 | + root, buf := newTestRoot(mock) |
| 110 | + root.SetArgs([]string{"matview", "create", "--name", "result_x_long", "--query-id", "1", "-o", "json"}) |
| 111 | + require.NoError(t, root.Execute()) |
| 112 | + |
| 113 | + var resp models.UpsertMaterializedViewResponse |
| 114 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) |
| 115 | + assert.Equal(t, "dune.t.result_x", resp.SQLID) |
| 116 | +} |
| 117 | + |
| 118 | +func TestCreateAPIError(t *testing.T) { |
| 119 | + mock := &mockClient{ |
| 120 | + upsertFn: func(models.UpsertMaterializedViewRequest) (*models.UpsertMaterializedViewResponse, error) { |
| 121 | + return nil, errors.New("api: plan does not allow private matviews") |
| 122 | + }, |
| 123 | + } |
| 124 | + root, _ := newTestRoot(mock) |
| 125 | + root.SetArgs([]string{"matview", "create", "--name", "result_token_summary", "--query-id", "1"}) |
| 126 | + err := root.Execute() |
| 127 | + require.Error(t, err) |
| 128 | + assert.Contains(t, err.Error(), "plan does not allow private matviews") |
| 129 | +} |
0 commit comments