Conditions | 18 |
Total Lines | 178 |
Code Lines | 104 |
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 |
||
172 | func TestBillsService_PayDStv(t *testing.T) { |
||
173 | t.Parallel() |
||
174 | t.Run("it returns the dstv transaction response with a valid payment", func(t *testing.T) { |
||
175 | t.Parallel() |
||
176 | |||
177 | // Arrange |
||
178 | server := helpers.MakeTestServer(http.StatusOK, stubs.PayDstvBillResponse()) |
||
179 | baseURL, _ := url.Parse(server.URL) |
||
180 | client := New(WithBaseURL(baseURL)) |
||
181 | |||
182 | // Act |
||
183 | transaction, _, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
184 | |||
185 | // Assert |
||
186 | assert.NoError(t, err) |
||
187 | |||
188 | assert.Equal(t, "122790223", transaction.TransactionID) |
||
189 | assert.Equal(t, "DSTV", transaction.Details.Service) |
||
190 | assert.Equal(t, "DStv Mobile MAXI", transaction.Details.Package) |
||
191 | assert.Equal(t, "4131953321", transaction.Details.SmartcardNumber) |
||
192 | assert.Equal(t, "790", transaction.Details.Price) |
||
193 | assert.Equal(t, "SUCCESSFUL", transaction.Details.Status) |
||
194 | assert.Equal(t, "7931", transaction.Details.Balance) |
||
195 | |||
196 | // Teardown |
||
197 | server.Close() |
||
198 | }) |
||
199 | |||
200 | t.Run("it constructs the request correctly", func(t *testing.T) { |
||
201 | t.Parallel() |
||
202 | |||
203 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
204 | for _, environment := range environments { |
||
205 | t.Run(environment.String(), func(t *testing.T) { |
||
206 | // Arrange |
||
207 | request := new(http.Request) |
||
208 | server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request) |
||
209 | |||
210 | baseURL, _ := url.Parse(server.URL) |
||
211 | username := testUsername |
||
212 | apiKey := testAPIKey |
||
213 | smartcardNumber := "4131953321" |
||
214 | customerNumber := "275953782" |
||
215 | customerName := "ESU INI OBONG BASSEY" |
||
216 | price := "790" |
||
217 | transactionID := "122790223" |
||
218 | |||
219 | client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment)) |
||
220 | |||
221 | // Act |
||
222 | _, _, _ = client.Bills.PayDStv(context.Background(), &PayDstvOptions{ |
||
223 | TransactionID: transactionID, |
||
224 | Price: price, |
||
225 | ProductCode: DstvProductCodePremium, |
||
226 | CustomerName: customerName, |
||
227 | CustomerNumber: customerNumber, |
||
228 | SmartcardNumber: smartcardNumber, |
||
229 | }) |
||
230 | |||
231 | // Assert |
||
232 | uri := "/bills/dstv" |
||
233 | if environment == TestEnvironment { |
||
234 | uri += "_test" |
||
235 | } |
||
236 | |||
237 | assert.Equal(t, uri, request.URL.Path) |
||
238 | assert.Equal(t, username, request.URL.Query().Get("username")) |
||
239 | assert.Equal(t, apiKey, request.URL.Query().Get("api_key")) |
||
240 | assert.Equal(t, smartcardNumber, request.URL.Query().Get("smartno")) |
||
241 | assert.Equal(t, string(DstvProductCodePremium), request.URL.Query().Get("product_code")) |
||
242 | assert.Equal(t, customerName, request.URL.Query().Get("customer_name")) |
||
243 | assert.Equal(t, customerNumber, request.URL.Query().Get("customer_number")) |
||
244 | assert.Equal(t, price, request.URL.Query().Get("price")) |
||
245 | assert.Equal(t, transactionID, request.URL.Query().Get("trans_id")) |
||
246 | |||
247 | // Teardown |
||
248 | server.Close() |
||
249 | }) |
||
250 | } |
||
251 | }) |
||
252 | |||
253 | t.Run("it returns an error when the api responds with an error", func(t *testing.T) { |
||
254 | t.Parallel() |
||
255 | |||
256 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
257 | for _, environment := range environments { |
||
258 | t.Run(environment.String(), func(t *testing.T) { |
||
259 | // Arrange |
||
260 | server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse()) |
||
261 | baseURL, _ := url.Parse(server.URL) |
||
262 | |||
263 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
264 | |||
265 | // Act |
||
266 | _, resp, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
267 | |||
268 | // Assert |
||
269 | assert.Error(t, err) |
||
270 | |||
271 | assert.Equal(t, "ERR101", resp.Error.Code) |
||
272 | assert.Equal(t, "Invalid username or api_key", resp.Error.Description) |
||
273 | |||
274 | server.Close() |
||
275 | }) |
||
276 | } |
||
277 | }) |
||
278 | |||
279 | t.Run("it returns an error when the context is cancelled", func(t *testing.T) { |
||
280 | t.Parallel() |
||
281 | |||
282 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
283 | for _, environment := range environments { |
||
284 | t.Run(environment.String(), func(t *testing.T) { |
||
285 | // Arrange |
||
286 | server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse()) |
||
287 | baseURL, _ := url.Parse(server.URL) |
||
288 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
289 | ctx, cancel := context.WithCancel(context.Background()) |
||
290 | cancel() |
||
291 | |||
292 | // Act |
||
293 | _, _, err := client.Bills.PayDStv(ctx, &PayDstvOptions{}) |
||
294 | |||
295 | // Assert |
||
296 | assert.True(t, errors.Is(err, context.Canceled)) |
||
297 | |||
298 | // Teardown |
||
299 | server.Close() |
||
300 | }) |
||
301 | } |
||
302 | }) |
||
303 | |||
304 | t.Run("it returns an error if the options is nil", func(t *testing.T) { |
||
305 | t.Parallel() |
||
306 | |||
307 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
308 | for _, environment := range environments { |
||
309 | t.Run(environment.String(), func(t *testing.T) { |
||
310 | // Arrange |
||
311 | server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse()) |
||
312 | baseURL, _ := url.Parse(server.URL) |
||
313 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
314 | ctx, cancel := context.WithCancel(context.Background()) |
||
315 | cancel() |
||
316 | |||
317 | // Act |
||
318 | _, _, err := client.Bills.PayDStv(ctx, nil) |
||
319 | |||
320 | // Assert |
||
321 | assert.Error(t, err) |
||
322 | |||
323 | // Teardown |
||
324 | server.Close() |
||
325 | }) |
||
326 | } |
||
327 | }) |
||
328 | |||
329 | t.Run("it returns an error when the response cannot be decoded", func(t *testing.T) { |
||
330 | t.Parallel() |
||
331 | |||
332 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
333 | for _, environment := range environments { |
||
334 | t.Run(environment.String(), func(t *testing.T) { |
||
335 | t.Parallel() |
||
336 | |||
337 | // Arrange |
||
338 | server := helpers.MakeTestServer(http.StatusOK, "<not-a-json></not-a-json>") |
||
339 | baseURL, _ := url.Parse(server.URL) |
||
340 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
341 | |||
342 | // Act |
||
343 | _, _, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
344 | |||
345 | // Assert |
||
346 | assert.Error(t, err) |
||
347 | |||
348 | // Teardown |
||
349 | server.Close() |
||
350 | }) |
||
494 |