Skip to content

Commit

Permalink
Merge pull request #188 from winebarrel/add_error_tests
Browse files Browse the repository at this point in the history
Add error test
  • Loading branch information
winebarrel authored Jun 14, 2024
2 parents 205b624 + 0113cc8 commit fd26fda
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,20 @@ func Test_CreateFavoriteQuery_OK(t *testing.T) {
assert.NoError(err)
}

func Test_CreateFavoriteQuery_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/favorite", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusInternalServerError, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.CreateFavoriteQuery(context.Background(), 1)
assert.ErrorContains(err, "POST api/queries/1/favorite failed: HTTP status code not OK: 500\nerror")
}

func Test_ForkQuery_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -906,6 +920,34 @@ func Test_ForkQuery_OK(t *testing.T) {
}, res)
}

func Test_ForkQuery_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/fork", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ForkQuery(context.Background(), 1)
assert.ErrorContains(err, "POST api/queries/1/fork failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ForkQuery(context.Background(), 1)
assert.ErrorContains(err, "Read response body failed: IO error")
}

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

0 comments on commit fd26fda

Please sign in to comment.