From c238873ed8b88e748171575607648df23d68e8f2 Mon Sep 17 00:00:00 2001 From: winebarrel Date: Mon, 10 Jun 2024 22:30:48 +0900 Subject: [PATCH] Add error tests --- job_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/job_test.go b/job_test.go index 6993525..3578c15 100644 --- a/job_test.go +++ b/job_test.go @@ -52,3 +52,31 @@ func Test_GetJob_OK(t *testing.T) { }, }, res) } + +func Test_GetJob_Err_5xx(t *testing.T) { + assert := assert.New(t) + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51", func(req *http.Request) (*http.Response, error) { + return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil + }) + + client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey) + _, err := client.GetJob(context.Background(), "623b290a-7fd9-4ea6-a2a6-96f9c9101f51") + assert.ErrorContains(err, "GET api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51 failed: HTTP status code not OK: 503\nerror") +} + +func Test_GetJob_IOErr(t *testing.T) { + assert := assert.New(t) + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/jobs/623b290a-7fd9-4ea6-a2a6-96f9c9101f51", func(req *http.Request) (*http.Response, error) { + return testIOErrResp, nil + }) + + client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey) + _, err := client.GetJob(context.Background(), "623b290a-7fd9-4ea6-a2a6-96f9c9101f51") + assert.ErrorContains(err, "Read response body failed: IO error") +}