Conditions | 9 |
Total Lines | 103 |
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:
1 | package mobilenig |
||
116 | func TestBillsService_PayDStv(t *testing.T) { |
||
117 | t.Parallel() |
||
118 | t.Run("it returns the dstv transaction response with a valid payment", func(t *testing.T) { |
||
119 | t.Parallel() |
||
120 | |||
121 | // Arrange |
||
122 | server := helpers.MakeTestServer(http.StatusOK, stubs.PayDstvBillResponse()) |
||
123 | baseURL, _ := url.Parse(server.URL) |
||
124 | client := New(WithBaseURL(baseURL)) |
||
125 | |||
126 | // Act |
||
127 | transaction, _, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
128 | |||
129 | // Assert |
||
130 | assert.NoError(t, err) |
||
131 | |||
132 | assert.Equal(t, "122790223", transaction.TransactionID) |
||
133 | assert.Equal(t, "DSTV", transaction.Details.Service) |
||
134 | assert.Equal(t, "DStv Mobile MAXI", transaction.Details.Package) |
||
135 | assert.Equal(t, "4131953321", transaction.Details.SmartcardNumber) |
||
136 | assert.Equal(t, "790", transaction.Details.Price) |
||
137 | assert.Equal(t, "SUCCESSFUL", transaction.Details.Status) |
||
138 | assert.Equal(t, "7931", transaction.Details.Balance) |
||
139 | |||
140 | // Teardown |
||
141 | server.Close() |
||
142 | }) |
||
143 | |||
144 | t.Run("it constructs the request correctly", func(t *testing.T) { |
||
145 | t.Parallel() |
||
146 | |||
147 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
148 | for _, environment := range environments { |
||
149 | t.Run(environment.String(), func(t *testing.T) { |
||
150 | // Arrange |
||
151 | request := new(http.Request) |
||
152 | server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request) |
||
153 | |||
154 | baseURL, _ := url.Parse(server.URL) |
||
155 | username := "test_username" |
||
156 | apiKey := "test_api_key" |
||
157 | smartcardNumber := "4131953321" |
||
158 | customerNumber := "275953782" |
||
159 | customerName := "ESU INI OBONG BASSEY" |
||
160 | price := "790" |
||
161 | transactionID := "122790223" |
||
162 | |||
163 | client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment)) |
||
164 | |||
165 | // Act |
||
166 | _, _, _ = client.Bills.PayDStv(context.Background(), &PayDstvOptions{ |
||
167 | TransactionID: transactionID, |
||
168 | Price: price, |
||
169 | ProductCode: DstvProductCodePremium, |
||
170 | CustomerName: customerName, |
||
171 | CustomerNumber: customerNumber, |
||
172 | SmartcardNumber: smartcardNumber, |
||
173 | }) |
||
174 | |||
175 | // Assert |
||
176 | uri := "/bills/dstv" |
||
177 | if environment == TestEnvironment { |
||
178 | uri += "_test" |
||
179 | } |
||
180 | |||
181 | assert.Equal(t, uri, request.URL.Path) |
||
182 | assert.Equal(t, username, request.URL.Query().Get("username")) |
||
183 | assert.Equal(t, apiKey, request.URL.Query().Get("api_key")) |
||
184 | assert.Equal(t, smartcardNumber, request.URL.Query().Get("smartno")) |
||
185 | assert.Equal(t, string(DstvProductCodePremium), request.URL.Query().Get("product_code")) |
||
186 | assert.Equal(t, customerName, request.URL.Query().Get("customer_name")) |
||
187 | assert.Equal(t, customerNumber, request.URL.Query().Get("customer_number")) |
||
188 | assert.Equal(t, price, request.URL.Query().Get("price")) |
||
189 | assert.Equal(t, transactionID, request.URL.Query().Get("trans_id")) |
||
190 | |||
191 | // Teardown |
||
192 | server.Close() |
||
193 | }) |
||
194 | } |
||
195 | }) |
||
196 | |||
197 | t.Run("it returns an error when the api responds with an error", func(t *testing.T) { |
||
198 | t.Parallel() |
||
199 | |||
200 | environments := []Environment{LiveEnvironment, TestEnvironment} |
||
201 | for _, environment := range environments { |
||
202 | t.Run(environment.String(), func(t *testing.T) { |
||
203 | // Arrange |
||
204 | server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse()) |
||
205 | baseURL, _ := url.Parse(server.URL) |
||
206 | |||
207 | client := New(WithBaseURL(baseURL), WithEnvironment(environment)) |
||
208 | |||
209 | // Act |
||
210 | _, resp, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{}) |
||
211 | |||
212 | // Assert |
||
213 | assert.Error(t, err) |
||
214 | |||
215 | assert.Equal(t, "ERR101", resp.Error.Code) |
||
216 | assert.Equal(t, "Invalid username or api_key", resp.Error.Description) |
||
217 | |||
218 | server.Close() |
||
219 | }) |
||
311 |