Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error test #180

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,34 @@ func Test_AddAlertSubscription_OK(t *testing.T) {
}, res)
}

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

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

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

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

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

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

func Test_RemoveAlertSubscription_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -646,6 +674,20 @@ func Test_RemoveAlertSubscription_OK(t *testing.T) {
assert.NoError(err)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.RemoveAlertSubscription(context.Background(), 1, 2)
assert.ErrorContains(err, "DELETE api/alerts/1/subscriptions/2 failed: HTTP status code not OK: 503\nerror")
}

func Test_MuteAlert_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -670,6 +712,20 @@ func Test_MuteAlert_OK(t *testing.T) {
assert.NoError(err)
}

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

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

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

func Test_UnmuteAlert_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -694,6 +750,20 @@ func Test_UnmuteAlert_OK(t *testing.T) {
assert.NoError(err)
}

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

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

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

func Test_Alert_Acc(t *testing.T) {
if !testAcc {
t.Skip()
Expand Down