| Conditions | 9 |
| Total Lines | 118 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package mollie |
||
| 249 | func TestChargebacksService_ListForPayment(t *testing.T) { |
||
| 250 | setEnv() |
||
| 251 | defer unsetEnv() |
||
| 252 | |||
| 253 | type args struct { |
||
| 254 | ctx context.Context |
||
| 255 | payment string |
||
| 256 | options *ListChargebacksOptions |
||
| 257 | } |
||
| 258 | |||
| 259 | cases := []struct { |
||
| 260 | name string |
||
| 261 | args args |
||
| 262 | wantErr bool |
||
| 263 | err error |
||
| 264 | handler http.HandlerFunc |
||
| 265 | pre func() |
||
| 266 | }{ |
||
| 267 | { |
||
| 268 | "list chargebacks attached to a payment", |
||
| 269 | args{ |
||
| 270 | context.Background(), |
||
| 271 | "tr_WDqYK6vllg", |
||
| 272 | nil, |
||
| 273 | }, |
||
| 274 | false, |
||
| 275 | nil, |
||
| 276 | func(w http.ResponseWriter, r *http.Request) { |
||
| 277 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 278 | testMethod(t, r, "GET") |
||
| 279 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 280 | w.WriteHeader(http.StatusUnauthorized) |
||
| 281 | } |
||
| 282 | |||
| 283 | _, _ = w.Write([]byte(testdata.ListChargebacksResponse)) |
||
| 284 | }, |
||
| 285 | noPre, |
||
| 286 | }, |
||
| 287 | { |
||
| 288 | "list chargebacks attached to a payment, with options", |
||
| 289 | args{ |
||
| 290 | context.Background(), |
||
| 291 | "tr_WDqYK6vllg", |
||
| 292 | &ListChargebacksOptions{ |
||
| 293 | ProfileID: "pfl_QkEhN94Ba", |
||
| 294 | }, |
||
| 295 | }, |
||
| 296 | false, |
||
| 297 | nil, |
||
| 298 | func(w http.ResponseWriter, r *http.Request) { |
||
| 299 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 300 | testMethod(t, r, "GET") |
||
| 301 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 302 | w.WriteHeader(http.StatusUnauthorized) |
||
| 303 | } |
||
| 304 | |||
| 305 | _, _ = w.Write([]byte(testdata.ListChargebacksResponse)) |
||
| 306 | }, |
||
| 307 | noPre, |
||
| 308 | }, |
||
| 309 | { |
||
| 310 | "list chargebacks returns an http error from the remote server", |
||
| 311 | args{ |
||
| 312 | context.Background(), |
||
| 313 | "tr_WDqYK6vllg", |
||
| 314 | nil, |
||
| 315 | }, |
||
| 316 | true, |
||
| 317 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
| 318 | errorHandler, |
||
| 319 | noPre, |
||
| 320 | }, |
||
| 321 | { |
||
| 322 | "list chargebacks returns an error when building the request", |
||
| 323 | args{ |
||
| 324 | context.Background(), |
||
| 325 | "tr_WDqYK6vllg", |
||
| 326 | nil, |
||
| 327 | }, |
||
| 328 | true, |
||
| 329 | errBadBaseURL, |
||
| 330 | func(rw http.ResponseWriter, r *http.Request) {}, |
||
| 331 | crashSrv, |
||
| 332 | }, |
||
| 333 | { |
||
| 334 | "list chargebacks returns an error when parsing the json response", |
||
| 335 | args{ |
||
| 336 | context.Background(), |
||
| 337 | "tr_WDqYK6vllg", |
||
| 338 | nil, |
||
| 339 | }, |
||
| 340 | true, |
||
| 341 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 342 | encodingHandler, |
||
| 343 | noPre, |
||
| 344 | }, |
||
| 345 | } |
||
| 346 | |||
| 347 | for _, c := range cases { |
||
| 348 | setup() |
||
| 349 | defer teardown() |
||
| 350 | t.Run(c.name, func(t *testing.T) { |
||
| 351 | tMux.HandleFunc( |
||
| 352 | fmt.Sprintf("/v2/payments/%s/chargebacks", c.args.payment), |
||
| 353 | c.handler, |
||
| 354 | ) |
||
| 355 | |||
| 356 | c.pre() |
||
| 357 | |||
| 358 | res, cbl, err := tClient.Chargebacks.ListForPayment(c.args.ctx, c.args.payment, c.args.options) |
||
| 359 | if c.wantErr { |
||
| 360 | assert.Error(t, err) |
||
| 361 | assert.EqualError(t, err, c.err.Error()) |
||
| 362 | } else { |
||
| 363 | assert.Nil(t, err) |
||
| 364 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
| 365 | assert.IsType(t, &ChargebacksList{}, cbl) |
||
| 366 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 367 | } |
||
| 371 |