-
Notifications
You must be signed in to change notification settings - Fork 409
/
test_list_object.py
350 lines (291 loc) · 10.1 KB
/
test_list_object.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from __future__ import absolute_import, division, print_function
import json
import pytest
import stripe
class TestListObject(object):
@pytest.fixture
def list_object(self):
return stripe.ListObject.construct_from(
{"object": "list", "url": "/my/path", "data": ["foo"]}, "mykey"
)
def test_list(self, request_mock, list_object):
request_mock.stub_request(
"get",
"/my/path",
{"object": "list", "data": [{"object": "charge", "foo": "bar"}]},
)
res = list_object.list(myparam="you", stripe_account="acct_123")
request_mock.assert_requested(
"get", "/my/path", {"myparam": "you"}, None
)
assert isinstance(res, stripe.ListObject)
assert res.stripe_account == "acct_123"
assert isinstance(res.data, list)
assert isinstance(res.data[0], stripe.Charge)
assert res.data[0].foo == "bar"
def test_create(self, request_mock, list_object):
request_mock.stub_request(
"post", "/my/path", {"object": "charge", "foo": "bar"}
)
res = list_object.create(myparam="eter", stripe_account="acct_123")
request_mock.assert_requested(
"post", "/my/path", {"myparam": "eter"}, None
)
assert isinstance(res, stripe.Charge)
assert res.foo == "bar"
assert res.stripe_account == "acct_123"
def test_create_maintains_list_properties(self, request_mock, list_object):
# Testing with real requests because our mock makes it impossible to
# test otherwise
customer = stripe.Customer.retrieve(
"cus_123", api_key="sk_test_custom"
)
res = customer.sources.create(source="tok_123")
request_mock.assert_requested(
"post",
"/v1/customers/cus_123/sources",
{"source": "tok_123"},
None,
)
assert res.api_key == "sk_test_custom"
def test_retrieve(self, request_mock, list_object):
request_mock.stub_request(
"get", "/my/path/myid", {"object": "charge", "foo": "bar"}
)
res = list_object.retrieve(
"myid", myparam="cow", stripe_account="acct_123"
)
request_mock.assert_requested(
"get", "/my/path/myid", {"myparam": "cow"}, None
)
assert isinstance(res, stripe.Charge)
assert res.foo == "bar"
assert res.stripe_account == "acct_123"
def test_is_empty(self):
lo = stripe.ListObject.construct_from({"data": []}, None)
assert lo.is_empty is True
def test_empty_list(self):
lo = stripe.ListObject.empty_list()
assert lo.is_empty
def test_iter(self):
arr = [{"id": 1}, {"id": 2}, {"id": 3}]
expected = stripe.util.convert_to_stripe_object(arr)
lo = stripe.ListObject.construct_from({"data": arr}, None)
assert list(lo) == expected
def test_iter_reversed(self):
arr = [{"id": 1}, {"id": 2}, {"id": 3}]
expected = stripe.util.convert_to_stripe_object(list(reversed(arr)))
lo = stripe.ListObject.construct_from({"data": arr}, None)
assert list(reversed(lo)) == expected
def test_len(self, list_object):
assert len(list_object) == 1
def test_bool(self, list_object):
assert list_object
empty = stripe.ListObject.construct_from(
{"object": "list", "url": "/my/path", "data": []}, "mykey"
)
assert bool(empty) is False
def test_next_page(self, request_mock):
lo = stripe.ListObject.construct_from(
{
"object": "list",
"data": [{"id": 1}],
"has_more": True,
"url": "/things",
},
None,
)
request_mock.stub_request(
"get",
"/things",
{
"object": "list",
"data": [{"id": 2}],
"has_more": False,
"url": "/things",
},
)
next_lo = lo.next_page()
assert not next_lo.is_empty
assert next_lo.data[0].id == 2
def test_next_page_with_filters(self, request_mock):
lo = stripe.ListObject.construct_from(
{
"object": "list",
"data": [{"id": 1}],
"has_more": True,
"url": "/things",
},
None,
)
lo._retrieve_params = {"expand": ["data.source"], "limit": 3}
request_mock.stub_request(
"get",
"/things",
{
"object": "list",
"data": [{"id": 2}],
"has_more": False,
"url": "/things",
},
)
next_lo = lo.next_page()
assert next_lo._retrieve_params == {
"expand": ["data.source"],
"limit": 3,
"starting_after": 1,
}
def test_next_page_empty_list(self):
lo = stripe.ListObject.construct_from(
{
"object": "list",
"data": [{"id": 1}],
"has_more": False,
"url": "/things",
},
None,
)
next_lo = lo.next_page()
assert next_lo == stripe.ListObject.empty_list()
def test_prev_page(self, request_mock):
lo = stripe.ListObject.construct_from(
{
"object": "list",
"data": [{"id": 2}],
"has_more": True,
"url": "/things",
},
None,
)
request_mock.stub_request(
"get",
"/things",
{
"object": "list",
"data": [{"id": 1}],
"has_more": False,
"url": "/things",
},
)
previous_lo = lo.previous_page()
assert not previous_lo.is_empty
assert previous_lo.data[0].id == 1
def test_prev_page_with_filters(self, request_mock):
lo = stripe.ListObject.construct_from(
{
"object": "list",
"data": [{"id": 2}],
"has_more": True,
"url": "/things",
},
None,
)
lo._retrieve_params = {"expand": ["data.source"], "limit": 3}
request_mock.stub_request(
"get",
"/things",
{
"object": "list",
"data": [{"id": 1}],
"has_more": False,
"url": "/things",
},
)
previous_lo = lo.previous_page()
assert previous_lo._retrieve_params == {
"expand": ["data.source"],
"limit": 3,
"ending_before": 2,
}
def test_serialize_empty_list(self):
empty = stripe.ListObject.construct_from(
{"object": "list", "data": []}, "mykey"
)
serialized = str(empty)
deserialized = stripe.ListObject.construct_from(
json.loads(serialized), "mykey"
)
assert deserialized == empty
def test_serialize_nested_empty_list(self):
empty = stripe.ListObject.construct_from(
{"object": "list", "data": []}, "mykey"
)
obj = stripe.stripe_object.StripeObject.construct_from(
{"nested": empty}, "mykey"
)
serialized = str(obj)
deserialized = stripe.stripe_object.StripeObject.construct_from(
json.loads(serialized), "mykey"
)
assert deserialized.nested == empty
class TestAutoPaging:
@staticmethod
def pageable_model_response(ids, has_more):
return {
"object": "list",
"url": "/v1/pageablemodels",
"data": [{"id": id, "object": "pageablemodel"} for id in ids],
"has_more": has_more,
}
def test_iter_one_page(self, request_mock):
lo = stripe.ListObject.construct_from(
self.pageable_model_response(["pm_123", "pm_124"], False), "mykey"
)
request_mock.assert_no_request()
seen = [item["id"] for item in lo.auto_paging_iter()]
assert seen == ["pm_123", "pm_124"]
def test_iter_two_pages(self, request_mock):
lo = stripe.ListObject.construct_from(
self.pageable_model_response(["pm_123", "pm_124"], True), "mykey"
)
lo._retrieve_params = {"foo": "bar"}
request_mock.stub_request(
"get",
"/v1/pageablemodels",
self.pageable_model_response(["pm_125", "pm_126"], False),
)
seen = [item["id"] for item in lo.auto_paging_iter()]
request_mock.assert_requested(
"get",
"/v1/pageablemodels",
{"starting_after": "pm_124", "foo": "bar"},
None,
)
assert seen == ["pm_123", "pm_124", "pm_125", "pm_126"]
def test_iter_reverse(self, request_mock):
lo = stripe.ListObject.construct_from(
self.pageable_model_response(["pm_125", "pm_126"], True), "mykey"
)
lo._retrieve_params = {"foo": "bar", "ending_before": "pm_127"}
request_mock.stub_request(
"get",
"/v1/pageablemodels",
self.pageable_model_response(["pm_123", "pm_124"], False),
)
seen = [item["id"] for item in lo.auto_paging_iter()]
request_mock.assert_requested(
"get",
"/v1/pageablemodels",
{"ending_before": "pm_125", "foo": "bar"},
None,
)
assert seen == ["pm_126", "pm_125", "pm_124", "pm_123"]
def test_class_method_two_pages(self, request_mock):
request_mock.stub_request(
"get",
"/v1/charges",
{
"object": "list",
"data": [{"id": "ch_001"}],
"url": "/v1/charges",
"has_more": False,
},
)
seen = [
item["id"]
for item in stripe.Charge.auto_paging_iter(limit=25, foo="bar")
]
request_mock.assert_requested(
"get", "/v1/charges", {"limit": 25, "foo": "bar"}
)
assert seen == ["ch_001"]