File: test_searchable_api_resource.py

package info (click to toggle)
python-stripe 13.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,476 kB
  • sloc: python: 187,843; makefile: 13; sh: 9
file content (75 lines) | stat: -rw-r--r-- 2,851 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
from stripe._searchable_api_resource import SearchableAPIResource
from stripe._charge import Charge


class TestSearchableAPIResource(object):
    class MySearchable(SearchableAPIResource):
        OBJECT_NAME = "mysearchable"

        @classmethod
        def search(cls, *args, **kwargs):
            return cls._search(
                search_url="/v1/mysearchables/search", *args, **kwargs
            )

    def test_search(self, http_client_mock):
        path = "/v1/mysearchables/search"
        query_string = "query=currency%3A%22CAD%22"
        http_client_mock.stub_request(
            "get",
            path,
            query_string,
            '{"object": "search_result", "data": [{"object": "charge", "name": "jose"}, {"object": "charge", "name": "curly"}], "url": "/v1/charges", "has_more": false, "next_page": null}',
            rheaders={"request-id": "req_id"},
        )

        res = self.MySearchable.search(query='currency:"CAD"')
        http_client_mock.assert_requested(
            "get", path=path, query_string=query_string
        )
        assert len(res.data) == 2
        assert all(isinstance(obj, Charge) for obj in res.data)
        assert res.data[0].name == "jose"
        assert res.data[1].name == "curly"

        assert res.last_response is not None
        assert res.last_response.request_id == "req_id"

    def test_search_multiple_pages(self, http_client_mock):
        path = "/v1/mysearchables/search"
        query_string = 'query=currency:"CAD"'
        http_client_mock.stub_request(
            "get",
            path,
            query_string,
            '{"object": "search_result", "data": [{"object": "charge", "name": "jose"}, {"object": "charge", "name": "curly"}], "url": "/v1/charges", "has_more": true, "next_page": "next-page-token"}',
            rheaders={"request-id": "req_id"},
        )

        res = self.MySearchable.search(query='currency:"CAD"')
        http_client_mock.assert_requested(
            "get", path=path, query_string=query_string
        )

        assert res.next_page == "next-page-token"

        query_string = 'page=next-page-token&query=currency:"CAD"'
        http_client_mock.stub_request(
            "get",
            path,
            query_string,
            '{"object": "search_result", "data": [{"object": "charge", "name": "test"}], "url": "/v1/charges", "has_more": false, "next_page": null}',
            rheaders={"request-id": "req_id"},
        )
        res2 = self.MySearchable.search(
            query='currency:"CAD"', page=res.next_page
        )
        http_client_mock.assert_requested(
            "get",
            path=path,
            query_string=query_string,
        )

        assert len(res2.data) == 1
        assert all(isinstance(obj, Charge) for obj in res2.data)
        assert res2.data[0].name == "test"