Skip to content

Commit

Permalink
test: convert Subscription Plan test
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon committed Sep 21, 2020
1 parent 1e57176 commit 735f017
Show file tree
Hide file tree
Showing 18 changed files with 1,184 additions and 29 deletions.
44 changes: 36 additions & 8 deletions getnet/services/plans/period.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
PERIOD_TYPES = ("yearly", "monthly", "bimonthly", "quarterly", "semesterly", "specific")
"""
Implements Plan Period
"""
from enum import Enum, unique
from typing import Union


@unique
class PeriodType(Enum):
"""PeriodType is the enum with Plan Period options"""
YEARLY = "yearly"
MONTHLY = "monthly"
BIMONTHLY = "bimonthly"
QUARTERLY = "quarterly"
SEMESTERLY = "semesterly"
SPECIFIC = "specific"


class Period:
type: str
"""Period represents the Plan Period entity"""
type: PeriodType
billing_cycle: int
specific_cycle_in_days: int

def __init__(
self, type: str, billing_cycle: int, specific_cycle_in_days: int = None
self, type: Union[PeriodType, str], billing_cycle: int, specific_cycle_in_days: int = None
):
if type not in PERIOD_TYPES:
raise TypeError("Invalid Type")

if type == "specific" and specific_cycle_in_days is None:
raise TypeError("'specific_cycle_in_days' required is type is specific")
"""
Args:
type: (PeriodType)
billing_cycle (int):
specific_cycle_in_days (int):
"""
if isinstance(type, str):
try:
type = PeriodType[type.upper()]
except Exception:
raise AttributeError("Invalid Type")

if type == PeriodType.SPECIFIC and specific_cycle_in_days is None:
raise AttributeError("'specific_cycle_in_days' required if type is specific")

self.type = type
self.billing_cycle = billing_cycle
self.specific_cycle_in_days = specific_cycle_in_days

def as_dict(self):
"""Format the data as dict to be sent to Getnet"""
data = self.__dict__.copy()
data["type"] = self.type.value
data.pop("specific_cycle_in_days")

if self.type == "specific":
if self.type == PeriodType.SPECIFIC:
data["specific_cycle_in_days"] = self.specific_cycle_in_days

return data
43 changes: 26 additions & 17 deletions getnet/services/plans/plan.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from typing import Union, List
from enum import Enum, unique
from typing import Union, List, Optional

from getnet.services.plans.period import Period

PRODUCT_TYPES = (
"cash_carry",
"digital_content",
"digital_goods",
"gift_card",
"physical_goods",
"renew_subs",
"shareware",
"service",
)

@unique
class ProductType(Enum):
CASH_CARRY = "cash_carry"
DIGITAL_CONTENT = "digital_content"
DIGITAL_GOODS = "digital_goods"
GIFT_CARD = "gift_card"
PHYSICAL_GOODS = "physical_goods"
RENEW_SUBS = "renew_subs"
SHAREWARE = "shareware"
SERVICE = "service"


class Plan:
Expand All @@ -22,7 +24,7 @@ class Plan:
currency: str
payment_types: List[str] = ("credit_card",)
sales_tax: int = 0
product_type: str
product_type: Optional[Union[ProductType, str]]
period: Period

