Conditions | 11 |
Total Lines | 117 |
Code Lines | 66 |
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_CheckDStvUser 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 |
||
17 | func TestBillsService_CheckDStvUser(t *testing.T) { |
||
18 | t.Parallel() |
||
19 | t.Run("it returns the dstv user with a valid smartcard", func(t *testing.T) { |
||
20 | // Setup |
||
21 | t.Parallel() |
||
22 | server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse()) |
||
23 | |||
24 | // Arrange |
||
25 | baseURL, _ := url.Parse(server.URL) |
||
26 | client := New(WithBaseURL(baseURL)) |
||
27 | date, _ := time.Parse("2006-01-02T15:04:05-07:00", "2018-11-13T00:00:00+01:00") |
||
28 | |||
29 | // Act |
||
30 | user, _, err := client.Bills.CheckDStvUser(context.Background(), "4131953321") |
||
31 | |||
32 | // Assert |
||
33 | assert.NoError(t, err) |
||
34 | |||
35 | assert.Equal(t, "OPEN", user.Details.AccountStatus) |
||
36 | assert.Equal(t, "ESU", user.Details.Firstname) |
||
37 | assert.Equal(t, "INI OBONG BASSEY", user.Details.Lastname) |
||
38 | assert.Equal(t, "SUD", user.Details.CustomerType) |
||
39 | assert.Equal(t, 1, user.Details.InvoicePeriod) |
||
40 | assert.Equal(t, date, user.Details.DueDate) |
||
41 | assert.Equal(t, int64(275953782), user.Details.CustomerNumber) |
||
42 | |||
43 | // Teardown |
||
44 | server.Close() |
||
45 | }) |
||
46 | |||
47 | t.Run("it constructs the request correctly", func(t *testing.T) { |
||
48 | // Setup |
||
49 | t.Parallel() |
||
50 | |||
51 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
52 | for _, environment := range environments { |
||
53 | t.Run(environment.String(), func(t *testing.T) { |
||
54 | // Setup |
||
55 | t.Parallel() |
||
56 | |||
57 | // Arrange |
||
58 | request := new(http.Request) |
||
59 | server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request) |
||
60 | |||
61 | baseURL, _ := url.Parse(server.URL) |
||
62 | username := "test_username" |
||
63 | apiKey := "test_api_key" |
||
64 | smartcardNumber := "4131953321" |
||
65 | |||
66 | client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment)) |
||
67 | |||
68 | // Act |
||
69 | _, _, _ = client.Bills.CheckDStvUser(context.Background(), smartcardNumber) |
||
70 | |||
71 | // Assert |
||
72 | assert.Equal(t, "/bills/user_check", request.URL.Path) |
||
73 | assert.Equal(t, username, request.URL.Query().Get("username")) |
||
74 | assert.Equal(t, apiKey, request.URL.Query().Get("api_key")) |
||
75 | assert.Equal(t, "DSTV", request.URL.Query().Get("service")) |
||
76 | assert.Equal(t, smartcardNumber, request.URL.Query().Get("number")) |
||
77 | |||
78 | // Teardown |
||
79 | server.Close() |
||
80 | }) |
||
81 | } |
||
82 | }) |
||
83 | |||
84 | t.Run("it returns an error when the api responds with an error", func(t *testing.T) { |
||
85 | t.Parallel() |
||
86 | |||
87 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
88 | for _, environment := range environments { |
||
89 | t.Run(environment.String(), func(t *testing.T) { |
||
90 | // Setup |
||
91 | t.Parallel() |
||
92 | |||
93 | // Arrange |
||
94 | server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse()) |
||
95 | baseURL, _ := url.Parse(server.URL) |
||
96 | smartcardNumber := "4131953321" |
||
97 | |||
98 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
99 | |||
100 | // Act |
||
101 | _, resp, err := client.Bills.CheckDStvUser(context.Background(), smartcardNumber) |
||
102 | |||
103 | // Assert |
||
104 | assert.Error(t, err) |
||
105 | |||
106 | assert.Equal(t, "ERR101", resp.Error.Code) |
||
107 | assert.Equal(t, "Invalid username or api_key", resp.Error.Description) |
||
108 | |||
109 | // Teardown |
||
110 | server.Close() |
||
111 | }) |
||
112 | } |
||
113 | }) |
||
114 | |||
115 | t.Run("it returns an error when the context is cancelled", func(t *testing.T) { |
||
116 | t.Parallel() |
||
117 | |||
118 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
119 | for _, environment := range environments { |
||
120 | t.Run(environment.String(), func(t *testing.T) { |
||
121 | // Arrange |
||
122 | server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse()) |
||
123 | baseURL, _ := url.Parse(server.URL) |
||
124 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
125 | ctx, cancel := context.WithCancel(context.Background()) |
||
126 | cancel() |
||
127 | |||
128 | // Act |
||
129 | _, _, err := client.Bills.CheckDStvUser(ctx, "") |
||
130 | |||
131 | // Assert |
||
132 | assert.True(t, errors.Is(err, context.Canceled)) |
||
133 | server.Close() |
||
134 | }) |
||
381 |