forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
257 lines (217 loc) · 5.93 KB
/
params_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package stripe_test
import (
"context"
"testing"
assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
. "github.com/stripe/stripe-go/testing"
)
func TestRangeQueryParamsAppendTo(t *testing.T) {
type testParams struct {
CreatedRange *stripe.RangeQueryParams `form:"created"`
}
{
body := &form.Values{}
// Try it with an empty set of parameters
params := testParams{
CreatedRange: &stripe.RangeQueryParams{},
}
form.AppendTo(body, params)
assert.True(t, body.Empty())
}
{
body := &form.Values{}
params := testParams{
CreatedRange: &stripe.RangeQueryParams{GreaterThan: 99},
}
form.AppendTo(body, params)
assert.Equal(t, []string{"99"}, body.Get("created[gt]"))
}
{
body := &form.Values{}
params := testParams{
CreatedRange: &stripe.RangeQueryParams{GreaterThanOrEqual: 99},
}
form.AppendTo(body, params)
assert.Equal(t, []string{"99"}, body.Get("created[gte]"))
}
{
body := &form.Values{}
params := testParams{
CreatedRange: &stripe.RangeQueryParams{LesserThan: 99},
}
form.AppendTo(body, params)
assert.Equal(t, []string{"99"}, body.Get("created[lt]"))
}
{
body := &form.Values{}
params := testParams{
CreatedRange: &stripe.RangeQueryParams{LesserThanOrEqual: 99},
}
form.AppendTo(body, params)
assert.Equal(t, []string{"99"}, body.Get("created[lte]"))
}
}
type testListParams struct {
stripe.ListParams `form:"*"`
Field string `form:"field"`
}
func TestListParams_Nested(t *testing.T) {
params := &testListParams{
Field: "field_value",
ListParams: stripe.ListParams{
EndingBefore: stripe.String("acct_123"),
Limit: stripe.Int64(100),
StartingAfter: stripe.String("acct_123"),
},
}
body := &form.Values{}
form.AppendTo(body, params)
assert.Equal(t, valuesFromArray([][2]string{
{"ending_before", "acct_123"},
{"limit", "100"},
{"starting_after", "acct_123"},
{"field", "field_value"},
}), body)
}
func TestParams_AppendTo_Extra(t *testing.T) {
testCases := []struct {
InitialBody [][2]string
Extras [][2]string
ExpectedBody [][2]string
}{
{
InitialBody: [][2]string{{"foo", "bar"}},
Extras: [][2]string{},
ExpectedBody: [][2]string{{"foo", "bar"}},
},
{
InitialBody: [][2]string{{"foo", "bar"}},
Extras: [][2]string{{"foo", "baz"}},
ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}},
},
}
for _, testCase := range testCases {
p := &testParams{}
for _, extra := range testCase.Extras {
p.AddExtra(extra[0], extra[1])
}
body := valuesFromArray(testCase.InitialBody)
form.AppendTo(body, p)
assert.Equal(t, valuesFromArray(testCase.ExpectedBody), body)
}
}
type testParams struct {
stripe.Params `form:"*"`
Field string `form:"field"`
SubParams *testSubParams `form:"sub_params"`
}
// AppendTo is implemented for testParams so that we can verify that Params is
// encoded properly even in the case where a leaf struct does a custom
// override.
func (p *testParams) AppendTo(body *form.Values, keyParts []string) {
}
type testSubParams struct {
stripe.Params `form:"*"`
SubField string `form:"sub_field"`
}
func TestParams_AppendTo_Nested(t *testing.T) {
params := &testParams{
Field: "field_value",
Params: stripe.Params{
Metadata: map[string]string{
"foo": "bar",
},
},
SubParams: &testSubParams{
Params: stripe.Params{
Metadata: map[string]string{
"sub_foo": "bar",
},
},
SubField: "sub_field_value",
},
}
body := &form.Values{}
form.AppendTo(body, params)
assert.Equal(t, valuesFromArray([][2]string{
{"metadata[foo]", "bar"},
{"field", "field_value"},
{"sub_params[metadata][sub_foo]", "bar"},
{"sub_params[sub_field]", "sub_field_value"},
}), body)
}
func TestListParams_Filters(t *testing.T) {
p := &testListParams{}
p.Filters.AddFilter("created", "gt", "123")
body := &form.Values{}
form.AppendTo(body, p)
assert.Equal(t, valuesFromArray([][2]string{
{"created[gt]", "123"},
}), body)
}
func TestListParams_Expand(t *testing.T) {
testCases := []struct {
InitialBody [][2]string
Expand []string
ExpectedBody [][2]string
}{
{
InitialBody: [][2]string{{"foo", "bar"}},
Expand: []string{},
ExpectedBody: [][2]string{{"foo", "bar"}},
},
{
InitialBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}},
Expand: []string{"data", "data.foo"},
ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}, {"expand[]", "data"}, {"expand[]", "data.foo"}},
},
}
for _, testCase := range testCases {
p := stripe.ListParams{}
for _, exp := range testCase.Expand {
p.AddExpand(exp)
}
body := valuesFromArray(testCase.InitialBody)
form.AppendTo(body, p)
assert.Equal(t, valuesFromArray(testCase.ExpectedBody), body)
}
}
func TestListParams_SetStripeAccount(t *testing.T) {
p := &stripe.ListParams{}
p.SetStripeAccount(TestMerchantID)
assert.Equal(t, TestMerchantID, *p.StripeAccount)
}
func TestListParams_ToParams(t *testing.T) {
listParams := &stripe.ListParams{
Context: context.Background(),
}
listParams.SetStripeAccount(TestMerchantID)
params := listParams.ToParams()
assert.Equal(t, listParams.Context, params.Context)
assert.Equal(t, *listParams.StripeAccount, *params.StripeAccount)
}
func TestParams_SetIdempotencyKey(t *testing.T) {
p := &stripe.Params{}
p.SetIdempotencyKey("my-idempotency-key")
assert.Equal(t, "my-idempotency-key", *p.IdempotencyKey)
}
func TestParams_SetStripeAccount(t *testing.T) {
p := &stripe.Params{}
p.SetStripeAccount(TestMerchantID)
assert.Equal(t, TestMerchantID, *p.StripeAccount)
}
//
// ---
//
// Converts a collection of key/value tuples in a two dimensional slice/array
// into form.Values form. The purpose of this is that it's much cleaner to
// initialize the array all at once on a single line.
func valuesFromArray(arr [][2]string) *form.Values {
body := &form.Values{}
for _, v := range arr {
body.Add(v[0], v[1])
}
return body
}