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