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 |
||
13 | func TestClientsService_Get(t *testing.T) { |
||
14 | setEnv() |
||
15 | defer unsetEnv() |
||
16 | |||
17 | type args struct { |
||
18 | ctx context.Context |
||
19 | client string |
||
20 | opts *GetLinkedClientOptions |
||
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 | "get partner client works as expected.", |
||
33 | args{ |
||
34 | context.Background(), |
||
35 | "org_1337", |
||
36 | nil, |
||
37 | }, |
||
38 | false, |
||
39 | nil, |
||
40 | noPre, |
||
41 | func(w http.ResponseWriter, r *http.Request) { |
||
42 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
43 | testMethod(t, r, "GET") |
||
44 | |||
45 | if _, ok := r.Header[AuthHeader]; !ok { |
||
46 | w.WriteHeader(http.StatusUnauthorized) |
||
47 | } |
||
48 | _, _ = w.Write([]byte(testdata.GetPartnerClientResponse)) |
||
49 | }, |
||
50 | }, |
||
51 | { |
||
52 | "get partner client with options works as expected.", |
||
53 | args{ |
||
54 | context.Background(), |
||
55 | "org_1337", |
||
56 | &GetLinkedClientOptions{ |
||
57 | Embed: []EmbedValue{EmbedOrganization}, |
||
58 | }, |
||
59 | }, |
||
60 | false, |
||
61 | nil, |
||
62 | noPre, |
||
63 | func(w http.ResponseWriter, r *http.Request) { |
||
64 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
65 | testMethod(t, r, "GET") |
||
66 | testQuery(t, r, "embed=organization") |
||
67 | |||
68 | if _, ok := r.Header[AuthHeader]; !ok { |
||
69 | w.WriteHeader(http.StatusUnauthorized) |
||
70 | } |
||
71 | _, _ = w.Write([]byte(testdata.GetPartnerClientResponse)) |
||
72 | }, |
||
73 | }, |
||
74 | { |
||
75 | "get partner client, an error is returned from the server", |
||
76 | args{ |
||
77 | context.Background(), |
||
78 | "org_1337", |
||
79 | nil, |
||
80 | }, |
||
81 | true, |
||
82 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
83 | noPre, |
||
84 | errorHandler, |
||
85 | }, |
||
86 | { |
||
87 | "get partner client, an error occurs when parsing json", |
||
88 | args{ |
||
89 | context.Background(), |
||
90 | "org_1337", |
||
91 | nil, |
||
92 | }, |
||
93 | true, |
||
94 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
95 | noPre, |
||
96 | encodingHandler, |
||
97 | }, |
||
98 | { |
||
99 | "get partner client, invalid url when building request", |
||
100 | args{ |
||
101 | context.Background(), |
||
102 | "org_1337", |
||
103 | nil, |
||
104 | }, |
||
105 | true, |
||
106 | errBadBaseURL, |
||
107 | crashSrv, |
||
108 | errorHandler, |
||
109 | }, |
||
110 | } |
||
111 | |||
112 | for _, c := range cases { |
||
113 | setup() |
||
114 | defer teardown() |
||
115 | |||
116 | t.Run(c.name, func(t *testing.T) { |
||
117 | c.pre() |
||
118 | tMux.HandleFunc(fmt.Sprintf("/v2/clients/%s", c.args.client), c.handler) |
||
119 | |||
120 | res, m, err := tClient.Clients.Get(c.args.ctx, c.args.client, c.args.opts) |
||
121 | if c.wantErr { |
||
122 | assert.NotNil(t, err) |
||
123 | assert.EqualError(t, err, c.err.Error()) |
||
124 | } else { |
||
125 | assert.Nil(t, err) |
||
126 | assert.IsType(t, &LinkedClient{}, m) |
||
127 | assert.IsType(t, &http.Response{}, res.Response) |
||
128 | } |
||
252 |