| Conditions | 17 |
| Total Lines | 296 |
| Code Lines | 210 |
| 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 mollie.TestSalesInvoicesService_Create 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 mollie |
||
| 14 | func TestSalesInvoicesService_Create(t *testing.T) { |
||
| 15 | setEnv() |
||
| 16 | defer unsetEnv() |
||
| 17 | |||
| 18 | type args struct { |
||
| 19 | ctx context.Context |
||
| 20 | req CreateSalesInvoice |
||
| 21 | } |
||
| 22 | |||
| 23 | recipient := SalesInvoiceRecipient{ |
||
| 24 | Type: ConsumerSalesInvoiceRecipientType, |
||
| 25 | Email: "[email protected]", |
||
| 26 | Address: Address{ |
||
| 27 | StreetAndNumber: "Keizersgracht 313", |
||
| 28 | PostalCode: "1016 EE", |
||
| 29 | City: "Amsterdam", |
||
| 30 | Country: "NL", |
||
| 31 | }, |
||
| 32 | Locale: Dutch, |
||
| 33 | } |
||
| 34 | |||
| 35 | lines := []SalesInvoiceLineItem{ |
||
| 36 | { |
||
| 37 | Description: "Product A", |
||
| 38 | Quantity: 2, |
||
| 39 | UnitPrice: Amount{ |
||
| 40 | Currency: "EUR", |
||
| 41 | Value: "50.00", |
||
| 42 | }, |
||
| 43 | VATRate: "21.00", |
||
| 44 | }, |
||
| 45 | } |
||
| 46 | |||
| 47 | issuedCreateHandler := func(w http.ResponseWriter, r *http.Request) { |
||
| 48 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 49 | testMethod(t, r, "POST") |
||
| 50 | |||
| 51 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 52 | w.WriteHeader(http.StatusUnauthorized) |
||
| 53 | return |
||
| 54 | } |
||
| 55 | |||
| 56 | var payload map[string]any |
||
| 57 | if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { |
||
| 58 | w.WriteHeader(http.StatusBadRequest) |
||
| 59 | return |
||
| 60 | } |
||
| 61 | |||
| 62 | status, _ := payload["status"].(string) |
||
| 63 | paymentDetails, hasPaymentDetails := payload["paymentDetails"].(map[string]any) |
||
| 64 | |||
| 65 | if hasPaymentDetails { |
||
| 66 | if source, ok := paymentDetails["source"].(string); !ok || source == "" { |
||
| 67 | w.Header().Set("Content-Type", "application/json") |
||
| 68 | w.WriteHeader(http.StatusUnprocessableEntity) |
||
| 69 | _, _ = w.Write([]byte(`{"status":422,"title":"Unprocessable Entity","detail":"The 'source' field is required when providing paymentDetails.","field":"paymentDetails.source"}`)) |
||
| 70 | return |
||
| 71 | } |
||
| 72 | |||
| 73 | if status == string(IssuedSalesInvoiceStatus) { |
||
| 74 | w.Header().Set("Content-Type", "application/json") |
||
| 75 | w.WriteHeader(http.StatusUnprocessableEntity) |
||
| 76 | _, _ = w.Write([]byte(`{"status":422,"title":"Unprocessable Entity","detail":"The 'paymentDetails' field is not allowed when status is 'issued'.","field":"paymentDetails"}`)) |
||
| 77 | return |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | w.Header().Set("Content-Type", "application/json") |
||
| 82 | w.WriteHeader(http.StatusCreated) |
||
| 83 | _, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse)) |
||
| 84 | } |
||
| 85 | |||
| 86 | cases := []struct { |
||
| 87 | name string |
||
| 88 | args args |
||
| 89 | wantErr bool |
||
| 90 | err error |
||
| 91 | pre func() |
||
| 92 | handler http.HandlerFunc |
||
| 93 | }{ |
||
| 94 | { |
||
| 95 | name: "create sales invoice successfully", |
||
| 96 | args: args{ |
||
| 97 | ctx: context.Background(), |
||
| 98 | req: CreateSalesInvoice{ |
||
| 99 | Status: DraftSalesInvoiceStatus, |
||
| 100 | RecipientIdentifier: "customer_123456789", |
||
| 101 | Recipient: recipient, |
||
| 102 | Lines: lines, |
||
| 103 | }, |
||
| 104 | }, |
||
| 105 | wantErr: false, |
||
| 106 | pre: noPre, |
||
| 107 | handler: func(w http.ResponseWriter, r *http.Request) { |
||
| 108 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 109 | testMethod(t, r, "POST") |
||
| 110 | |||
| 111 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 112 | w.WriteHeader(http.StatusUnauthorized) |
||
| 113 | } |
||
| 114 | |||
| 115 | w.Header().Set("Content-Type", "application/json") |
||
| 116 | w.WriteHeader(http.StatusCreated) |
||
| 117 | _, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse)) |
||
| 118 | }, |
||
| 119 | }, |
||
| 120 | { |
||
| 121 | "create sales invoice works as expected with access tokens", |
||
| 122 | args{ |
||
| 123 | context.Background(), |
||
| 124 | CreateSalesInvoice{ |
||
| 125 | Status: DraftSalesInvoiceStatus, |
||
| 126 | RecipientIdentifier: "customer_123456789", |
||
| 127 | Recipient: recipient, |
||
| 128 | Lines: lines, |
||
| 129 | }, |
||
| 130 | }, |
||
| 131 | false, |
||
| 132 | nil, |
||
| 133 | setAccessToken, |
||
| 134 | func(w http.ResponseWriter, r *http.Request) { |
||
| 135 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
| 136 | testMethod(t, r, "POST") |
||
| 137 | |||
| 138 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 139 | w.WriteHeader(http.StatusUnauthorized) |
||
| 140 | } |
||
| 141 | |||
| 142 | w.WriteHeader(http.StatusCreated) |
||
| 143 | _, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse)) |
||
| 144 | }, |
||
| 145 | }, |
||
| 146 | { |
||
| 147 | "create sales invoices error handler", |
||
| 148 | args{ |
||
| 149 | ctx: context.Background(), |
||
| 150 | req: CreateSalesInvoice{ |
||
| 151 | Status: DraftSalesInvoiceStatus, |
||
| 152 | RecipientIdentifier: "customer_123456789", |
||
| 153 | Recipient: recipient, |
||
| 154 | Lines: lines, |
||
| 155 | }, |
||
| 156 | }, |
||
| 157 | true, |
||
| 158 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
| 159 | noPre, |
||
| 160 | errorHandler, |
||
| 161 | }, |
||
| 162 | { |
||
| 163 | "create sales invoice, an error occurs when parsing json", |
||
| 164 | args{ |
||
| 165 | ctx: context.Background(), |
||
| 166 | req: CreateSalesInvoice{ |
||
| 167 | Status: DraftSalesInvoiceStatus, |
||
| 168 | RecipientIdentifier: "customer_123456789", |
||
| 169 | Recipient: recipient, |
||
| 170 | Lines: lines, |
||
| 171 | }, |
||
| 172 | }, |
||
| 173 | true, |
||
| 174 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 175 | noPre, |
||
| 176 | encodingHandler, |
||
| 177 | }, |
||
| 178 | { |
||
| 179 | "create sales invoice, invalid url when building request", |
||
| 180 | args{ |
||
| 181 | ctx: context.Background(), |
||
| 182 | req: CreateSalesInvoice{ |
||
| 183 | Status: DraftSalesInvoiceStatus, |
||
| 184 | RecipientIdentifier: "customer_123456789", |
||
| 185 | Recipient: recipient, |
||
| 186 | Lines: lines, |
||
| 187 | }, |
||
| 188 | }, |
||
| 189 | true, |
||
| 190 | errBadBaseURL, |
||
| 191 | crashSrv, |
||
| 192 | errorHandler, |
||
| 193 | }, |
||
| 194 | { |
||
| 195 | name: "create sales invoice, payment details list in response", |
||
| 196 | args: args{ |
||
| 197 | ctx: context.Background(), |
||
| 198 | req: CreateSalesInvoice{ |
||
| 199 | Status: DraftSalesInvoiceStatus, |
||
| 200 | RecipientIdentifier: "customer_123456789", |
||
| 201 | Recipient: recipient, |
||
| 202 | Lines: lines, |
||
| 203 | }, |
||
| 204 | }, |
||
| 205 | wantErr: false, |
||
| 206 | err: nil, |
||
| 207 | pre: noPre, |
||
| 208 | handler: func(w http.ResponseWriter, r *http.Request) { |
||
| 209 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 210 | testMethod(t, r, "POST") |
||
| 211 | |||
| 212 | if _, ok := r.Header[AuthHeader]; !ok { |
||
| 213 | w.WriteHeader(http.StatusUnauthorized) |
||
| 214 | return |
||
| 215 | } |
||
| 216 | |||
| 217 | w.Header().Set("Content-Type", "application/json") |
||
| 218 | w.WriteHeader(http.StatusCreated) |
||
| 219 | _, _ = w.Write([]byte(`{ |
||
| 220 | "resource": "sales-invoice", |
||
| 221 | "id": "invoice_4Y0eZitmBnQ6IDoMqZQKh", |
||
| 222 | "status": "paid", |
||
| 223 | "paymentDetails": [ |
||
| 224 | { "source": "manual", "sourceReference": "ref_1" }, |
||
| 225 | { "source": "payment", "sourceReference": "tr_2" } |
||
| 226 | ] |
||
| 227 | }`)) |
||
| 228 | }, |
||
| 229 | }, |
||
| 230 | { |
||
| 231 | name: "create issued sales invoice, without payment details", |
||
| 232 | args: args{ |
||
| 233 | ctx: context.Background(), |
||
| 234 | req: CreateSalesInvoice{ |
||
| 235 | Status: IssuedSalesInvoiceStatus, |
||
| 236 | RecipientIdentifier: "customer_123456789", |
||
| 237 | Recipient: recipient, |
||
| 238 | Lines: lines, |
||
| 239 | }, |
||
| 240 | }, |
||
| 241 | wantErr: false, |
||
| 242 | err: nil, |
||
| 243 | pre: noPre, |
||
| 244 | handler: issuedCreateHandler, |
||
| 245 | }, |
||
| 246 | { |
||
| 247 | name: "create issued sales invoice, payment details without source returns error", |
||
| 248 | args: args{ |
||
| 249 | ctx: context.Background(), |
||
| 250 | req: CreateSalesInvoice{ |
||
| 251 | Status: IssuedSalesInvoiceStatus, |
||
| 252 | RecipientIdentifier: "customer_123456789", |
||
| 253 | Recipient: recipient, |
||
| 254 | Lines: lines, |
||
| 255 | PaymentDetails: &SalesInvoicePaymentDetails{}, |
||
| 256 | }, |
||
| 257 | }, |
||
| 258 | wantErr: true, |
||
| 259 | err: &BaseError{ |
||
| 260 | Status: http.StatusUnprocessableEntity, |
||
| 261 | Title: "Unprocessable Entity", |
||
| 262 | Detail: "The 'source' field is required when providing paymentDetails.", |
||
| 263 | Field: "paymentDetails.source", |
||
| 264 | }, |
||
| 265 | pre: noPre, |
||
| 266 | handler: issuedCreateHandler, |
||
| 267 | }, |
||
| 268 | { |
||
| 269 | name: "create issued sales invoice, payment details with source returns error", |
||
| 270 | args: args{ |
||
| 271 | ctx: context.Background(), |
||
| 272 | req: CreateSalesInvoice{ |
||
| 273 | Status: IssuedSalesInvoiceStatus, |
||
| 274 | RecipientIdentifier: "customer_123456789", |
||
| 275 | Recipient: recipient, |
||
| 276 | Lines: lines, |
||
| 277 | PaymentDetails: &SalesInvoicePaymentDetails{ |
||
| 278 | Source: ManualSalesInvoiceSource, |
||
| 279 | }, |
||
| 280 | }, |
||
| 281 | }, |
||
| 282 | wantErr: true, |
||
| 283 | err: &BaseError{ |
||
| 284 | Status: http.StatusUnprocessableEntity, |
||
| 285 | Title: "Unprocessable Entity", |
||
| 286 | Detail: "The 'paymentDetails' field is not allowed when status is 'issued'.", |
||
| 287 | Field: "paymentDetails", |
||
| 288 | }, |
||
| 289 | pre: noPre, |
||
| 290 | handler: issuedCreateHandler, |
||
| 291 | }, |
||
| 292 | } |
||
| 293 | |||
| 294 | for _, c := range cases { |
||
| 295 | setup() |
||
| 296 | defer teardown() |
||
| 297 | |||
| 298 | t.Run(c.name, func(t *testing.T) { |
||
| 299 | c.pre() |
||
| 300 | tMux.HandleFunc("/v2/sales-invoices", c.handler) |
||
| 301 | |||
| 302 | res, m, err := tClient.SalesInvoices.Create(c.args.ctx, c.args.req) |
||
| 303 | if c.wantErr { |
||
| 304 | assert.NotNil(t, err) |
||
| 305 | assert.EqualError(t, err, c.err.Error()) |
||
| 306 | } else { |
||
| 307 | assert.Nil(t, err) |
||
| 308 | assert.IsType(t, &SalesInvoice{}, m) |
||
| 309 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 310 | } |
||
| 779 |