| 1 |
from stripe._searchable_api_resource import SearchableAPIResource |
| 2 |
from stripe._charge import Charge |
| 3 |
|
| 4 |
|
| 5 |
class TestSearchableAPIResource(object): |
| 6 |
class MySearchable(SearchableAPIResource): |
| 7 |
OBJECT_NAME = "mysearchable" |
| 8 |
|
| 9 |
@classmethod |
| 10 |
def search(cls, *args, **kwargs): |
| 11 |
return cls._search( |
| 12 |
search_url="/v1/mysearchables/search", *args, **kwargs |
| 13 |
) |
| 14 |
|
| 15 |
def test_search(self, http_client_mock): |
| 16 |
path = "/v1/mysearchables/search" |
| 17 |
query_string = "query=currency%3A%22CAD%22" |
| 18 |
http_client_mock.stub_request( |
| 19 |
"get", |
| 20 |
path, |
| 21 |
query_string, |
| 22 |
'{"object": "search_result", "data": [{"object": "charge", "name": "jose"}, {"object": "charge", "name": "curly"}], "url": "/v1/charges", "has_more": false, "next_page": null}', |
| 23 |
rheaders={"request-id": "req_id"}, |
| 24 |
) |
| 25 |
|
| 26 |
res = self.MySearchable.search(query='currency:"CAD"') |
| 27 |
http_client_mock.assert_requested( |
| 28 |
"get", path=path, query_string=query_string |
| 29 |
) |
| 30 |
assert len(res.data) == 2 |
| 31 |
assert all(isinstance(obj, Charge) for obj in res.data) |
| 32 |
assert res.data[0].name == "jose" |
| 33 |
assert res.data[1].name == "curly" |
| 34 |
|
| 35 |
assert res.last_response is not None |
| 36 |
assert res.last_response.request_id == "req_id" |
| 37 |
|
| 38 |
def test_search_multiple_pages(self, http_client_mock): |
| 39 |
path = "/v1/mysearchables/search" |
| 40 |
query_string = 'query=currency:"CAD"' |
| 41 |
http_client_mock.stub_request( |
| 42 |
"get", |
| 43 |
path, |
| 44 |
query_string, |
| 45 |
'{"object": "search_result", "data": [{"object": "charge", "name": "jose"}, {"object": "charge", "name": "curly"}], "url": "/v1/charges", "has_more": true, "next_page": "next-page-token"}', |
| 46 |
rheaders={"request-id": "req_id"}, |
| 47 |
) |
| 48 |
|
| 49 |
res = self.MySearchable.search(query='currency:"CAD"') |
| 50 |
http_client_mock.assert_requested( |
| 51 |
"get", path=path, query_string=query_string |
| 52 |
) |
| 53 |
|
| 54 |
assert res.next_page == "next-page-token" |
| 55 |
|
| 56 |
query_string = 'page=next-page-token&query=currency:"CAD"' |
| 57 |
http_client_mock.stub_request( |
| 58 |
"get", |
| 59 |
path, |
| 60 |
query_string, |
| 61 |
'{"object": "search_result", "data": [{"object": "charge", "name": "test"}], "url": "/v1/charges", "has_more": false, "next_page": null}', |
| 62 |
rheaders={"request-id": "req_id"}, |
| 63 |
) |
| 64 |
res2 = self.MySearchable.search( |
| 65 |
query='currency:"CAD"', page=res.next_page |
| 66 |
) |
| 67 |
http_client_mock.assert_requested( |
| 68 |
"get", |
| 69 |
path=path, |
| 70 |
query_string=query_string, |
| 71 |
) |
| 72 |
|
| 73 |
assert len(res2.data) == 1 |
| 74 |
assert all(isinstance(obj, Charge) for obj in res2.data) |
| 75 |
assert res2.data[0].name == "test" |