File: test_search_result_object.py

package info (click to toggle)
python-stripe 12.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,864 kB
  • sloc: python: 157,573; makefile: 13; sh: 9
file content (311 lines) | stat: -rw-r--r-- 9,868 bytes parent folder | download
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
import json

import pytest

import stripe


class TestSearchResultObject(object):
    @pytest.fixture
    def search_result_object(self):
        return stripe.SearchResultObject.construct_from(
            {"object": "search_result", "url": "/my/path", "data": ["foo"]},
            "mykey",
        )

    def test_search(self, http_client_mock, search_result_object):
        http_client_mock.stub_request(
            "get",
            path="/my/path",
            query_string="myparam=you",
            rbody=json.dumps(
                {
                    "object": "search_result",
                    "data": [{"object": "charge", "foo": "bar"}],
                }
            ),
        )

        res = search_result_object.search(
            myparam="you", stripe_account="acct_123"
        )

        http_client_mock.assert_requested(
            "get",
            path="/my/path",
            query_string="myparam=you",
            stripe_account="acct_123",
        )
        assert isinstance(res, stripe.SearchResultObject)
        assert res.stripe_account == "acct_123"
        assert isinstance(res.data, list)
        assert isinstance(res.data[0], stripe.Charge)
        assert res.data[0].foo == "bar"

    @pytest.mark.anyio
    async def test_search_async(self, http_client_mock, search_result_object):
        http_client_mock.stub_request(
            "get",
            path="/my/path",
            query_string="myparam=you",
            rbody=json.dumps(
                {
                    "object": "search_result",
                    "data": [{"object": "charge", "foo": "bar"}],
                }
            ),
        )

        res = await search_result_object._search_async(
            myparam="you", stripe_account="acct_123"
        )

        http_client_mock.assert_requested(
            "get",
            path="/my/path",
            query_string="myparam=you",
            stripe_account="acct_123",
        )
        assert isinstance(res, stripe.SearchResultObject)
        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_is_empty(self):
        sro = stripe.SearchResultObject.construct_from({"data": []}, None)
        assert sro.is_empty is True

    def test_empty_search_result(self):
        sro = stripe.SearchResultObject._empty_search_result()
        assert sro.is_empty

    def test_iter(self):
        arr = [{"id": 1}, {"id": 2}, {"id": 3}]
        expected = stripe.util.convert_to_stripe_object(arr, api_mode="V1")
        sro = stripe.SearchResultObject.construct_from({"data": arr}, None)
        assert list(sro) == expected

    def test_len(self, search_result_object):
        assert len(search_result_object) == 1

    def test_bool(self, search_result_object):
        assert search_result_object

        empty = stripe.SearchResultObject.construct_from(
            {"object": "search_result", "url": "/my/path", "data": []}, "mykey"
        )
        assert bool(empty) is False

    def test_next_search_result_page(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            {
                "object": "search_result",
                "data": [{"id": 1}],
                "has_more": True,
                "next_page": "next_page_token",
                "url": "/things",
            },
            None,
        )

        http_client_mock.stub_request(
            "get",
            path="/things",
            query_string="page=next_page_token",
            rbody=json.dumps(
                {
                    "object": "search_result",
                    "data": [{"id": 2}],
                    "has_more": False,
                    "url": "/things",
                }
            ),
        )

        next_sro = sro.next_search_result_page()

        http_client_mock.assert_requested(
            "get", path="/things", query_string="page=next_page_token"
        )
        assert not next_sro.is_empty
        assert next_sro.data[0].id == 2

    def test_next_search_result_page_with_filters(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            {
                "object": "search_result",
                "data": [{"id": 1}],
                "has_more": True,
                "next_page": "next_page_token",
                "url": "/things",
            },
            None,
        )
        sro._retrieve_params = {"expand": ["data.source"], "limit": 3}

        http_client_mock.stub_request(
            "get",
            path="/things",
            query_string="expand[0]=data.source&limit=3&page=next_page_token",
            rbody=json.dumps(
                {
                    "object": "search_result",
                    "data": [{"id": 2}],
                    "has_more": False,
                    "next_page": None,
                    "url": "/things",
                }
            ),
        )

        next_sro = sro.next_search_result_page()
        assert next_sro._retrieve_params == {
            "expand": ["data.source"],
            "limit": 3,
            "page": "next_page_token",
        }

    def test_next_search_result_page_empty_search_result(self):
        sro = stripe.SearchResultObject.construct_from(
            {
                "object": "search_result",
                "data": [{"id": 1}],
                "has_more": False,
                "next_page": None,
                "url": "/things",
            },
            None,
        )

        next_sro = sro.next_search_result_page()
        assert next_sro == stripe.SearchResultObject._empty_search_result()

    def test_serialize_empty_search_result(self):
        empty = stripe.SearchResultObject.construct_from(
            {"object": "search_result", "data": []}, "mykey"
        )
        serialized = str(empty)
        deserialized = stripe.SearchResultObject.construct_from(
            json.loads(serialized), "mykey"
        )
        assert deserialized == empty

    def test_serialize_nested_empty_search_result(self):
        empty = stripe.SearchResultObject.construct_from(
            {"object": "search_result", "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, next_page_token):
        model = {
            "object": "search_result",
            "url": "/v1/pageablemodels",
            "data": [{"id": id, "object": "pageablemodel"} for id in ids],
            "has_more": has_more,
            "next_page": next_page_token,
        }

        return model

    def test_iter_one_page(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            self.pageable_model_response(["pm_123", "pm_124"], False, None),
            "mykey",
        )

        http_client_mock.assert_no_request()

        seen = [item["id"] for item in sro.auto_paging_iter()]

        assert seen == ["pm_123", "pm_124"]

    def test_iter_two_pages(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            self.pageable_model_response(["pm_123", "pm_124"], True, "token"),
            "mykey",
        )
        sro._retrieve_params = {"foo": "bar"}

        http_client_mock.stub_request(
            "get",
            path="/v1/pageablemodels",
            query_string="page=token&foo=bar",
            rbody=json.dumps(
                self.pageable_model_response(["pm_125", "pm_126"], False, None)
            ),
        )

        seen = [item["id"] for item in sro.auto_paging_iter()]

        http_client_mock.assert_requested(
            "get",
            path="/v1/pageablemodels",
            query_string="page=token&foo=bar",
        )

        assert seen == ["pm_123", "pm_124", "pm_125", "pm_126"]


class TestAutoPagingAsync:
    @staticmethod
    def pageable_model_response(ids, has_more, next_page_token):
        model = {
            "object": "search_result",
            "url": "/v1/pageablemodels",
            "data": [{"id": id, "object": "pageablemodel"} for id in ids],
            "has_more": has_more,
            "next_page": next_page_token,
        }

        return model

    @pytest.mark.anyio
    async def test_iter_one_page(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            self.pageable_model_response(["pm_123", "pm_124"], False, None),
            "mykey",
        )

        http_client_mock.assert_no_request()

        seen = [item["id"] async for item in sro.auto_paging_iter()]

        assert seen == ["pm_123", "pm_124"]

    @pytest.mark.anyio
    async def test_iter_two_pages(self, http_client_mock):
        sro = stripe.SearchResultObject.construct_from(
            self.pageable_model_response(["pm_123", "pm_124"], True, "token"),
            "mykey",
        )
        sro._retrieve_params = {"foo": "bar"}

        http_client_mock.stub_request(
            "get",
            path="/v1/pageablemodels",
            query_string="page=token&foo=bar",
            rbody=json.dumps(
                self.pageable_model_response(["pm_125", "pm_126"], False, None)
            ),
        )

        seen = [item["id"] async for item in sro.auto_paging_iter()]

        http_client_mock.assert_requested(
            "get",
            path="/v1/pageablemodels",
            query_string="page=token&foo=bar",
        )

        assert seen == ["pm_123", "pm_124", "pm_125", "pm_126"]