File: test_list_object_v2.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 (147 lines) | stat: -rw-r--r-- 4,279 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
from __future__ import absolute_import, division, print_function

import json

import pytest

import stripe
from stripe.v2._list_object import ListObject
from tests.http_client_mock import HTTPClientMock


class TestListObjectV2(object):
    @pytest.fixture
    def list_object(self):
        return ListObject.construct_from(
            {
                "data": ["a", "b", "c"],
                "next_page_url": None,
                "previous_page_url": None,
            },
            "mykey",
        )

    def test_iter(self):
        arr = ["a", "b", "c"]
        expected = stripe.util.convert_to_stripe_object(arr, api_mode="V2")
        lo = ListObject.construct_from({"data": arr}, None)
        assert list(lo) == expected

    @staticmethod
    def pageable_model_response(ids, next_page_url):
        return {
            "data": [{"id": id, "object": "pageablemodel"} for id in ids],
            "next_page_url": next_page_url,
        }

    def test_iter_one_page(self, http_client_mock):
        lo = ListObject.construct_from(
            self.pageable_model_response(["pm_123", "pm_124"], None), "mykey"
        )

        http_client_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, http_client_mock):
        method = "get"
        path = "/v2/pageablemodels"

        lo = ListObject.construct_from(
            self.pageable_model_response(
                ["pm_123", "pm_124"], "/v2/pageablemodels?foo=bar&page=page_2"
            ),
            None,
        )

        http_client_mock.stub_request(
            method,
            path=path,
            query_string="foo=bar&page=page_3",
            rbody=json.dumps(
                self.pageable_model_response(["pm_127", "pm_128"], None)
            ),
        )

        http_client_mock.stub_request(
            method,
            path=path,
            query_string="foo=bar&page=page_2",
            rbody=json.dumps(
                self.pageable_model_response(
                    ["pm_125", "pm_126"],
                    "/v2/pageablemodels?foo=bar&page=page_3",
                )
            ),
        )

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

        http_client_mock.assert_requested(
            method, path=path, query_string="foo=bar&page=page_2"
        )

        http_client_mock.assert_requested(
            method, path=path, query_string="foo=bar&page=page_3"
        )

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

    def test_iter_forwards_api_key(self, http_client_mock: HTTPClientMock):
        client = stripe.StripeClient(
            http_client=http_client_mock.get_mock_http_client(),
            api_key="sk_test_xyz",
        )

        method = "get"
        query_string_1 = "object_id=obj_123"
        query_string_2 = "object_id=obj_123&page=page_2"
        path = "/v2/core/events"

        http_client_mock.stub_request(
            method,
            path=path,
            query_string=query_string_1,
            rbody='{"data": [{"id": "x"}], "next_page_url": "/v2/core/events?object_id=obj_123&page=page_2"}',
            rcode=200,
            rheaders={},
        )

        http_client_mock.stub_request(
            method,
            path=path,
            query_string=query_string_2,
            rbody='{"data": [{"id": "y"}, {"id": "z"}], "next_page_url": null}',
            rcode=200,
            rheaders={},
        )

        lo = client.v2.core.events.list(
            params={"object_id": "obj_123"},
            options={"api_key": "sk_test_iter_forwards_options"},
        )

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

        assert seen == ["x", "y", "z"]
        http_client_mock.assert_requested(
            method,
            path=path,
            query_string=query_string_1,
            api_key="sk_test_iter_forwards_options",
        )
        http_client_mock.assert_requested(
            method,
            path=path,
            query_string=query_string_2,
            api_key="sk_test_iter_forwards_options",
        )