Conditions | 7 |
Total Lines | 115 |
Code Lines | 87 |
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 |
||
14 | func TestChargebacksService_Get(t *testing.T) { |
||
15 | setEnv() |
||
16 | defer unsetEnv() |
||
17 | |||
18 | type args struct { |
||
19 | ctx context.Context |
||
20 | payment string |
||
21 | chargeback string |
||
22 | options *ChargebackOptions |
||
23 | } |
||
24 | |||
25 | cases := []struct { |
||
26 | name string |
||
27 | args args |
||
28 | wantErr bool |
||
29 | err error |
||
30 | handler http.HandlerFunc |
||
31 | pre func() |
||
32 | }{ |
||
33 | { |
||
34 | "get chargebacks", |
||
35 | args{ |
||
36 | context.Background(), |
||
37 | "tr_WDqYK6vllg", |
||
38 | "chb_n9z0tp", |
||
39 | &ChargebackOptions{ |
||
40 | Include: []IncludeValue{IncludeQrCode}, |
||
41 | }, |
||
42 | }, |
||
43 | false, |
||
44 | nil, |
||
45 | func(w http.ResponseWriter, r *http.Request) { |
||
46 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
47 | testMethod(t, r, "GET") |
||
48 | if _, ok := r.Header[AuthHeader]; !ok { |
||
49 | w.WriteHeader(http.StatusUnauthorized) |
||
50 | } |
||
51 | |||
52 | _, _ = w.Write([]byte(testdata.GetChargebackResponse)) |
||
53 | }, |
||
54 | noPre, |
||
55 | }, |
||
56 | { |
||
57 | "get chargebacks returns an http error from the server", |
||
58 | args{ |
||
59 | context.Background(), |
||
60 | "tr_WDqYK6vllg", |
||
61 | "chb_n9z0tp", |
||
62 | &ChargebackOptions{ |
||
63 | Include: []IncludeValue{IncludeQrCode}, |
||
64 | }, |
||
65 | }, |
||
66 | true, |
||
67 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
68 | errorHandler, |
||
69 | noPre, |
||
70 | }, |
||
71 | { |
||
72 | "get chargebacks returns an error when creating the request", |
||
73 | args{ |
||
74 | context.Background(), |
||
75 | "tr_WDqYK6vllg", |
||
76 | "chb_n9z0tp", |
||
77 | &ChargebackOptions{ |
||
78 | Include: []IncludeValue{IncludeQrCode}, |
||
79 | }, |
||
80 | }, |
||
81 | true, |
||
82 | errBadBaseURL, |
||
83 | errorHandler, |
||
84 | func() { |
||
85 | u, _ := url.Parse(tServer.URL) |
||
86 | tClient.BaseURL = u |
||
87 | }, |
||
88 | }, |
||
89 | { |
||
90 | "get chargebacks returns an error when trying to parse the json response", |
||
91 | args{ |
||
92 | context.Background(), |
||
93 | "tr_WDqYK6vllg", |
||
94 | "chb_n9z0tp", |
||
95 | &ChargebackOptions{ |
||
96 | Include: []IncludeValue{IncludeQrCode}, |
||
97 | }, |
||
98 | }, |
||
99 | true, |
||
100 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
101 | encodingHandler, |
||
102 | noPre, |
||
103 | }, |
||
104 | } |
||
105 | |||
106 | for _, c := range cases { |
||
107 | setup() |
||
108 | defer teardown() |
||
109 | t.Run(c.name, func(t *testing.T) { |
||
110 | tMux.HandleFunc( |
||
111 | fmt.Sprintf( |
||
112 | "/v2/payments/%s/chargebacks/%s", |
||
113 | c.args.payment, |
||
114 | c.args.chargeback, |
||
115 | ), |
||
116 | c.handler, |
||
117 | ) |
||
118 | c.pre() |
||
119 | |||
120 | res, cb, err := tClient.Chargebacks.Get(c.args.ctx, c.args.payment, c.args.chargeback, c.args.options) |
||
121 | if c.wantErr { |
||
122 | assert.Error(t, err) |
||
123 | assert.EqualError(t, err, c.err.Error()) |
||
124 | } else { |
||
125 | assert.Nil(t, err) |
||
126 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
127 | assert.IsType(t, &Chargeback{}, cb) |
||
128 | assert.IsType(t, &http.Response{}, res.Response) |
||
129 | } |
||
371 |