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