-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_test.go
150 lines (137 loc) · 3.86 KB
/
status_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
package redash_test
import (
"context"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/redash-go/v2"
)
func Test_GetStatus_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/status.json", 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,
)
return httpmock.NewStringResponse(http.StatusOK, `
{
"dashboards_count": 2,
"database_metrics": {
"metrics": [
[
"Query Results Size",
49152
],
[
"Redash DB Size",
9371820
]
]
},
"manager": {
"last_refresh_at": "1676217265.221729",
"outdated_queries_count": "0",
"query_ids": "[]",
"queues": {
"celery": {
"size": 0
},
"queries": {
"size": 0
},
"scheduled_queries": {
"size": 0
}
}
},
"queries_count": 5,
"query_results_count": 4,
"redis_used_memory": 2383280,
"redis_used_memory_human": "2.27M",
"unused_query_results_count": 0,
"version": "8.0.0+b32245",
"widgets_count": 0,
"workers": []
}
`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
res, err := client.GetStatus(context.Background())
assert.NoError(err)
assert.Equal(&redash.Status{
DashboardsCount: 2,
DatabaseMetrics: redash.StatusDatabaseMetrics{
Metrics: [][]any{
{"Query Results Size", float64(49152)},
{"Redash DB Size", 9.37182e+06},
},
},
Manager: redash.StatusManager{
LastRefreshAt: "1676217265.221729",
OutdatedQueriesCount: "0",
QueryIds: "[]",
Queues: redash.StatusManagerQueues{
Celery: redash.StatusManagerQueuesCelery{
Size: 0,
},
Queries: redash.StatusManagerQueuesQueries{
Size: 0,
},
ScheduledQueries: redash.StatusManagerQueuesScheduledQueries{
Size: 0,
},
},
},
QueriesCount: 5,
QueryResultsCount: 4,
RedisUsedMemory: 2383280,
RedisUsedMemoryHuman: "2.27M",
UnusedQueryResultsCount: 0,
Version: "8.0.0+b32245",
WidgetsCount: 0,
Workers: []any{},
}, res)
}
func Test_GetStatus_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/status.json", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetStatus(context.Background())
assert.ErrorContains(err, "GET status.json failed: HTTP status code not OK: 503\nerror")
}
func Test_GetStatus_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/status.json", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetStatus(context.Background())
assert.ErrorContains(err, "Read response body failed: IO error")
}
func Test_GetStatus_Acc(t *testing.T) {
if !testAcc {
t.Skip()
}
assert := assert.New(t)
require := require.New(t)
client, _ := redash.NewClient(testRedashEndpoint, testRedashAPIKey)
status, err := client.GetStatus(context.Background())
require.NoError(err)
assert.NotEmpty(status.Version)
}