Skip to content

Commit

Permalink
Replace request_mock with http_client_mock in tests (#1179)
Browse files Browse the repository at this point in the history
* Replace request_mock with http_client_mock in tests

* Support reserved word resource properties

* Add StripeObject test

* fmt

* Use http_client_mock in generated tests

* Add back APIResource test

* fmt

* Don't include APIRequestor-related test changes

* remove unnecessary check in util test

* Revert test_integration.py changes
  • Loading branch information
anniel-stripe authored Dec 19, 2023
1 parent 3a34544 commit de0a704
Show file tree
Hide file tree
Showing 92 changed files with 4,797 additions and 3,170 deletions.
50 changes: 33 additions & 17 deletions tests/api_resources/abstract/test_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,68 @@ class TestAPIResource(object):
class MyResource(stripe.api_resources.abstract.APIResource):
OBJECT_NAME = "myresource"

def test_retrieve_and_refresh(self, request_mock):
url = "/v1/myresources/foo%2A"
request_mock.stub_request(
def test_retrieve_and_refresh(self, http_client_mock):
path = "/v1/myresources/foo%2A"
query_string = "myparam=5"
http_client_mock.stub_request(
"get",
url,
{"id": "foo2", "bobble": "scrobble"},
path,
query_string,
'{"id": "foo2", "bobble": "scrobble"}',
rheaders={"request-id": "req_id"},
)

res = self.MyResource.retrieve("foo*", myparam=5)

request_mock.assert_requested("get", url, {"myparam": 5}, None)
http_client_mock.assert_requested(
"get", path=path, query_string=query_string
)
assert res.bobble == "scrobble"
assert res.id == "foo2"
assert res.api_key == "sk_test_123"

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

url = "/v1/myresources/foo2"
request_mock.stub_request("get", url, {"frobble": 5})
path = "/v1/myresources/foo2"
query_string = "myparam=5"
http_client_mock.stub_request(
"get", path, query_string, '{"frobble": 5}'
)

res = res.refresh()

request_mock.assert_requested("get", url, {"myparam": 5}, None)
http_client_mock.assert_requested(
"get", path=path, query_string=query_string
)
assert res.frobble == 5
with pytest.raises(KeyError):
res["bobble"]

def test_request_with_special_fields_prefers_explicit(self, request_mock):
url = "/v1/myresources/foo"
request_mock.stub_request(
def test_request_with_special_fields_prefers_explicit(
self, http_client_mock
):
path = "/v1/myresources/foo"
query_string = "bobble=scrobble"
http_client_mock.stub_request(
"get",
url,
{"id": "foo2", "bobble": "scrobble"},
path,
query_string,
'{"id": "foo2", "bobble": "scrobble"}',
)

self.MyResource._static_request(
"get",
"/v1/myresources/foo",
path,
idempotency_key="explicit",
params={"idempotency_key": "params", "bobble": "scrobble"},
)

request_mock.assert_requested(
"get", url, {"bobble": "scrobble"}, {"Idempotency-Key": "explicit"}
http_client_mock.assert_requested(
"get",
path=path,
query_string=query_string,
idempotency_key="explicit",
)

def test_convert_to_stripe_object(self):
Expand Down
27 changes: 16 additions & 11 deletions tests/api_resources/abstract/test_createable_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,40 @@ class TestCreateableAPIResource(object):
class MyCreatable(stripe.api_resources.abstract.CreateableAPIResource):
OBJECT_NAME = "mycreatable"

def test_create(self, request_mock):
request_mock.stub_request(
def test_create(self, http_client_mock):
http_client_mock.stub_request(
"post",
"/v1/mycreatables",
{"object": "charge", "foo": "bar"},
path="/v1/mycreatables",
rbody='{"object": "charge", "foo": "bar"}',
rheaders={"request-id": "req_id"},
)

res = self.MyCreatable.create()

request_mock.assert_requested("post", "/v1/mycreatables", {})
http_client_mock.assert_requested(
"post", path="/v1/mycreatables", post_data=""
)
assert isinstance(res, stripe.Charge)
assert res.foo == "bar"

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

def test_idempotent_create(self, request_mock):
request_mock.stub_request(
def test_idempotent_create(self, http_client_mock):
http_client_mock.stub_request(
"post",
"/v1/mycreatables",
{"object": "charge", "foo": "bar"},
path="/v1/mycreatables",
rbody='{"object": "charge", "foo": "bar"}',
rheaders={"idempotency-key": "foo"},
)

res = self.MyCreatable.create(idempotency_key="foo")

request_mock.assert_requested(
"post", "/v1/mycreatables", {}, {"Idempotency-Key": "foo"}
http_client_mock.assert_requested(
"post",
path="/v1/mycreatables",
post_data="",
idempotency_key="foo",
)
assert isinstance(res, stripe.Charge)
assert res.foo == "bar"
Loading

0 comments on commit de0a704

Please sign in to comment.