Conditions | 8 |
Total Lines | 119 |
Code Lines | 90 |
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 |
||
343 | func TestMandatesService_List(t *testing.T) { |
||
344 | setEnv() |
||
345 | defer unsetEnv() |
||
346 | |||
347 | type args struct { |
||
348 | ctx context.Context |
||
349 | options *ListMandatesOptions |
||
350 | customer string |
||
351 | } |
||
352 | |||
353 | cases := []struct { |
||
354 | name string |
||
355 | args args |
||
356 | wantErr bool |
||
357 | err error |
||
358 | pre func() |
||
359 | handler http.HandlerFunc |
||
360 | }{ |
||
361 | { |
||
362 | "list mandates works as expected.", |
||
363 | args{ |
||
364 | context.Background(), |
||
365 | nil, |
||
366 | "cst_4qqhO89gsT", |
||
367 | }, |
||
368 | false, |
||
369 | nil, |
||
370 | noPre, |
||
371 | func(w http.ResponseWriter, r *http.Request) { |
||
372 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
373 | testMethod(t, r, "GET") |
||
374 | if _, ok := r.Header[AuthHeader]; !ok { |
||
375 | w.WriteHeader(http.StatusUnauthorized) |
||
376 | } |
||
377 | _, _ = w.Write([]byte(testdata.GetMandateResponse)) |
||
378 | }, |
||
379 | }, |
||
380 | { |
||
381 | "list mandates with options works as expected.", |
||
382 | args{ |
||
383 | context.Background(), |
||
384 | &ListMandatesOptions{ |
||
385 | Limit: 10, |
||
386 | }, |
||
387 | "cst_4qqhO89gsT", |
||
388 | }, |
||
389 | false, |
||
390 | nil, |
||
391 | noPre, |
||
392 | func(w http.ResponseWriter, r *http.Request) { |
||
393 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
394 | testMethod(t, r, "GET") |
||
395 | if _, ok := r.Header[AuthHeader]; !ok { |
||
396 | w.WriteHeader(http.StatusUnauthorized) |
||
397 | } |
||
398 | _, _ = w.Write([]byte(testdata.GetMandateResponse)) |
||
399 | }, |
||
400 | }, |
||
401 | { |
||
402 | "list mandates, an error is returned from the server", |
||
403 | args{ |
||
404 | context.Background(), |
||
405 | nil, |
||
406 | "cst_4qqhO89gsT", |
||
407 | }, |
||
408 | true, |
||
409 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
410 | noPre, |
||
411 | errorHandler, |
||
412 | }, |
||
413 | { |
||
414 | "list mandates, an error occurs when parsing json", |
||
415 | args{ |
||
416 | context.Background(), |
||
417 | nil, |
||
418 | "cst_4qqhO89gsT", |
||
419 | }, |
||
420 | true, |
||
421 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
422 | noPre, |
||
423 | encodingHandler, |
||
424 | }, |
||
425 | { |
||
426 | "list mandates, invalid url when building request", |
||
427 | args{ |
||
428 | context.Background(), |
||
429 | nil, |
||
430 | "cst_4qqhO89gsT", |
||
431 | }, |
||
432 | true, |
||
433 | errBadBaseURL, |
||
434 | crashSrv, |
||
435 | errorHandler, |
||
436 | }, |
||
437 | } |
||
438 | |||
439 | for _, c := range cases { |
||
440 | setup() |
||
441 | defer teardown() |
||
442 | |||
443 | t.Run(c.name, func(t *testing.T) { |
||
444 | c.pre() |
||
445 | tMux.HandleFunc( |
||
446 | fmt.Sprintf( |
||
447 | "/v2/customers/%s/mandates", |
||
448 | c.args.customer, |
||
449 | ), |
||
450 | c.handler, |
||
451 | ) |
||
452 | |||
453 | res, m, err := tClient.Mandates.List(c.args.ctx, c.args.customer, c.args.options) |
||
454 | if c.wantErr { |
||
455 | assert.NotNil(t, err) |
||
456 | assert.EqualError(t, err, c.err.Error()) |
||
457 | } else { |
||
458 | assert.Nil(t, err) |
||
459 | assert.IsType(t, &MandatesList{}, m) |
||
460 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
461 | assert.IsType(t, &http.Response{}, res.Response) |
||
462 | } |
||
466 |