Skip to content

Commit

Permalink
Merge pull request #184 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 13, 2024
2 parents 28d863f + f0b748e commit c0ac215
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ func Test_ListGroups_OK(t *testing.T) {
}, res)
}

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

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

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

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

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

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

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

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

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/groups/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.GetGroup(context.Background(), 1)
assert.ErrorContains(err, "GET api/groups/1 failed: HTTP status code not OK: 503\nerror")
}

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

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

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

func Test_CreateGroup_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -180,6 +236,38 @@ func Test_CreateGroup_OK(t *testing.T) {
}, res)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateGroup(context.Background(), &redash.CreateGroupInput{
Name: "my-group",
})
assert.ErrorContains(err, "POST api/groups failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateGroup(context.Background(), &redash.CreateGroupInput{
Name: "my-group",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

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

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

httpmock.RegisterResponder(http.MethodDelete, "https://redash.example.com/api/groups/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.DeleteGroup(context.Background(), 2)
assert.ErrorContains(err, "DELETE api/groups/2 failed: HTTP status code not OK: 503\nerror")
}

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

0 comments on commit c0ac215

Please sign in to comment.