Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate @test_helper decorator #998

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions stripe/api_resources/abstract/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class TestHelpers(APIResourceTestHelpers):
def __init__(self, resource):
self.resource = resource

@classmethod
def _static_request(cls, *args, **kwargs):
return cls._resource_cls._static_request(*args, **kwargs)

@classmethod
def _static_request_stream(cls, *args, **kwargs):
return cls._resource_cls._static_request_stream(*args, **kwargs)

@classmethod
def class_url(cls):
if cls == APIResourceTestHelpers:
Expand Down Expand Up @@ -48,16 +56,11 @@ def instance_url(self):
return "%s/%s" % (base, extn)


# TODO (next major)
def test_helpers(cls):
"""
test_helpers decorator adds a test_helpers property and
wires the parent resource class to the nested TestHelpers class.

Should only be used on types that inherit from APIResource.

@test_helpers
class Foo(APIResource):
class TestHelpers(APIResourceTestHelpers):
The test_helpers decorator is deprecated and will be removed in a future version
of the library.
"""

def test_helpers_getter(self):
Expand Down
74 changes: 42 additions & 32 deletions stripe/api_resources/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,42 @@
from stripe.api_resources.abstract import UpdateableAPIResource
from stripe.api_resources.abstract import nested_resource_class_methods
from stripe.api_resources.abstract import test_helpers
from typing import Type


class _TestHelpers(APIResourceTestHelpers):
@classmethod
def _cls_fund_cash_balance(
cls,
customer,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/customers/{customer}/fund_cash_balance".format(
customer=util.sanitize_id(customer)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_fund_cash_balance")
def fund_cash_balance(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/customers/{customer}/fund_cash_balance".format(
customer=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)


@test_helpers
@nested_resource_class_methods(
"balance_transaction",
operations=["create", "retrieve", "update", "list"],
Expand Down Expand Up @@ -222,34 +255,11 @@ def modify_cash_balance(
params=params,
)

class TestHelpers(APIResourceTestHelpers):
@classmethod
def _cls_fund_cash_balance(
cls,
customer,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/customers/{customer}/fund_cash_balance".format(
customer=util.sanitize_id(customer)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_fund_cash_balance")
def fund_cash_balance(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/customers/{customer}/fund_cash_balance".format(
customer=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)
TestHelpers = _TestHelpers

@property
def test_helpers(self):
return self.TestHelpers(self)


_TestHelpers._resource_cls = Customer
260 changes: 135 additions & 125 deletions stripe/api_resources/issuing/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,137 +8,147 @@
from stripe.api_resources.abstract import ListableAPIResource
from stripe.api_resources.abstract import UpdateableAPIResource
from stripe.api_resources.abstract import test_helpers
from typing import Type


class _TestHelpers(APIResourceTestHelpers):
@classmethod
def _cls_deliver_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/deliver".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_deliver_card")
def deliver_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/deliver".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_fail_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/fail".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_fail_card")
def fail_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/fail".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_return_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/return".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_return_card")
def return_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/return".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_ship_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/ship".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_ship_card")
def ship_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/ship".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)


@test_helpers
class Card(CreateableAPIResource, ListableAPIResource, UpdateableAPIResource):
"""
You can [create physical or virtual cards](https://stripe.com/docs/issuing/cards) that are issued to cardholders.
"""

OBJECT_NAME = "issuing.card"

class TestHelpers(APIResourceTestHelpers):
@classmethod
def _cls_deliver_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/deliver".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_deliver_card")
def deliver_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/deliver".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_fail_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/fail".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_fail_card")
def fail_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/fail".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_return_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/return".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_return_card")
def return_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/return".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)

@classmethod
def _cls_ship_card(
cls,
card,
api_key=None,
stripe_version=None,
stripe_account=None,
**params
):
return cls._static_request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/ship".format(
card=util.sanitize_id(card)
),
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
params=params,
)

@util.class_method_variant("_cls_ship_card")
def ship_card(self, idempotency_key=None, **params):
return self.resource._request(
"post",
"/v1/test_helpers/issuing/cards/{card}/shipping/ship".format(
card=util.sanitize_id(self.resource.get("id"))
),
idempotency_key=idempotency_key,
params=params,
)
TestHelpers = _TestHelpers

@property
def test_helpers(self):
return self.TestHelpers(self)


_TestHelpers._resource_cls = Card
Loading