From f00bd88c90d5442e8a24639c7798032f832f4f0b Mon Sep 17 00:00:00 2001 From: winebarrel Date: Fri, 14 Jun 2024 15:00:52 +0900 Subject: [PATCH] Add error test --- query_test.go | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) diff --git a/query_test.go b/query_test.go index fbc6427..f789d92 100644 --- a/query_test.go +++ b/query_test.go @@ -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() @@ -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() @@ -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() @@ -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() @@ -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) @@ -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() @@ -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()