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 #194

Merged
merged 1 commit into from
Jun 14, 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
216 changes: 216 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,34 @@ func Test_GetQueryTags_OK(t *testing.T) {
}, res)
}

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

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

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

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

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

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

func Test_RefreshQuery_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -1879,6 +1907,34 @@ func Test_RefreshQuery_OK(t *testing.T) {
}, job)
}

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

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

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

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

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

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

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

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.SearchQueries(context.Background(), &redash.SearchQueriesInput{
Q: "my-query",
})
assert.ErrorContains(err, "GET api/queries/search failed: HTTP status code not OK: 503")
}

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

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

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

func Test_ListMyQueries_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -2091,6 +2179,42 @@ func Test_ListMyQueries_OK(t *testing.T) {
}, res)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListMyQueries(context.Background(), &redash.ListMyQueriesInput{
Page: 1,
PageSize: 25,
Q: "my-query",
})
assert.ErrorContains(err, "GET api/queries/my failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListMyQueries(context.Background(), &redash.ListMyQueriesInput{
Page: 1,
PageSize: 25,
Q: "my-query",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_ListFavoriteQueries_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -2198,6 +2322,42 @@ func Test_ListFavoriteQueries_OK(t *testing.T) {
}, res)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListFavoriteQueries(context.Background(), &redash.ListFavoriteQueriesInput{
Page: 1,
PageSize: 25,
Q: "my-query",
})
assert.ErrorContains(err, "GET api/queries/favorites failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListFavoriteQueries(context.Background(), &redash.ListFavoriteQueriesInput{
Page: 1,
PageSize: 25,
Q: "my-query",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_FormatQuery_OK(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
Expand Down Expand Up @@ -2233,6 +2393,34 @@ func Test_FormatQuery_OK(t *testing.T) {
}, res)
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.FormatQuery(context.Background(), "select 1 from dual")
assert.ErrorContains(err, "POST api/queries/format failed: HTTP status code not OK: 503\nerror")
}

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

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

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

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

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

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

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

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

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

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

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