Conditions | 9 |
Total Lines | 110 |
Code Lines | 81 |
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 |
||
134 | func TestChargebacksService_List(t *testing.T) { |
||
135 | setEnv() |
||
136 | defer unsetEnv() |
||
137 | |||
138 | type args struct { |
||
139 | ctx context.Context |
||
140 | options *ListChargebacksOptions |
||
141 | } |
||
142 | |||
143 | noPre := func() {} |
||
144 | |||
145 | cases := []struct { |
||
146 | name string |
||
147 | args args |
||
148 | wantErr bool |
||
149 | err error |
||
150 | handler http.HandlerFunc |
||
151 | pre func() |
||
152 | }{ |
||
153 | { |
||
154 | "list chargebacks", |
||
155 | args{ |
||
156 | context.Background(), |
||
157 | nil, |
||
158 | }, |
||
159 | false, |
||
160 | nil, |
||
161 | func(w http.ResponseWriter, r *http.Request) { |
||
162 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
163 | testMethod(t, r, "GET") |
||
164 | if _, ok := r.Header[AuthHeader]; !ok { |
||
165 | w.WriteHeader(http.StatusUnauthorized) |
||
166 | } |
||
167 | |||
168 | _, _ = w.Write([]byte(testdata.ListChargebacksResponse)) |
||
169 | }, |
||
170 | noPre, |
||
171 | }, |
||
172 | { |
||
173 | "list chargebacks with options", |
||
174 | args{ |
||
175 | context.Background(), |
||
176 | &ListChargebacksOptions{ |
||
177 | ProfileID: "pfl_QkEhN94Ba", |
||
178 | }, |
||
179 | }, |
||
180 | false, |
||
181 | nil, |
||
182 | func(w http.ResponseWriter, r *http.Request) { |
||
183 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
184 | testMethod(t, r, "GET") |
||
185 | if _, ok := r.Header[AuthHeader]; !ok { |
||
186 | w.WriteHeader(http.StatusUnauthorized) |
||
187 | } |
||
188 | |||
189 | _, _ = w.Write([]byte(testdata.ListChargebacksResponse)) |
||
190 | }, |
||
191 | noPre, |
||
192 | }, |
||
193 | { |
||
194 | "list chargebacks return an http error from the remote server", |
||
195 | args{ |
||
196 | context.Background(), |
||
197 | nil, |
||
198 | }, |
||
199 | true, |
||
200 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
201 | errorHandler, |
||
202 | noPre, |
||
203 | }, |
||
204 | { |
||
205 | "list chargebacks return an error when creating the requests", |
||
206 | args{ |
||
207 | context.Background(), |
||
208 | nil, |
||
209 | }, |
||
210 | true, |
||
211 | errBadBaseURL, |
||
212 | errorHandler, |
||
213 | crashSrv, |
||
214 | }, |
||
215 | { |
||
216 | "list chargebacks return an error when parsing the json response", |
||
217 | args{ |
||
218 | context.Background(), |
||
219 | nil, |
||
220 | }, |
||
221 | true, |
||
222 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
223 | encodingHandler, |
||
224 | noPre, |
||
225 | }, |
||
226 | } |
||
227 | |||
228 | for _, c := range cases { |
||
229 | setup() |
||
230 | defer teardown() |
||
231 | t.Run(c.name, func(t *testing.T) { |
||
232 | tMux.HandleFunc("/v2/chargebacks", c.handler) |
||
233 | |||
234 | c.pre() |
||
235 | res, cbl, err := tClient.Chargebacks.List(c.args.ctx, c.args.options) |
||
236 | if c.wantErr { |
||
237 | assert.Error(t, err) |
||
238 | assert.EqualError(t, err, c.err.Error()) |
||
239 | } else { |
||
240 | assert.Nil(t, err) |
||
241 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
242 | assert.IsType(t, &ChargebacksList{}, cbl) |
||
243 | assert.IsType(t, &http.Response{}, res.Response) |
||
244 | } |
||
371 |