Skip to content

Commit

Permalink
Merge pull request #174 from winebarrel/add_error_tests
Browse files Browse the repository at this point in the history
Add error test
  • Loading branch information
winebarrel authored Jun 11, 2024
2 parents 8327076 + 1a45a82 commit c8bbd50
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ func Test_ListDestinations_OK(t *testing.T) {
}, res)
}

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

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

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

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

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

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

func Test_GetDestination_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -97,6 +125,34 @@ func Test_GetDestination_OK(t *testing.T) {
}, res)
}

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

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

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

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

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

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

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

0 comments on commit c8bbd50

Please sign in to comment.