-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_test.go
82 lines (71 loc) · 2.54 KB
/
job_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
package redash_test
import (
"context"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/winebarrel/redash-go/v2"
)
func Test_GetJob_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51", 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, `
{
"job": {
"error": "",
"id": "623b290a-7fd9-4ea6-a2a6-96f9c9101f51",
"query_result_id": 1,
"status": 3,
"updated_at": 0
}
}
`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
res, err := client.GetJob(context.Background(), "623b290a-7fd9-4ea6-a2a6-96f9c9101f51")
assert.NoError(err)
assert.Equal(&redash.JobResponse{
Job: redash.Job{
Error: "",
ID: "623b290a-7fd9-4ea6-a2a6-96f9c9101f51",
QueryResultID: 1,
Status: redash.JobStatusSuccess,
UpdatedAt: float64(0),
},
}, res)
}
func Test_GetJob_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetJob(context.Background(), "623b290a-7fd9-4ea6-a2a6-96f9c9101f51")
assert.ErrorContains(err, "GET api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51 failed: HTTP status code not OK: 503\nerror")
}
func Test_GetJob_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetJob(context.Background(), "623b290a-7fd9-4ea6-a2a6-96f9c9101f51")
assert.ErrorContains(err, "Read response body failed: IO error")
}