def __init__(
Expand All @@ -32,11 +34,17 @@ def __init__(
currency: str,
payment_types: List[str] = ("credit_card",),
sales_tax: int = 0,
description: str = None,
product_type: str = None,
seller_id: str = None,
period: Union[Period, dict] = None,
description: Optional[str] = None,
product_type: Optional[ProductType] = None,
seller_id: Optional[str] = None,
period: Optional[Union[Period, dict]] = None,
):
if isinstance(product_type, str):
try:
product_type = ProductType[product_type.upper()]
except Exception:
raise AttributeError("Invalid Product Type")

self.product_type = product_type
self.name = name
self.description = description
Expand All @@ -59,8 +67,9 @@ def __eq__(self, other):

def as_dict(self):
data = self.__dict__.copy()
data["product_type"] = self.product_type.value

period = data.pop("period")
data["period"] = period._as_dict()
data["period"] = period.as_dict()

return data
9 changes: 8 additions & 1 deletion getnet/services/plans/plan_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dateutil import parser

from getnet.services.plans.plan import Plan
from getnet.services.plans.plan import Plan, ProductType


class PlanResponse(Plan):
Expand All @@ -15,6 +15,13 @@ class PlanResponse(Plan):
def __init__(
self, plan_id: Union[UUID, str], status: str, create_date: str = None, **kwargs
):
"""
Args:
plan_id:
status (str):
create_date (str):
**kwargs:
"""
self.plan_id = (
plan_id if isinstance(plan_id, UUID) or plan_id is None else UUID(plan_id)
)
Expand Down
43 changes: 41 additions & 2 deletions getnet/services/plans/service.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
"""
Implements Subscription Plan Service
"""
from typing import Union
from uuid import UUID

from getnet.services.plans.plan import Plan
from getnet.services.plans.plan_response import PlanResponse
from getnet.services.service import Service, ResponseList
from getnet.services.service import Service as BaseService, ResponseList


class Service(Service):
class Service(BaseService):
"""Service implements the Subscription Plan service operations"""

path = "/v1/plans/{plan_id}"

def create(self, plan: Plan) -> PlanResponse:
"""Create the Plan
Args:
plan (Plan): Plan data
"""
plan.seller_id = self._client.seller_id
response = self._post(self._format_url(), json=plan.as_dict())
return PlanResponse(**response)
Expand All @@ -24,6 +34,17 @@ def all(
sort: str = "name",
sort_type: str = "asc",
) -> ResponseList:
"""Return an list of plans
Args:
page (int):
limit (int):
plan_id (str):
status (str):
name (str):
sort (str):
sort_type (str):
"""
if page <= 0:
raise AttributeError("page must be greater then 0")

Expand All @@ -49,13 +70,25 @@ def all(
)

def get(self, plan_id: Union[UUID, str]):
"""Return the specific plan data
Args:
plan_id (UUID, str):
"""
response = self._get(self._format_url(plan_id=str(plan_id)))

return PlanResponse(**response)

def update(
self, plan: Union[Plan, UUID, str], name: str = None, description: str = None
) -> PlanResponse:
"""Update the name and/or description of specific plan
Args:
plan (Plan, UUID, str):
name (str): Optional new plan name
description (str): Optional new plan description
"""
if isinstance(plan, str):
plan_id = UUID(plan)
elif isinstance(plan, Plan):
Expand All @@ -75,6 +108,12 @@ def update(
def update_status(
self, plan: Union[Plan, UUID, str], active: bool = True
) -> PlanResponse:
"""Update the plan status to active or inactive
Args:
plan (Plan, UUID, str):
active (bool): True to active and False to inactive
"""
if isinstance(plan, str):
plan_id = UUID(plan)
elif isinstance(plan, Plan):
Expand Down
2 changes: 1 addition & 1 deletion tests/getnet/services/cards/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from getnet.services.service import ResponseList


@pytest.mark.vcr()
@pytest.mark.vcr
def test_create(client, card_sample: dict):
card_sample["number_token"] = client.generate_token_card(
"5155901222280001", "getnet-py"
Expand Down
67 changes: 67 additions & 0 deletions tests/getnet/services/plans/cassettes/test_all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Bearer 7bf82cb7-b067-4bfe-a223-44a97136af01
Connection:
- keep-alive
Cookie:
- 50d7ab163603b0393a70ef4f24c58ff1=a9e3a93d34c9a041616054c748856805; c4c8a95d3eb7a29ca158fae307fb79ad=ed096d8d7e14e7cfffc3432ed836a77e;
50d7ab163603b0393a70ef4f24c58ff1=a9e3a93d34c9a041616054c748856805; c4c8a95d3eb7a29ca158fae307fb79ad=ed096d8d7e14e7cfffc3432ed836a77e
seller_id:
- d167cde9-1c7f-4af9-a8a2-d38424dad863
user-agent:
- getnet-py/1.1
method: GET
uri: https://api-homologacao.getnet.com.br/v1/plans?page=1&limit=100&status=active&sort=name&sort_type=asc
response:
body:
string: !!binary |
H4sIAAAAAAAAAM2UPY/bMAyG/8pBt0aGvj883tCpQ1F0alEYFCXfCXDswFYODYL899K9pWtuymIQ
9MuX1ANRVzbVY22sl0Ic2AleC4UUTDBvrP91/RcNNbOe5YQ+mGg5mCS5MWrk4EziJaFNmH2SUrMD
g+NynnfDGCNZ4lqglSHThzyUUIKLyJX8IU1vfa9kJ539SXV4Xtcy44VUL9+/UiKXDdd6anWZKUeJ
GY67x5dleYH16VmyfeDLscxtaJdT2efd2+XaBoQ1s9/0v6x1oeGvbFdQ8XGZ29t0odJUp6nOrwNe
cNoPrW4kX5d8xg87Em9lfa9YSLzBVLahwR/W05m2Bu1M7Rhgq++F3Q7/c3IIEjIInnRx3Iw5cRDR
cwExeRWSAy3u42TizslI+SlO6kE5aYtZjc5yW1BzY0fgIGXgyo3FuJQgqHT3fdKqi859ipN+UE4B
hQ9FSR68o73ziu6TNwTLRNTGJBxduYeT7YXureuCv2/vvtFEy9OzlZ0VQTlrtdAiuuAelNsYwUUt
FE9JI+2hzxxGjzyDBxNyyD7f9V59cPOdc+Yz3KLrvDdRK69kMMY80PNFndrSYGK9vf0FtBBd1BEG
AAA=
headers:
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Length:
- '458'
Content-Type:
- application/json;charset=UTF-8
Date:
- Mon, 21 Sep 2020 15:03:59 GMT
Server:
- Apache-Coyote/1.1
Set-Cookie:
- 50d7ab163603b0393a70ef4f24c58ff1=bd25528a5dd8b32a7b5a5c1f2b312b79; Path=/v1;
HttpOnly; Secure; Domain=api-homologacao.getnet.com.br
- c4c8a95d3eb7a29ca158fae307fb79ad=5f1ad24f8e69240ef05f465af4cdb9c1; Path=/v1;
HttpOnly; Secure; Domain=api-homologacao.getnet.com.br
Strict-Transport-Security:
- max-age=15552000; includeSubDomains
Vary:
- Accept-Encoding
X-EdgeConnect-MidMile-RTT:
- '122'
X-EdgeConnect-Origin-MEX-Latency:
- '82'
X-Powered-By:
- '59928'
preload:
- ''
status:
code: 200
message: OK
version: 1
60 changes: 60 additions & 0 deletions tests/getnet/services/plans/cassettes/test_all_not_found.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Bearer 7bf82cb7-b067-4bfe-a223-44a97136af01
Connection:
- keep-alive
Cookie:
- 50d7ab163603b0393a70ef4f24c58ff1=a9e3a93d34c9a041616054c748856805; c4c8a95d3eb7a29ca158fae307fb79ad=ed096d8d7e14e7cfffc3432ed836a77e;
50d7ab163603b0393a70ef4f24c58ff1=bd25528a5dd8b32a7b5a5c1f2b312b79; c4c8a95d3eb7a29ca158fae307fb79ad=5f1ad24f8e69240ef05f465af4cdb9c1
seller_id:
- d167cde9-1c7f-4af9-a8a2-d38424dad863
user-agent:
- getnet-py/1.1
method: GET
uri: https://api-homologacao.getnet.com.br/v1/plans?page=1&limit=100&status=active&name=foobarTest123&sort=name&sort_type=asc
response:
body:
string: !!binary |
H4sIAAAAAAAAAKtWysnMzSxRsjI0MNBRKkhMTwUygYycxLxiJavoWB2lkvySxBwlK4NaAKYPbjsr
AAAA
headers:
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Length:
- '60'
Content-Type:
- application/json;charset=UTF-8
Date:
- Mon, 21 Sep 2020 15:04:00 GMT
Server:
- Apache-Coyote/1.1
Set-Cookie:
- 50d7ab163603b0393a70ef4f24c58ff1=bd25528a5dd8b32a7b5a5c1f2b312b79; Path=/v1;
HttpOnly; Secure; Domain=api-homologacao.getnet.com.br
- c4c8a95d3eb7a29ca158fae307fb79ad=ed096d8d7e14e7cfffc3432ed836a77e; Path=/v1;
HttpOnly; Secure; Domain=api-homologacao.getnet.com.br
Strict-Transport-Security:
- max-age=15552000; includeSubDomains
Vary:
- Accept-Encoding
X-EdgeConnect-MidMile-RTT:
- '132'
X-EdgeConnect-Origin-MEX-Latency:
- '92'
X-Powered-By:
- '59929'
preload:
- ''
status:
code: 200
message: OK
version: 1

0 comments on commit 735f017

Please sign in to comment.