Skip to content

Commit

Permalink
Merge pull request #189 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 14, 2024
2 parents fd26fda + bd1ad57 commit fa76e2d
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,36 @@ func Test_GetQueryResultsJSON_OK(t *testing.T) {
assert.Equal(`{"foo":"bar"}`, buf.String())
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResultsJSON(context.Background(), 1, &buf)
assert.ErrorContains(err, "GET api/queries/1/results.json failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResultsJSON(context.Background(), 1, &buf)
assert.ErrorContains(err, "IO error")
}

func Test_GetQueryResultsStruct_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -1019,6 +1049,34 @@ func Test_GetQueryResultsStruct_OK(t *testing.T) {
)
}

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

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

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

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

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

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

func Test_GetQueryResultsCSV_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -1045,6 +1103,36 @@ func Test_GetQueryResultsCSV_OK(t *testing.T) {
assert.Equal(`foo,bar`, buf.String())
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResultsCSV(context.Background(), 1, &buf)
assert.ErrorContains(err, "GET api/queries/1/results.csv failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResultsCSV(context.Background(), 1, &buf)
assert.ErrorContains(err, "IO error")
}

func Test_GetQueryResults_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -1071,6 +1159,36 @@ func Test_GetQueryResults_OK(t *testing.T) {
assert.Equal(`{"foo":"bar"}`, buf.String())
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResults(context.Background(), 1, "json", &buf)
assert.ErrorContains(err, "GET api/queries/1/results.json failed: HTTP status code not OK: 503\nerror")
}

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

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

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
var buf bytes.Buffer
err := client.GetQueryResults(context.Background(), 1, "json", &buf)
assert.ErrorContains(err, "IO error")
}

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

0 comments on commit fa76e2d

Please sign in to comment.