diff options
author | Richard Marmorstein <richardm@stripe.com> | 2024-02-16 13:03:22 -0800 |
---|---|---|
committer | Richard Marmorstein <richardm@stripe.com> | 2024-02-16 13:03:22 -0800 |
commit | 6ab43fa2ac801c14e81b05a9b5989db8c078a9c2 (patch) | |
tree | d96cc5dc247ee8395846af4c7a0c997dce351ac9 | |
parent | 01c9ee56c92c9afa1d741b5dbc55a149530087c1 (diff) | |
parent | aa7d8bf305eb6262919b5237cfebea6491de1b8c (diff) |
Merge changes from stripe/stripe-python master
-rw-r--r-- | tests/api_resources/abstract/test_custom_method.py | 20 | ||||
-rw-r--r-- | tests/api_resources/test_quote.py | 8 | ||||
-rw-r--r-- | tests/conftest.py | 45 | ||||
-rw-r--r-- | tests/http_client_mock.py | 148 | ||||
-rw-r--r-- | tests/services/test_quote.py | 10 | ||||
-rw-r--r-- | tests/test_api_requestor.py | 44 | ||||
-rw-r--r-- | tests/test_generated_examples.py | 10 |
7 files changed, 110 insertions, 175 deletions
diff --git a/tests/api_resources/abstract/test_custom_method.py b/tests/api_resources/abstract/test_custom_method.py index 12142473..01aa8435 100644 --- a/tests/api_resources/abstract/test_custom_method.py +++ b/tests/api_resources/abstract/test_custom_method.py @@ -109,8 +109,8 @@ class TestCustomMethod(object): assert ids == ["cus_1", "cus_2", "cus_3"] - def test_call_custom_stream_method_class(self, http_client_mock_streaming): - http_client_mock_streaming.stub_request( + def test_call_custom_stream_method_class(self, http_client_mock): + http_client_mock.stub_request( "post", path="/v1/myresources/mid/do_the_stream_thing", rbody=util.io.BytesIO(str.encode("response body")), @@ -119,7 +119,7 @@ class TestCustomMethod(object): resp = self.MyResource.do_stream_stuff("mid", foo="bar") - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "post", path="/v1/myresources/mid/do_the_stream_thing", post_data="foo=bar", @@ -150,9 +150,9 @@ class TestCustomMethod(object): assert obj.thing_done is True def test_call_custom_stream_method_class_with_object( - self, http_client_mock_streaming + self, http_client_mock ): - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( "post", path="/v1/myresources/mid/do_the_stream_thing", rbody=util.io.BytesIO(str.encode("response body")), @@ -162,7 +162,7 @@ class TestCustomMethod(object): obj = self.MyResource.construct_from({"id": "mid"}, "mykey") resp = self.MyResource.do_stream_stuff(obj, foo="bar") - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "post", path="/v1/myresources/mid/do_the_stream_thing", post_data="foo=bar", @@ -192,10 +192,8 @@ class TestCustomMethod(object): ) assert obj.thing_done is True - def test_call_custom_stream_method_instance( - self, http_client_mock_streaming - ): - http_client_mock_streaming.stub_request( + def test_call_custom_stream_method_instance(self, http_client_mock): + http_client_mock.stub_request( "post", path="/v1/myresources/mid/do_the_stream_thing", rbody=util.io.BytesIO(str.encode("response body")), @@ -205,7 +203,7 @@ class TestCustomMethod(object): obj = self.MyResource.construct_from({"id": "mid"}, "mykey") resp = obj.do_stream_stuff(foo="bar") - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "post", path="/v1/myresources/mid/do_the_stream_thing", post_data="foo=bar", diff --git a/tests/api_resources/test_quote.py b/tests/api_resources/test_quote.py index 20d04cff..ebf50976 100644 --- a/tests/api_resources/test_quote.py +++ b/tests/api_resources/test_quote.py @@ -140,10 +140,10 @@ class TestQuote(object): ) assert isinstance(resources.data[0], stripe.LineItem) - def test_can_pdf(self, setup_upload_api_base, http_client_mock_streaming): + def test_can_pdf(self, setup_upload_api_base, http_client_mock): resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) stream = resource.pdf() - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "get", api_base=stripe.upload_api_base, path="/v1/quotes/%s/pdf" % TEST_RESOURCE_ID, @@ -152,10 +152,10 @@ class TestQuote(object): assert content == b"Stripe binary response" def test_can_pdf_classmethod( - self, setup_upload_api_base, http_client_mock_streaming + self, setup_upload_api_base, http_client_mock ): stream = stripe.Quote.pdf(TEST_RESOURCE_ID) - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "get", api_base=stripe.upload_api_base, path="/v1/quotes/%s/pdf" % TEST_RESOURCE_ID, diff --git a/tests/conftest.py b/tests/conftest.py index 739d706c..d62924a4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -99,33 +99,6 @@ def http_client_mock(mocker): @pytest.fixture -def http_client_mock_streaming(mocker): - mock_client = HTTPClientMock(mocker, is_streaming=True) - old_client = stripe.default_http_client - stripe.default_http_client = mock_client.get_mock_http_client() - yield mock_client - stripe.default_http_client = old_client - - -@pytest.fixture -def http_client_mock_async(mocker): - mock_client = HTTPClientMock(mocker, is_async=True) - old_client = stripe.default_http_client_async - stripe.default_http_client_async = mock_client.get_mock_http_client() - yield mock_client - stripe.default_http_client_async = old_client - - -@pytest.fixture -def http_client_mock_streaming_async(mocker): - mock_client = HTTPClientMock(mocker, is_streaming=True, is_async=True) - old_client = stripe.default_http_client_async - stripe.default_http_client_async = mock_client.get_mock_http_client() - yield mock_client - stripe.default_http_client_async = old_client - - -@pytest.fixture def stripe_mock_stripe_client(http_client_mock): return StripeClient( MOCK_API_KEY, @@ -141,21 +114,3 @@ def file_stripe_mock_stripe_client(http_client_mock): base_addresses={"files": MOCK_API_BASE}, http_client=http_client_mock.get_mock_http_client(), ) - - -@pytest.fixture -def stripe_mock_stripe_client_streaming(http_client_mock_streaming): - return StripeClient( - MOCK_API_KEY, - base_addresses={"api": MOCK_API_BASE}, - http_client=http_client_mock_streaming.get_mock_http_client(), - ) - - -@pytest.fixture -def file_stripe_mock_stripe_client_streaming(http_client_mock_streaming): - return StripeClient( - MOCK_API_KEY, - base_addresses={"files": MOCK_API_BASE}, - http_client=http_client_mock_streaming.get_mock_http_client(), - ) diff --git a/tests/http_client_mock.py b/tests/http_client_mock.py index 28a69460..d19414c0 100644 --- a/tests/http_client_mock.py +++ b/tests/http_client_mock.py @@ -215,28 +215,19 @@ class StripeRequestCall(object): class HTTPClientMock(object): - def __init__(self, mocker, is_streaming=False, is_async=False): - if is_async: - self.mock_client = mocker.Mock( - wraps=stripe.http_client.new_default_http_client_async() - ) - else: - self.mock_client = mocker.Mock( - wraps=stripe.http_client.new_default_http_client() - ) + def __init__(self, mocker): + self.mock_client = mocker.Mock( + wraps=stripe.http_client.new_default_http_client() + ) - self.is_async = is_async self.mock_client._verify_ssl_certs = True self.mock_client.name = "mockclient" - if is_async and is_streaming: - self.func = self.mock_client.request_stream_with_retries_async - elif is_async and not is_streaming: - self.func = self.mock_client.request_with_retries_async - elif is_streaming: - self.func = self.mock_client.request_stream_with_retries - else: - self.func = self.mock_client.request_with_retries self.registered_responses = {} + self.funcs = [ + self.mock_client.request_with_retries, + self.mock_client.request_stream_with_retries, + ] + self.func_call_order = [] def get_mock_http_client(self) -> Mock: return self.mock_client @@ -250,73 +241,78 @@ class HTTPClientMock(object): rcode=200, rheaders={}, ) -> None: - def custom_side_effect(called_method, called_abs_url, *args, **kwargs): - called_path = urlsplit(called_abs_url).path - called_query = "" - if urlsplit(called_abs_url).query: - called_query = urlencode( - parse_and_sort(urlsplit(called_abs_url).query) - ) - if ( - called_method, - called_path, - called_query, - ) not in self.registered_responses: - raise AssertionError( - "Unexpected request made to %s %s %s" - % (called_method, called_path, called_query) - ) - return self.registered_responses[ - (called_method, called_path, called_query) - ] - - async def awaitable(x): - return x + def custom_side_effect_for_func(func): + def custom_side_effect( + called_method, called_abs_url, *args, **kwargs + ): + self.func_call_order.append(func) + called_path = urlsplit(called_abs_url).path + called_query = "" + if urlsplit(called_abs_url).query: + called_query = urlencode( + parse_and_sort(urlsplit(called_abs_url).query) + ) + if ( + called_method, + called_path, + called_query, + ) not in self.registered_responses: + raise AssertionError( + "Unexpected request made to %s %s %s" + % (called_method, called_path, called_query) + ) + ret = self.registered_responses[ + (called_method, called_path, called_query) + ] + return ret + + return custom_side_effect self.registered_responses[ (method, path, urlencode(parse_and_sort(query_string))) - ] = ( - awaitable( - ( - rbody, - rcode, - rheaders, - ) - ) - if self.is_async - else (rbody, rcode, rheaders) - ) + ] = (rbody, rcode, rheaders) - self.func.side_effect = custom_side_effect + for func in self.funcs: + func.side_effect = custom_side_effect_for_func(func) def get_last_call(self) -> StripeRequestCall: - if not self.func.called: + if len(self.func_call_order) == 0: raise AssertionError( "Expected request to have been made, but no calls were found." ) - return StripeRequestCall.from_mock_call(self.func.call_args) + return StripeRequestCall.from_mock_call( + self.func_call_order[-1].call_args + ) def get_all_calls(self) -> List[StripeRequestCall]: + calls_by_func = { + func: list(func.call_args_list) for func in self.funcs + } + + calls = [] + for func in self.func_call_order: + calls.append(calls_by_func[func].pop(0)) + return [ - StripeRequestCall.from_mock_call(call_args) - for call_args in self.func.call_args_list + StripeRequestCall.from_mock_call(call_args) for call_args in calls ] def find_call( self, method, api_base, path, query_string ) -> StripeRequestCall: - for call_args in self.func.call_args_list: - request_call = StripeRequestCall.from_mock_call(call_args) - try: - if request_call.check( - method=method, - api_base=api_base, - path=path, - query_string=query_string, - ): - return request_call - except AssertionError: - pass + for func in self.funcs: + for call_args in func.call_args_list: + request_call = StripeRequestCall.from_mock_call(call_args) + try: + if request_call.check( + method=method, + api_base=api_base, + path=path, + query_string=query_string, + ): + return request_call + except AssertionError: + pass raise AssertionError( "Expected request to have been made, but no calls were found." ) @@ -374,13 +370,15 @@ class HTTPClientMock(object): ) def assert_no_request(self): - if self.func.called: - msg = ( - "Expected no request to have been made, but %s calls were " - "found." % (self.func.call_count) - ) - raise AssertionError(msg) + for func in self.funcs: + if func.called: + msg = ( + "Expected no request to have been made, but %s calls were " + "found." % (sum([func.call_count for func in self.funcs])) + ) + raise AssertionError(msg) def reset_mock(self): - self.func.reset_mock() + for func in self.funcs: + func.reset_mock() self.registered_responses = {} diff --git a/tests/services/test_quote.py b/tests/services/test_quote.py index 555b7ef5..7b7a1afd 100644 --- a/tests/services/test_quote.py +++ b/tests/services/test_quote.py @@ -105,13 +105,11 @@ class TestQuote(object): def test_can_pdf( self, - file_stripe_mock_stripe_client_streaming, - http_client_mock_streaming, + file_stripe_mock_stripe_client, + http_client_mock, ): - stream = file_stripe_mock_stripe_client_streaming.quotes.pdf( - TEST_RESOURCE_ID - ) - http_client_mock_streaming.assert_requested( + stream = file_stripe_mock_stripe_client.quotes.pdf(TEST_RESOURCE_ID) + http_client_mock.assert_requested( "get", api_base=stripe.upload_api_base, path="/v1/quotes/%s/pdf" % TEST_RESOURCE_ID, diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index a20a0f29..d7cb14fc 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -113,14 +113,6 @@ class TestAPIRequestor(object): ) return requestor - @pytest.fixture - def requestor_streaming(self, http_client_mock_streaming): - requestor_streaming = _APIRequestor( - client=http_client_mock_streaming.get_mock_http_client(), - options=_GlobalRequestorOptions(), - ) - return requestor_streaming - @property def valid_path(self): return "/foo" @@ -337,17 +329,17 @@ class TestAPIRequestor(object): assert b"".join([x async for x in resp.stream()]) == b"thisisdata" def test_empty_methods_streaming_response( - self, requestor_streaming, http_client_mock_streaming + self, requestor, http_client_mock ): for meth in VALID_API_METHODS: - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( meth, path=self.valid_path, rbody=util.io.BytesIO(b"thisisdata"), rcode=200, ) - resp = requestor_streaming.request_stream( + resp = requestor.request_stream( meth, self.valid_path, {}, @@ -360,9 +352,7 @@ class TestAPIRequestor(object): else: post_data = None - http_client_mock_streaming.assert_requested( - meth, post_data=post_data - ) + http_client_mock.assert_requested(meth, post_data=post_data) assert isinstance(resp, StripeStreamResponse) assert resp.io.getvalue() == b"thisisdata" @@ -415,7 +405,7 @@ class TestAPIRequestor(object): http_client_mock.assert_requested(method, abs_url=abs_url) def test_methods_with_params_and_streaming_response( - self, requestor_streaming, http_client_mock_streaming + self, requestor, http_client_mock ): for method in VALID_API_METHODS: encoded = ( @@ -423,7 +413,7 @@ class TestAPIRequestor(object): "alist[0]=1&alist[1]=2&alist[2]=3" ) - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( method, path=self.valid_path, query_string=encoded if method != "post" else "", @@ -437,7 +427,7 @@ class TestAPIRequestor(object): "adatetime": datetime.datetime(2013, 1, 1, tzinfo=GMT1()), } - resp = requestor_streaming.request_stream( + resp = requestor.request_stream( method, self.valid_path, params, @@ -449,18 +439,14 @@ class TestAPIRequestor(object): assert resp.io.getvalue() == b'{"foo": "bar", "baz": 6}' if method == "post": - http_client_mock_streaming.assert_requested( - method, post_data=encoded - ) + http_client_mock.assert_requested(method, post_data=encoded) else: abs_url = "%s%s?%s" % ( stripe.api_base, self.valid_path, encoded, ) - http_client_mock_streaming.assert_requested( - method, abs_url=abs_url - ) + http_client_mock.assert_requested(method, abs_url=abs_url) def test_uses_headers(self, requestor, http_client_mock): http_client_mock.stub_request( @@ -832,9 +818,9 @@ class TestAPIRequestor(object): ) def test_extract_error_from_stream_request_for_bytes( - self, requestor_streaming, http_client_mock_streaming + self, requestor, http_client_mock ): - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( "get", path=self.valid_path, rbody=util.io.BytesIO(b'{"error": "invalid_grant"}'), @@ -842,15 +828,15 @@ class TestAPIRequestor(object): ) with pytest.raises(stripe.oauth_error.InvalidGrantError): - requestor_streaming.request_stream( + requestor.request_stream( "get", self.valid_path, {}, base_address="api", api_mode="V1" ) def test_extract_error_from_stream_request_for_response( - self, requestor_streaming, http_client_mock_streaming + self, requestor, http_client_mock ): # Responses don't have getvalue, they only have a read method. - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( "get", path=self.valid_path, rbody=urllib3.response.HTTPResponse( @@ -860,7 +846,7 @@ class TestAPIRequestor(object): rcode=400, ) with pytest.raises(stripe.oauth_error.InvalidGrantError): - requestor_streaming.request_stream( + requestor.request_stream( "get", self.valid_path, {}, base_address="api", api_mode="V1" ) diff --git a/tests/test_generated_examples.py b/tests/test_generated_examples.py index db95d4b2..a12eb0e5 100644 --- a/tests/test_generated_examples.py +++ b/tests/test_generated_examples.py @@ -17814,26 +17814,26 @@ class TestGeneratedExamples(object): self, http_client_mock_streaming: HTTPClientMock ) -> None: stripe.Quote.pdf("qt_xxxxxxxxxxxxx") - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "get", path="/v1/quotes/qt_xxxxxxxxxxxxx/pdf", query_string="", ) def test_quotes_pdf_get_service( - self, http_client_mock_streaming: HTTPClientMock + self, http_client_mock: HTTPClientMock ) -> None: - http_client_mock_streaming.stub_request( + http_client_mock.stub_request( "get", "/v1/quotes/qt_xxxxxxxxxxxxx/pdf", ) client = StripeClient( "sk_test_123", - http_client=http_client_mock_streaming.get_mock_http_client(), + http_client=http_client_mock.get_mock_http_client(), ) client.quotes.pdf("qt_xxxxxxxxxxxxx") - http_client_mock_streaming.assert_requested( + http_client_mock.assert_requested( "get", path="/v1/quotes/qt_xxxxxxxxxxxxx/pdf", query_string="", |