forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors_test.go
232 lines (210 loc) · 6.67 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// +build unit
package braintree
import (
"encoding/xml"
"testing"
)
var errorXML = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<api-error-response>
<errors>
<errors type="array"/>
<transaction>
<errors type="array">
<error>
<code>91560</code>
<attribute type="symbol">base</attribute>
<message>Transaction could not be held in escrow.</message>
</error>
<error>
<code>81502</code>
<attribute type="symbol">amount</attribute>
<message>Amount is required.</message>
</error>
<error>
<code>91526</code>
<attribute type="symbol">custom_fields</attribute>
<message>Custom field is invalid: store_me.</message>
</error>
<error>
<code>91513</code>
<attribute type="symbol">merchant_account_id</attribute>
<message>Merchant account ID is invalid.</message>
</error>
<error>
<code>915157</code>
<attribute type="symbol">line_items</attribute>
<message>Too many line items.</message>
</error>
</errors>
<credit-card>
<errors type="array">
<error>
<code>91708</code>
<attribute type="symbol">base</attribute>
<message>Cannot provide expiration_date if you are also providing expiration_month and expiration_year.</message>
</error>
<error>
<code>81714</code>
<attribute type="symbol">number</attribute>
<message>Credit card number is required.</message>
</error>
<error>
<code>81725</code>
<attribute type="symbol">base</attribute>
<message>Credit card must include either number or venmo_sdk_payment_method_code.</message>
</error>
<error>
<code>81703</code>
<attribute type="symbol">number</attribute>
<message>Credit card type is not accepted by this merchant account.</message>
</error>
</errors>
</credit-card>
<customer>
<errors type="array">
<error>
<code>81606</code>
<attribute type="symbol">email</attribute>
<message>Email is an invalid format.</message>
</error>
</errors>
</customer>
<line-items>
<index-1>
<errors type="array">
<error>
<code>95801</code>
<attribute type="symbol">commodity_code</attribute>
<message>Commodity code is too long.</message>
</error>
</errors>
</index-1>
<index-3>
<errors type="array">
<error>
<code>95803</code>
<attribute type="symbol">description</attribute>
<message>Description is too long.</message>
</error>
<error>
<code>95809</code>
<attribute type="symbol">product_code</attribute>
<message>Product code is too long.</message>
</error>
</errors>
</index-3>
</line-items>
</transaction>
</errors>
<message>Everything is broken!</message>
</api-error-response>`)
func TestErrorsUnmarshalEverything(t *testing.T) {
t.Parallel()
apiErrors := &BraintreeError{}
err := xml.Unmarshal(errorXML, apiErrors)
if err != nil {
t.Fatal("Error unmarshalling: " + err.Error())
}
allErrors := apiErrors.All()
if g, w := len(allErrors), 13; g != w {
t.Fatalf("got %d errors, want %d errors", g, w)
}
}
func TestErrorsAccessors(t *testing.T) {
t.Parallel()
apiErrors := &BraintreeError{}
err := xml.Unmarshal(errorXML, apiErrors)
if err != nil {
t.Fatal("Error unmarshalling: " + err.Error())
}
ccObjectErrors := apiErrors.For("Transaction").For("CreditCard")
if g, w := ccObjectErrors.Object, "CreditCard"; g != w {
t.Errorf("cc object, got %q, want %q", g, w)
}
ccErrors := apiErrors.For("Transaction").For("CreditCard").All()
if len(ccErrors) != 4 {
t.Error("Did not get the right credit card errors")
}
numberErrors := apiErrors.For("Transaction").For("CreditCard").On("Number")
if len(numberErrors) != 2 {
t.Error("Did not get the right number errors")
}
customerErrors := apiErrors.For("Transaction").For("Customer").All()
if len(customerErrors) != 1 {
t.Error("Did not get the right customer errors")
}
lineItemsErrors := apiErrors.For("Transaction").On("LineItems")
if g, w := len(lineItemsErrors), 1; g != w {
t.Errorf("line items, got %d, want %d", g, w)
}
lineItem1CommodityCodeErrors := apiErrors.For("Transaction").For("LineItems").ForIndex(1).On("CommodityCode")
if g, w := len(lineItem1CommodityCodeErrors), 1; g != w {
t.Errorf("line item 1, got %d, want %d", g, w)
}
if g, w := lineItem1CommodityCodeErrors[0].Code, "95801"; g != w {
t.Errorf("line item 1, got %q, want %q", g, w)
}
if g, w := lineItem1CommodityCodeErrors[0].Attribute, "CommodityCode"; g != w {
t.Errorf("line item 1, got %q, want %q", g, w)
}
if g, w := lineItem1CommodityCodeErrors[0].Message, "Commodity code is too long."; g != w {
t.Errorf("line item 1, got %q, want %q", g, w)
}
lineItem3Errors := apiErrors.For("Transaction").For("LineItems").ForIndex(3).On("Description")
if g, w := len(lineItem3Errors), 1; g != w {
t.Errorf("line item 3, got %d, want %d", g, w)
}
baseErrors := apiErrors.For("Transaction").All()
if g, w := len(baseErrors), 5; g != w {
t.Errorf("transaction, got %d, want %d", g, w)
}
baseBaseErrors := apiErrors.For("Transaction").On("Base")
if g, w := len(baseBaseErrors), 1; g != w {
t.Errorf("transaction base, got %d, want %d", g, w)
}
}
func TestErrorsNameSnakeToCamel(t *testing.T) {
cases := []struct {
Snake string
Camel string
}{
{"amount", "Amount"},
{"index_1", "Index1"},
{"index_123", "Index123"},
{"commodity_code", "CommodityCode"},
{"description", "Description"},
}
for _, c := range cases {
t.Run(c.Snake+"=>"+c.Camel, func(t *testing.T) {
camel := errorNameSnakeToCamel(c.Snake)
if g, w := camel, c.Camel; g == w {
t.Logf("got %q, want %q", g, w)
} else {
t.Errorf("got %q, want %q", g, w)
}
})
}
}
func TestErrorsNameKebabToCamel(t *testing.T) {
cases := []struct {
Kebab string
Camel string
}{
{"amount", "Amount"},
{"line-items", "LineItems"},
{"index-1", "Index1"},
{"index-123", "Index123"},
{"commodity-code", "CommodityCode"},
{"description", "Description"},
}
for _, c := range cases {
t.Run(c.Kebab+"=>"+c.Camel, func(t *testing.T) {
camel := errorNameKebabToCamel(c.Kebab)
if g, w := camel, c.Camel; g == w {
t.Logf("got %q, want %q", g, w)
} else {
t.Errorf("got %q, want %q", g, w)
}
})
}
}