| Conditions | 9 |
| Total Lines | 141 |
| Code Lines | 105 |
| 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 |
||
| 148 | func TestCapturesService_Create(t *testing.T) { |
||
| 149 | setEnv() |
||
| 150 | defer unsetEnv() |
||
| 151 | |||
| 152 | type args struct { |
||
| 153 | ctx context.Context |
||
| 154 | payment string |
||
| 155 | capture CreateCapture |
||
| 156 | options *CaptureOptions |
||
| 157 | } |
||
| 158 | |||
| 159 | cases := []struct { |
||
| 160 | name string |
||
| 161 | args args |
||
| 162 | wantErr bool |
||
| 163 | err error |
||
| 164 | handler http.HandlerFunc |
||
| 165 | pre func() |
||
| 166 | }{ |
||
| 167 | { |
||
| 168 | "create captures works as expected", |
||
| 169 | args{ |
||
| 170 | context.Background(), |
||
| 171 | "tr_WDqYK6vllg", |
||
| 172 | CreateCapture{ |
||
| 173 | Amount: &Amount{ |
||
| 174 | Value: "20.00", |
||
| 175 | Currency: "EUR", |
||
| 176 | }, |
||
| 177 | Description: "Order #12345", |
||
| 178 | }, |
||
| 179 | nil, |
||
| 180 | }, |
||
| 181 | false, |
||
| 182 | nil, |
||
| 183 | func(w http.ResponseWriter, r *http.Request) { |
||
| 184 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 185 | testMethod(t, r, "POST") |
||
| 186 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 187 | w.WriteHeader(http.StatusUnauthorized) |
||
| 188 | } |
||
| 189 | |||
| 190 | _, _ = w.Write([]byte(testdata.CreateCaptureResponse)) |
||
| 191 | }, |
||
| 192 | noPre, |
||
| 193 | }, |
||
| 194 | { |
||
| 195 | "create captures works as with access token expected", |
||
| 196 | args{ |
||
| 197 | context.Background(), |
||
| 198 | "tr_WDqYK6vllg", |
||
| 199 | CreateCapture{ |
||
| 200 | Amount: &Amount{ |
||
| 201 | Value: "20.00", |
||
| 202 | Currency: "EUR", |
||
| 203 | }, |
||
| 204 | Description: "Order #12345", |
||
| 205 | }, |
||
| 206 | nil, |
||
| 207 | }, |
||
| 208 | false, |
||
| 209 | nil, |
||
| 210 | func(w http.ResponseWriter, r *http.Request) { |
||
| 211 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
| 212 | testMethod(t, r, "POST") |
||
| 213 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 214 | w.WriteHeader(http.StatusUnauthorized) |
||
| 215 | } |
||
| 216 | |||
| 217 | _, _ = w.Write([]byte(testdata.CreateCaptureWithAccessTokenResponse)) |
||
| 218 | }, |
||
| 219 | setAccessToken, |
||
| 220 | }, |
||
| 221 | { |
||
| 222 | "create captures returns an http error from the server", |
||
| 223 | args{ |
||
| 224 | context.Background(), |
||
| 225 | "tr_WDqYK6vllg", |
||
| 226 | CreateCapture{}, |
||
| 227 | nil, |
||
| 228 | }, |
||
| 229 | true, |
||
| 230 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
| 231 | errorHandler, |
||
| 232 | noPre, |
||
| 233 | }, |
||
| 234 | { |
||
| 235 | "create captures returns an error when creating the request", |
||
| 236 | args{ |
||
| 237 | context.Background(), |
||
| 238 | "tr_WDqYK6vllg", |
||
| 239 | CreateCapture{}, |
||
| 240 | nil, |
||
| 241 | }, |
||
| 242 | true, |
||
| 243 | errBadBaseURL, |
||
| 244 | errorHandler, |
||
| 245 | crashSrv, |
||
| 246 | }, |
||
| 247 | { |
||
| 248 | "create captures returns an error when trying to parse the json response", |
||
| 249 | args{ |
||
| 250 | context.Background(), |
||
| 251 | "tr_WDqYK6vllg", |
||
| 252 | CreateCapture{}, |
||
| 253 | nil, |
||
| 254 | }, |
||
| 255 | true, |
||
| 256 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 257 | encodingHandler, |
||
| 258 | noPre, |
||
| 259 | }, |
||
| 260 | } |
||
| 261 | |||
| 262 | for _, c := range cases { |
||
| 263 | setup() |
||
| 264 | defer teardown() |
||
| 265 | |||
| 266 | t.Run(c.name, func(t *testing.T) { |
||
| 267 | c.pre() |
||
| 268 | |||
| 269 | tMux.HandleFunc( |
||
| 270 | fmt.Sprintf( |
||
| 271 | "/v2/payments/%s/captures", |
||
| 272 | c.args.payment, |
||
| 273 | ), |
||
| 274 | c.handler, |
||
| 275 | ) |
||
| 276 | |||
| 277 | res, capture, err := tClient.Captures.Create(c.args.ctx, c.args.payment, c.args.capture) |
||
| 278 | if c.wantErr { |
||
| 279 | assert.NotNil(t, err) |
||
| 280 | assert.EqualError(t, err, c.err.Error()) |
||
| 281 | if tClient.HasAccessToken() { |
||
| 282 | assert.True(t, capture.Testmode) |
||
| 283 | } |
||
| 284 | } else { |
||
| 285 | assert.Nil(t, err) |
||
| 286 | assert.IsType(t, &Capture{}, capture) |
||
| 287 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
| 288 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 289 | } |
||
| 409 |