Conditions | 8 |
Total Lines | 130 |
Code Lines | 98 |
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 TestCapturesService_Get(t *testing.T) { |
||
14 | setEnv() |
||
15 | defer unsetEnv() |
||
16 | |||
17 | type args struct { |
||
18 | ctx context.Context |
||
19 | payment string |
||
20 | capture string |
||
21 | options *CaptureOptions |
||
22 | } |
||
23 | |||
24 | cases := []struct { |
||
25 | name string |
||
26 | args args |
||
27 | wantErr bool |
||
28 | err error |
||
29 | handler http.HandlerFunc |
||
30 | pre func() |
||
31 | }{ |
||
32 | { |
||
33 | "get captures works as expected", |
||
34 | args{ |
||
35 | context.Background(), |
||
36 | "tr_WDqYK6vllg", |
||
37 | "cpt_4qqhO89gsT", |
||
38 | nil, |
||
39 | }, |
||
40 | false, |
||
41 | nil, |
||
42 | func(w http.ResponseWriter, r *http.Request) { |
||
43 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
44 | testMethod(t, r, "GET") |
||
45 | if _, ok := r.Header[AuthHeader]; !ok { |
||
46 | w.WriteHeader(http.StatusUnauthorized) |
||
47 | } |
||
48 | |||
49 | _, _ = w.Write([]byte(testdata.GetCaptureResponse)) |
||
50 | }, |
||
51 | noPre, |
||
52 | }, |
||
53 | { |
||
54 | "get captures works expands query params correctly", |
||
55 | args{ |
||
56 | context.Background(), |
||
57 | "tr_WDqYK6vllg", |
||
58 | "cpt_4qqhO89gsT", |
||
59 | &CaptureOptions{ |
||
60 | Embed: []EmbedValue{EmbedPayments}, |
||
61 | }, |
||
62 | }, |
||
63 | false, |
||
64 | nil, |
||
65 | func(w http.ResponseWriter, r *http.Request) { |
||
66 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
67 | testMethod(t, r, "GET") |
||
68 | testQuery(t, r, "embed=payments") |
||
69 | if _, ok := r.Header[AuthHeader]; !ok { |
||
70 | w.WriteHeader(http.StatusUnauthorized) |
||
71 | } |
||
72 | |||
73 | _, _ = w.Write([]byte(testdata.GetCaptureResponse)) |
||
74 | }, |
||
75 | noPre, |
||
76 | }, |
||
77 | { |
||
78 | "get captures returns an http error from the server", |
||
79 | args{ |
||
80 | context.Background(), |
||
81 | "tr_WDqYK6vllg", |
||
82 | "cpt_4qqhO89gsT", |
||
83 | nil, |
||
84 | }, |
||
85 | true, |
||
86 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
87 | errorHandler, |
||
88 | noPre, |
||
89 | }, |
||
90 | { |
||
91 | "get captures returns an error when creating the request", |
||
92 | args{ |
||
93 | context.Background(), |
||
94 | "tr_WDqYK6vllg", |
||
95 | "cpt_4qqhO89gsT", |
||
96 | nil, |
||
97 | }, |
||
98 | true, |
||
99 | errBadBaseURL, |
||
100 | errorHandler, |
||
101 | crashSrv, |
||
102 | }, |
||
103 | { |
||
104 | "get captures returns an error when trying to parse the json response", |
||
105 | args{ |
||
106 | context.Background(), |
||
107 | "tr_WDqYK6vllg", |
||
108 | "cpt_4qqhO89gsT", |
||
109 | nil, |
||
110 | }, |
||
111 | true, |
||
112 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
113 | encodingHandler, |
||
114 | noPre, |
||
115 | }, |
||
116 | } |
||
117 | |||
118 | for _, c := range cases { |
||
119 | setup() |
||
120 | defer teardown() |
||
121 | |||
122 | t.Run(c.name, func(t *testing.T) { |
||
123 | c.pre() |
||
124 | |||
125 | tMux.HandleFunc( |
||
126 | fmt.Sprintf( |
||
127 | "/v2/payments/%s/captures/%s", |
||
128 | c.args.payment, |
||
129 | c.args.capture, |
||
130 | ), |
||
131 | c.handler, |
||
132 | ) |
||
133 | |||
134 | res, capture, err := tClient.Captures.Get(c.args.ctx, c.args.payment, c.args.capture, c.args.options) |
||
135 | if c.wantErr { |
||
136 | assert.NotNil(t, err) |
||
137 | assert.EqualError(t, err, c.err.Error()) |
||
138 | } else { |
||
139 | assert.Nil(t, err) |
||
140 | assert.IsType(t, &Capture{}, capture) |
||
141 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
142 | assert.IsType(t, &http.Response{}, res.Response) |
||
143 | } |
||
409 |