Skip to content

Commit

Permalink
Merge pull request #168 from winebarrel/add_error_tests
Browse files Browse the repository at this point in the history
Add error tests
  • Loading branch information
winebarrel authored Jun 10, 2024
2 parents ceee97f + 88b6a2d commit d943759
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,50 @@ func Test_CreateWidget_OK(t *testing.T) {
}, res)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateWidget(context.Background(), &redash.CreateWidgetInput{
DashboardID: 1,
Options: map[string]any{
"isHidden": false,
},
Text: "text",
VisualizationID: 1,
Width: 1,
})
assert.ErrorContains(err, "POST api/widgets failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateWidget(context.Background(), &redash.CreateWidgetInput{
DashboardID: 1,
Options: map[string]any{
"isHidden": false,
},
Text: "text",
VisualizationID: 1,
Width: 1,
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

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

0 comments on commit d943759

Please sign in to comment.