Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed Feb 16, 2023
1 parent ef180e1 commit b20f66e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func (cli *Client) GetBusinessDays(ctx context.Context, date time.Time) (*GetBus
return nil, ErrInvalidArgument
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.Endpoint+"/businessdays/check?date="+date.Format(RFC3339DateFormat), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
cli.Endpoint+"/businessdays/check?date="+date.Format(RFC3339DateFormat), nil)
if err != nil {
return nil, fmt.Errorf(errFailedGenerateRequestFormat, err)
}
Expand Down
11 changes: 8 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ func TestClient_GetBusinessDays(t *testing.T) {
srv.Close()
})

//nolint: maligned
cases := map[string]struct {
endpoint string
token string
Expand All @@ -527,9 +528,9 @@ func TestClient_GetBusinessDays(t *testing.T) {
wantError any
wantResult bool
}{
"Normal case": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveTime: time.UnixMilli(1672498800000), checkAsError: false, wantError: nil, wantResult: true},
"Normal case": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveTime: time.UnixMilli(1672531200000), checkAsError: false, wantError: nil, wantResult: true},
"Empty case": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveTime: time.Time{}, checkAsError: true, wantError: kenall.ErrInvalidArgument, wantResult: false},
"Wrong response": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveTime: time.Time{}.Add(time.Hour), checkAsError: true, wantError: &json.MarshalerError{}, wantResult: false},
"Wrong response": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveTime: time.Time{}.Add(24 * time.Hour), checkAsError: true, wantError: &json.MarshalerError{}, wantResult: false},
"nil context": {endpoint: srv.URL, token: "opencollector", ctx: nil, giveTime: time.Now(), checkAsError: true, wantError: &url.Error{}, wantResult: false},
}

Expand Down Expand Up @@ -729,7 +730,7 @@ func ExampleClient_GetBusinessDays() {
log.Fatal(err)
}

res, err := cli.GetBusinessDays(context.Background(), time.UnixMilli(946652400000)) // 2000-01-01
res, err := cli.GetBusinessDays(context.Background(), time.UnixMilli(946684800000)) // 2000-01-01
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -925,6 +926,10 @@ func handleBusinessDaysAPI(t *testing.T, w http.ResponseWriter, uri string) {
if _, err := w.Write(businessDaysResponse); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
case "/businessdays/check?date=0001-01-02":
if _, err := w.Write([]byte(`{"result": "worng"}`)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
default:
w.WriteHeader(http.StatusNotFound)
}
Expand Down

0 comments on commit b20f66e

Please sign in to comment.