Conditions | 12 |
Total Lines | 126 |
Code Lines | 79 |
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:
Complex classes like mobilenig.TestBillsService_PayDStv often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package mobilenig |
||
140 | func TestBillsService_PayDStv(t *testing.T) { |
||
141 | t.Parallel() |
||
142 | t.Run("it returns the dstv transaction response with a valid payment", func(t *testing.T) { |
||
143 | t.Parallel() |
||
144 | |||
145 | // Arrange |
||
146 | server := helpers.MakeTestServer(http.StatusOK, stubs.PayDstvBillResponse()) |
||
147 | baseURL, _ := url.Parse(server.URL) |
||
148 | client := New(WithBaseURL(baseURL)) |
||
149 | |||
150 | // Act |
||
151 | transaction, _, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
152 | |||
153 | // Assert |
||
154 | assert.NoError(t, err) |
||
155 | |||
156 | assert.Equal(t, "122790223", transaction.TransactionID) |
||
157 | assert.Equal(t, "DSTV", transaction.Details.Service) |
||
158 | assert.Equal(t, "DStv Mobile MAXI", transaction.Details.Package) |
||
159 | assert.Equal(t, "4131953321", transaction.Details.SmartcardNumber) |
||
160 | assert.Equal(t, "790", transaction.Details.Price) |
||
161 | assert.Equal(t, "SUCCESSFUL", transaction.Details.Status) |
||
162 | assert.Equal(t, "7931", transaction.Details.Balance) |
||
163 | |||
164 | // Teardown |
||
165 | server.Close() |
||
166 | }) |
||
167 | |||
168 | t.Run("it constructs the request correctly", func(t *testing.T) { |
||
169 | t.Parallel() |
||
170 | |||
171 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
172 | for _, environment := range environments { |
||
173 | t.Run(environment.String(), func(t *testing.T) { |
||
174 | // Arrange |
||
175 | request := new(http.Request) |
||
176 | server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request) |
||
177 | |||
178 | baseURL, _ := url.Parse(server.URL) |
||
179 | username := "test_username" |
||
180 | apiKey := "test_api_key" |
||
181 | smartcardNumber := "4131953321" |
||
182 | customerNumber := "275953782" |
||
183 | customerName := "ESU INI OBONG BASSEY" |
||
184 | price := "790" |
||
185 | transactionID := "122790223" |
||
186 | |||
187 | client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment)) |
||
188 | |||
189 | // Act |
||
190 | _, _, _ = client.Bills.PayDStv(context.Background(), &PayDstvOptions{ |
||
191 | TransactionID: transactionID, |
||
192 | Price: price, |
||
193 | ProductCode: DstvProductCodePremium, |
||
194 | CustomerName: customerName, |
||
195 | CustomerNumber: customerNumber, |
||
196 | SmartcardNumber: smartcardNumber, |
||
197 | }) |
||
198 | |||
199 | // Assert |
||
200 | uri := "/bills/dstv" |
||
201 | if environment == TestEnvironment { |
||
202 | uri += "_test" |
||
203 | } |
||
204 | |||
205 | assert.Equal(t, uri, request.URL.Path) |
||
206 | assert.Equal(t, username, request.URL.Query().Get("username")) |
||
207 | assert.Equal(t, apiKey, request.URL.Query().Get("api_key")) |
||
208 | assert.Equal(t, smartcardNumber, request.URL.Query().Get("smartno")) |
||
209 | assert.Equal(t, string(DstvProductCodePremium), request.URL.Query().Get("product_code")) |
||
210 | assert.Equal(t, customerName, request.URL.Query().Get("customer_name")) |
||
211 | assert.Equal(t, customerNumber, request.URL.Query().Get("customer_number")) |
||
212 | assert.Equal(t, price, request.URL.Query().Get("price")) |
||
213 | assert.Equal(t, transactionID, request.URL.Query().Get("trans_id")) |
||
214 | |||
215 | // Teardown |
||
216 | server.Close() |
||
217 | }) |
||
218 | } |
||
219 | }) |
||
220 | |||
221 | t.Run("it returns an error when the api responds with an error", func(t *testing.T) { |
||
222 | t.Parallel() |
||
223 | |||
224 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
225 | for _, environment := range environments { |
||
226 | t.Run(environment.String(), func(t *testing.T) { |
||
227 | // Arrange |
||
228 | server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse()) |
||
229 | baseURL, _ := url.Parse(server.URL) |
||
230 | |||
231 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
232 | |||
233 | // Act |
||
234 | _, resp, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
235 | |||
236 | // Assert |
||
237 | assert.Error(t, err) |
||
238 | |||
239 | assert.Equal(t, "ERR101", resp.Error.Code) |
||
240 | assert.Equal(t, "Invalid username or api_key", resp.Error.Description) |
||
241 | |||
242 | server.Close() |
||
243 | }) |
||
244 | } |
||
245 | }) |
||
246 | |||
247 | t.Run("it returns an error when the context is cancelled", func(t *testing.T) { |
||
248 | t.Parallel() |
||
249 | |||
250 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
251 | for _, environment := range environments { |
||
252 | t.Run(environment.String(), func(t *testing.T) { |
||
253 | // Arrange |
||
254 | server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse()) |
||
255 | baseURL, _ := url.Parse(server.URL) |
||
256 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
257 | ctx, cancel := context.WithCancel(context.Background()) |
||
258 | cancel() |
||
259 | |||
260 | // Act |
||
261 | _, _, err := client.Bills.PayDStv(ctx, &PayDstvOptions{}) |
||
262 | |||
263 | // Assert |
||
264 | assert.True(t, errors.Is(err, context.Canceled)) |
||
265 | server.Close() |
||
266 | }) |
||
381 |