Skip to content

Commit

Permalink
Add error test
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Jun 14, 2024
1 parent bd1ad57 commit face54a
Showing 1 changed file with 126 additions and 2 deletions.
128 changes: 126 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,36 @@ func Test_ExecQueryJSON_OK(t *testing.T) {
assert.Empty(jobId)
}

func Test_ExecQueryJSON_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/queries/1/results", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
_, err := client.ExecQueryJSON(context.Background(), 1, &redash.ExecQueryJSONInput{}, &buf)
assert.ErrorContains(err, "POST api/queries/1/results failed: HTTP status code not OK: 503\nerror")
}

func Test_ExecQueryJSON_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/queries/1/results", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
_, err := client.ExecQueryJSON(context.Background(), 1, &redash.ExecQueryJSONInput{}, &buf)
assert.ErrorContains(err, "IO error")
}

func Test_ExecQueryJSON_OK_WithNil(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
Expand Down Expand Up @@ -1351,7 +1381,7 @@ func Test_ExecQueryJSON_ReturnJob(t *testing.T) {
}, job)
}

func Test_WaitQueryJSON(t *testing.T) {
func Test_WaitQueryJSON_Ok(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -1421,7 +1451,101 @@ func Test_WaitQueryJSON(t *testing.T) {
assert.Equal(`{"foo":"bar"}`, buf.String())
}

func Test_WaitQueryStruct(t *testing.T) {
func Test_WaitQueryJSON_Err_GetJob(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/queries/1/results", 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,
)
require.NotNil(req.Body)
body, _ := io.ReadAll(req.Body)
assert.Equal(`{}`, string(body))
return httpmock.NewStringResponse(http.StatusOK, `{"job": {"status": 1, "error": "", "id": "623b290a-7fd9-4ea6-a2a6-96f9c9101f51", "query_result_id": null, "status": 1, "updated_at": 0}}`), nil
})

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)
var buf bytes.Buffer
job, err := client.ExecQueryJSON(context.Background(), 1, &redash.ExecQueryJSONInput{}, &buf)
assert.NoError(err)
err = client.WaitQueryJSON(context.Background(), 1, job, nil, &buf)
assert.ErrorContains(err, "GET api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51 failed: HTTP status code not OK: 503\nerror")
}

func Test_WaitQueryJSON_Err_GetQueryResultsJSON(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/queries/1/results", 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,
)
require.NotNil(req.Body)
body, _ := io.ReadAll(req.Body)
assert.Equal(`{}`, string(body))
return httpmock.NewStringResponse(http.StatusOK, `{"job": {"status": 1, "error": "", "id": "623b290a-7fd9-4ea6-a2a6-96f9c9101f51", "query_result_id": null, "status": 1, "updated_at": 0}}`), nil
})

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
})

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/queries/1/results.json", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
job, err := client.ExecQueryJSON(context.Background(), 1, &redash.ExecQueryJSONInput{}, &buf)
assert.NoError(err)
err = client.WaitQueryJSON(context.Background(), 1, job, nil, &buf)
assert.ErrorContains(err, "GET api/queries/1/results.json failed: HTTP status code not OK: 503\nerror")
}

func Test_WaitQueryStruct_Ok(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
httpmock.Activate()
Expand Down

0 comments on commit face54a

Please sign in to comment.