Skip to content

Commit

Permalink
Add error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Jun 10, 2024
1 parent cd004b2 commit 88b6a2d
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 88b6a2d

Please sign in to comment.