| Conditions | 6 |
| Total Lines | 160 |
| Code Lines | 119 |
| 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 |
||
| 13 | func TestDelayedRoutingService_Create(t *testing.T) { |
||
| 14 | setEnv() |
||
| 15 | defer unsetEnv() |
||
| 16 | |||
| 17 | type args struct { |
||
| 18 | ctx context.Context |
||
| 19 | payment string |
||
| 20 | dr CreateDelayedRouting |
||
| 21 | } |
||
| 22 | |||
| 23 | cases := []struct { |
||
| 24 | name string |
||
| 25 | args args |
||
| 26 | wantErr bool |
||
| 27 | err error |
||
| 28 | pre func() |
||
| 29 | handler http.HandlerFunc |
||
| 30 | }{ |
||
| 31 | { |
||
| 32 | "creates a delayed routing successfully", |
||
| 33 | args{ |
||
| 34 | ctx: context.Background(), |
||
| 35 | payment: "tr_123456789", |
||
| 36 | dr: CreateDelayedRouting{ |
||
| 37 | Description: "Delayed routing description", |
||
| 38 | Amount: Amount{ |
||
| 39 | Currency: "EUR", |
||
| 40 | Value: "10.00", |
||
| 41 | }, |
||
| 42 | Destination: DelayedRoutingDestination{ |
||
| 43 | Type: "organization", |
||
| 44 | OrganizationID: "org_123456789", |
||
| 45 | }, |
||
| 46 | }, |
||
| 47 | }, |
||
| 48 | false, |
||
| 49 | nil, |
||
| 50 | noPre, |
||
| 51 | func(w http.ResponseWriter, r *http.Request) { |
||
| 52 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 53 | testMethod(t, r, "POST") |
||
| 54 | |||
| 55 | w.Header().Set("Content-Type", "application/json") |
||
| 56 | w.WriteHeader(http.StatusCreated) |
||
| 57 | _, _ = w.Write([]byte(testdata.GetDelayedRoutingExample)) |
||
| 58 | }, |
||
| 59 | }, |
||
| 60 | { |
||
| 61 | "creates a delayed routing works as expected with access tokens", |
||
| 62 | args{ |
||
| 63 | ctx: context.Background(), |
||
| 64 | payment: "tr_123456789", |
||
| 65 | dr: CreateDelayedRouting{ |
||
| 66 | Description: "Delayed routing description", |
||
| 67 | Amount: Amount{ |
||
| 68 | Currency: "EUR", |
||
| 69 | Value: "10.00", |
||
| 70 | }, |
||
| 71 | Destination: DelayedRoutingDestination{ |
||
| 72 | Type: "organization", |
||
| 73 | OrganizationID: "org_123456789", |
||
| 74 | }, |
||
| 75 | }, |
||
| 76 | }, |
||
| 77 | false, |
||
| 78 | nil, |
||
| 79 | setAccessToken, |
||
| 80 | func(w http.ResponseWriter, r *http.Request) { |
||
| 81 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
| 82 | testMethod(t, r, "POST") |
||
| 83 | |||
| 84 | w.Header().Set("Content-Type", "application/json") |
||
| 85 | w.WriteHeader(http.StatusCreated) |
||
| 86 | _, _ = w.Write([]byte(testdata.GetDelayedRoutingExample)) |
||
| 87 | }, |
||
| 88 | }, |
||
| 89 | { |
||
| 90 | "create delayed routing fails with error in handler", |
||
| 91 | args{ |
||
| 92 | ctx: context.Background(), |
||
| 93 | payment: "tr_123456789", |
||
| 94 | dr: CreateDelayedRouting{ |
||
| 95 | Description: "Delayed routing description", |
||
| 96 | Amount: Amount{ |
||
| 97 | Currency: "EUR", |
||
| 98 | Value: "10.00", |
||
| 99 | }, |
||
| 100 | Destination: DelayedRoutingDestination{ |
||
| 101 | Type: "organization", |
||
| 102 | OrganizationID: "org_123456789", |
||
| 103 | }, |
||
| 104 | }, |
||
| 105 | }, |
||
| 106 | true, |
||
| 107 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
| 108 | noPre, |
||
| 109 | errorHandler, |
||
| 110 | }, |
||
| 111 | { |
||
| 112 | "create delayed routing, an error occurs when parsing json", |
||
| 113 | args{ |
||
| 114 | ctx: context.Background(), |
||
| 115 | payment: "tr_123456789", |
||
| 116 | dr: CreateDelayedRouting{ |
||
| 117 | Description: "Delayed routing description", |
||
| 118 | Amount: Amount{ |
||
| 119 | Currency: "EUR", |
||
| 120 | Value: "10.00", |
||
| 121 | }, |
||
| 122 | Destination: DelayedRoutingDestination{ |
||
| 123 | Type: "organization", |
||
| 124 | OrganizationID: "org_123456789", |
||
| 125 | }, |
||
| 126 | }, |
||
| 127 | }, |
||
| 128 | true, |
||
| 129 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 130 | noPre, |
||
| 131 | encodingHandler, |
||
| 132 | }, |
||
| 133 | { |
||
| 134 | "create delayed routing, invalid url when building request", |
||
| 135 | args{ |
||
| 136 | ctx: context.Background(), |
||
| 137 | payment: "tr_123456789", |
||
| 138 | dr: CreateDelayedRouting{ |
||
| 139 | Description: "Delayed routing description", |
||
| 140 | Amount: Amount{ |
||
| 141 | Currency: "EUR", |
||
| 142 | Value: "10.00", |
||
| 143 | }, |
||
| 144 | Destination: DelayedRoutingDestination{ |
||
| 145 | Type: "organization", |
||
| 146 | OrganizationID: "org_123456789", |
||
| 147 | }, |
||
| 148 | }, |
||
| 149 | }, |
||
| 150 | true, |
||
| 151 | errBadBaseURL, |
||
| 152 | crashSrv, |
||
| 153 | errorHandler, |
||
| 154 | }, |
||
| 155 | } |
||
| 156 | |||
| 157 | for _, c := range cases { |
||
| 158 | setup() |
||
| 159 | defer teardown() |
||
| 160 | |||
| 161 | t.Run(c.name, func(t *testing.T) { |
||
| 162 | c.pre() |
||
| 163 | tMux.HandleFunc(fmt.Sprintf("/v2/payments/%s/routes", c.args.payment), c.handler) |
||
| 164 | |||
| 165 | res, m, err := tClient.DelayedRouting.Create(c.args.ctx, c.args.payment, c.args.dr) |
||
| 166 | if c.wantErr { |
||
| 167 | assert.NotNil(t, err) |
||
| 168 | assert.EqualError(t, err, c.err.Error()) |
||
| 169 | } else { |
||
| 170 | assert.Nil(t, err) |
||
| 171 | assert.IsType(t, &Route{}, m) |
||
| 172 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 173 | } |
||
| 286 |