From 6faacc8f994399afacc65f14bf15f84ea1c0145c Mon Sep 17 00:00:00 2001 From: winebarrel Date: Wed, 12 Jun 2024 11:29:48 +0900 Subject: [PATCH] Add error test --- alert_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/alert_test.go b/alert_test.go index 25ddff1..2d04d3c 100644 --- a/alert_test.go +++ b/alert_test.go @@ -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() @@ -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() @@ -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() @@ -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()