-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualization_test.go
151 lines (133 loc) · 4.36 KB
/
visualization_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package redash_test
import (
"context"
"io"
"net/http"
"testing"
"github.com/araddon/dateparse"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/redash-go/v2"
)
func Test_UpdateVisualization_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/visualizations/1", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key " + testRedashAPIKey},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
if req.Body == nil {
assert.FailNow("req.Body is nil")
}
body, _ := io.ReadAll(req.Body)
assert.Equal(`{"description":"description","name":"name","type":"TABLE"}`, string(body))
return httpmock.NewStringResponse(http.StatusOK, `
{
"created_at": "2023-02-10T01:23:45.000Z",
"description": "description",
"id": 1,
"name": "Table",
"options": {},
"type": "TABLE",
"updated_at": "2023-02-10T01:23:45.000Z"
}
`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
res, err := client.UpdateVisualization(context.Background(), 1, &redash.UpdateVisualizationInput{
Description: "description",
Name: "name",
Type: "TABLE",
})
assert.NoError(err)
assert.Equal(&redash.Visualization{
CreatedAt: dateparse.MustParse("2023-02-10T01:23:45.000Z"),
Description: "description",
ID: 1,
Name: "Table",
Options: map[string]any{},
Query: redash.Query{},
Type: "TABLE",
UpdatedAt: dateparse.MustParse("2023-02-10T01:23:45.000Z"),
}, res)
}
func Test_UpdateVisualization_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/visualizations/1", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.UpdateVisualization(context.Background(), 1, &redash.UpdateVisualizationInput{
Description: "description",
Name: "name",
Type: "TABLE",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}
func Test_UpdateVisualization_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/visualizations/1", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.UpdateVisualization(context.Background(), 1, &redash.UpdateVisualizationInput{
Description: "description",
Name: "name",
Type: "TABLE",
})
assert.ErrorContains(err, "POST api/visualizations/1 failed: HTTP status code not OK: 503\nerror")
}
func Test_Visualization_Acc(t *testing.T) {
if !testAcc {
t.Skip()
}
assert := assert.New(t)
require := require.New(t)
client, _ := redash.NewClient(testRedashEndpoint, testRedashAPIKey)
ds, err := client.CreateDataSource(context.Background(), &redash.CreateDataSourceInput{
Name: "test-postgres-1",
Type: "pg",
Options: map[string]any{
"dbname": "postgres",
"host": "postgres",
"port": 5432,
"user": "postgres",
},
})
require.NoError(err)
defer func() {
client.DeleteDataSource(context.Background(), ds.ID) //nolint:errcheck
}()
query, _ := client.CreateQuery(context.Background(), &redash.CreateQueryInput{
DataSourceID: ds.ID,
Name: "test-query-1",
Query: "select 1",
})
defer func() {
client.ArchiveQuery(context.Background(), query.ID) //nolint:errcheck
}()
if len(query.Visualizations) < 1 {
assert.FailNow("len(query.Visualizations) < 1")
}
vizId := query.Visualizations[0].ID
viz, err := client.UpdateVisualization(context.Background(), vizId, &redash.UpdateVisualizationInput{
Name: "test-viz-1",
Description: "test-viz-1-desc",
})
require.NoError(err)
assert.Equal("test-viz-1", viz.Name)
assert.Equal("test-viz-1-desc", viz.Description)
}