From b47e17844cd2559bd5ad52b1c12b8f4471c3a786 Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Fri, 30 Aug 2024 10:59:49 +0200 Subject: [PATCH] Force autoformatting with ruff --- .github/workflows/ruff.yml | 4 + lago_python_client/add_ons/clients.py | 12 +- .../billable_metrics/clients.py | 21 +- lago_python_client/client.py | 10 +- lago_python_client/coupons/clients.py | 33 ++- lago_python_client/credit_notes/clients.py | 46 ++-- lago_python_client/customers/clients.py | 65 +++-- lago_python_client/events/clients.py | 33 ++- lago_python_client/fees/clients.py | 11 +- lago_python_client/functools_ext.py | 15 +- lago_python_client/gross_revenues/clients.py | 10 +- .../invoice_collections/clients.py | 14 +- lago_python_client/invoiced_usages/clients.py | 10 +- lago_python_client/invoices/clients.py | 42 +++- lago_python_client/mixins.py | 73 ++++-- lago_python_client/models/add_on.py | 1 + lago_python_client/models/billable_metric.py | 2 +- lago_python_client/models/customer_usage.py | 2 + lago_python_client/models/event.py | 2 + lago_python_client/models/fee.py | 3 +- lago_python_client/models/gross_revenue.py | 2 +- .../models/invoice_collection.py | 2 +- lago_python_client/models/invoiced_usage.py | 2 +- .../models/minimum_commitment.py | 4 +- lago_python_client/models/mrr.py | 2 +- lago_python_client/models/overdue_balance.py | 4 +- lago_python_client/models/payment_request.py | 3 +- lago_python_client/mrrs/clients.py | 10 +- lago_python_client/organizations/clients.py | 4 +- .../overdue_balances/clients.py | 16 +- .../payment_requests/clients.py | 14 +- lago_python_client/plans/clients.py | 12 +- lago_python_client/services/json.py | 17 +- lago_python_client/services/request.py | 24 +- lago_python_client/services/response.py | 43 +++- lago_python_client/subscriptions/clients.py | 12 +- lago_python_client/taxes/clients.py | 12 +- lago_python_client/version.py | 2 +- lago_python_client/wallets/clients.py | 46 ++-- .../webhook_endpoints/clients.py | 25 +- lago_python_client/webhooks/clients.py | 19 +- tests/test_add_on_client.py | 160 ++++++++----- tests/test_applied_coupon_client.py | 100 +++++--- tests/test_billable_metric_client.py | 169 ++++++++----- tests/test_client.py | 11 +- tests/test_coupon_client.py | 151 ++++++++---- tests/test_credit_note_client.py | 139 ++++++----- tests/test_customer_client.py | 225 ++++++++++-------- tests/test_event_client.py | 118 ++++++--- tests/test_fee_client.py | 20 +- tests/test_functools_ext.py | 4 +- tests/test_gross_revenue_client.py | 23 +- tests/test_invoice_client.py | 219 +++++++++++------ tests/test_invoice_collection_client.py | 25 +- tests/test_invoiced_usage_client.py | 23 +- tests/test_json_services.py | 7 +- tests/test_mrr_client.py | 21 +- tests/test_organization_client.py | 39 +-- tests/test_overdue_balance_client.py | 25 +- tests/test_payment_request_client.py | 48 ++-- tests/test_request_services.py | 39 ++- tests/test_response_services.py | 71 +++--- tests/test_subscription_client.py | 204 ++++++++++------ tests/test_tax_client.py | 147 ++++++++---- tests/test_wallet_client.py | 167 ++++++++----- tests/test_wallet_transaction_client.py | 57 +++-- tests/test_webhook_client.py | 25 +- tests/test_webhook_endpoint_client.py | 132 ++++++---- 68 files changed, 1981 insertions(+), 1072 deletions(-) diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 6978afa7..af75a3f3 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -12,5 +12,9 @@ jobs: - name: Install dependencies run: | python -m pip install .[test] + - name: Validate linter rules with Ruff + uses: chartboost/ruff-action@v1 - name: Validate formatting with Ruff uses: chartboost/ruff-action@v1 + with: + args: 'format --check' diff --git a/lago_python_client/add_ons/clients.py b/lago_python_client/add_ons/clients.py index 0ef4d168..800f07ca 100644 --- a/lago_python_client/add_ons/clients.py +++ b/lago_python_client/add_ons/clients.py @@ -1,7 +1,13 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.add_on import AddOnResponse @@ -13,6 +19,6 @@ class AddOnClient( UpdateCommandMixin[AddOnResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'add_ons' + API_RESOURCE: ClassVar[str] = "add_ons" RESPONSE_MODEL: ClassVar[Type[AddOnResponse]] = AddOnResponse - ROOT_NAME: ClassVar[str] = 'add_on' + ROOT_NAME: ClassVar[str] = "add_on" diff --git a/lago_python_client/billable_metrics/clients.py b/lago_python_client/billable_metrics/clients.py index 184024bb..59a0298b 100644 --- a/lago_python_client/billable_metrics/clients.py +++ b/lago_python_client/billable_metrics/clients.py @@ -1,16 +1,15 @@ import sys -from typing import Any, ClassVar, Type, Union +from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.billable_metric import BillableMetricResponse -from ..services.request import make_headers, make_url, send_get_request -from ..services.response import get_response_data, prepare_index_response, Response - -if sys.version_info >= (3, 9): - from collections.abc import Mapping -else: - from typing import Mapping class BillableMetricClient( @@ -21,6 +20,6 @@ class BillableMetricClient( UpdateCommandMixin[BillableMetricResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'billable_metrics' + API_RESOURCE: ClassVar[str] = "billable_metrics" RESPONSE_MODEL: ClassVar[Type[BillableMetricResponse]] = BillableMetricResponse - ROOT_NAME: ClassVar[str] = 'billable_metric' + ROOT_NAME: ClassVar[str] = "billable_metric" diff --git a/lago_python_client/client.py b/lago_python_client/client.py index 864eeaef..0c9b509d 100644 --- a/lago_python_client/client.py +++ b/lago_python_client/client.py @@ -30,16 +30,18 @@ class Client: - BASE_URL: Final[str] = 'https://api.getlago.com/' - API_PATH: Final[str] = 'api/v1/' + BASE_URL: Final[str] = "https://api.getlago.com/" + API_PATH: Final[str] = "api/v1/" - def __init__(self, api_key: str = '', api_url: str = '') -> None: + def __init__(self, api_key: str = "", api_url: str = "") -> None: self.api_key: str = api_key self.api_url: str = api_url @property def base_api_url(self) -> str: - return urljoin(self.api_url if self.api_url else Client.BASE_URL, Client.API_PATH) + return urljoin( + self.api_url if self.api_url else Client.BASE_URL, Client.API_PATH + ) @callable_cached_property def add_ons(self) -> AddOnClient: diff --git a/lago_python_client/coupons/clients.py b/lago_python_client/coupons/clients.py index 2c9cb0b9..f39eb99f 100644 --- a/lago_python_client/coupons/clients.py +++ b/lago_python_client/coupons/clients.py @@ -1,7 +1,13 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.coupon import CouponResponse from ..models.applied_coupon import AppliedCouponResponse from ..services.request import make_headers, make_url, send_delete_request @@ -16,21 +22,32 @@ class CouponClient( UpdateCommandMixin[CouponResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'coupons' + API_RESOURCE: ClassVar[str] = "coupons" RESPONSE_MODEL: ClassVar[Type[CouponResponse]] = CouponResponse - ROOT_NAME: ClassVar[str] = 'coupon' + ROOT_NAME: ClassVar[str] = "coupon" -class AppliedCouponClient(CreateCommandMixin[AppliedCouponResponse], FindAllCommandMixin[AppliedCouponResponse], BaseClient): - API_RESOURCE: ClassVar[str] = 'applied_coupons' +class AppliedCouponClient( + CreateCommandMixin[AppliedCouponResponse], + FindAllCommandMixin[AppliedCouponResponse], + BaseClient, +): + API_RESOURCE: ClassVar[str] = "applied_coupons" RESPONSE_MODEL: ClassVar[Type[AppliedCouponResponse]] = AppliedCouponResponse - ROOT_NAME: ClassVar[str] = 'applied_coupon' + ROOT_NAME: ClassVar[str] = "applied_coupon" - def destroy(self, external_customer_id: str, applied_coupon_id: str) -> AppliedCouponResponse: + def destroy( + self, external_customer_id: str, applied_coupon_id: str + ) -> AppliedCouponResponse: api_response: Response = send_delete_request( url=make_url( origin=self.base_url, - path_parts=('customers', external_customer_id, self.API_RESOURCE, applied_coupon_id), + path_parts=( + "customers", + external_customer_id, + self.API_RESOURCE, + applied_coupon_id, + ), ), headers=make_headers(api_key=self.api_key), ) diff --git a/lago_python_client/credit_notes/clients.py b/lago_python_client/credit_notes/clients.py index 4a195111..c0373687 100644 --- a/lago_python_client/credit_notes/clients.py +++ b/lago_python_client/credit_notes/clients.py @@ -1,10 +1,24 @@ -from typing import ClassVar, Optional, Type, Union +from typing import ClassVar, Optional, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin -from ..models.credit_note import CreditNoteResponse, CreditNoteEstimatedResponse, CreditNoteEstimate +from ..mixins import ( + CreateCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) +from ..models.credit_note import ( + CreditNoteResponse, + CreditNoteEstimatedResponse, + CreditNoteEstimate, +) from ..services.json import to_json -from ..services.request import make_headers, make_url, send_post_request, send_put_request +from ..services.request import ( + make_headers, + make_url, + send_post_request, + send_put_request, +) from ..services.response import get_response_data, prepare_object_response, Response @@ -15,16 +29,16 @@ class CreditNoteClient( UpdateCommandMixin[CreditNoteResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'credit_notes' - ESTIMATE_API_RESOURCE: ClassVar[str] = 'estimated_credit_note' + API_RESOURCE: ClassVar[str] = "credit_notes" + ESTIMATE_API_RESOURCE: ClassVar[str] = "estimated_credit_note" RESPONSE_MODEL: ClassVar[Type[CreditNoteResponse]] = CreditNoteResponse - ROOT_NAME: ClassVar[str] = 'credit_note' + ROOT_NAME: ClassVar[str] = "credit_note" def download(self, resource_id: str) -> Optional[CreditNoteResponse]: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'download'), + path_parts=(self.API_RESOURCE, resource_id, "download"), ), headers=make_headers(api_key=self.api_key), ) @@ -42,7 +56,7 @@ def void(self, resource_id: str) -> CreditNoteResponse: api_response: Response = send_put_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'void'), + path_parts=(self.API_RESOURCE, resource_id, "void"), ), headers=make_headers(api_key=self.api_key), ) @@ -56,15 +70,19 @@ def estimate(self, input_object: CreditNoteEstimate) -> CreditNoteEstimatedRespo api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, 'estimate'), + path_parts=(self.API_RESOURCE, "estimate"), + ), + content=to_json( + { + self.ROOT_NAME: input_object.dict(), + } ), - content=to_json({ - self.ROOT_NAME: input_object.dict(), - }), headers=make_headers(api_key=self.api_key), ) return prepare_object_response( response_model=CreditNoteEstimatedResponse, - data=get_response_data(response=api_response, key=self.ESTIMATE_API_RESOURCE), + data=get_response_data( + response=api_response, key=self.ESTIMATE_API_RESOURCE + ), ) diff --git a/lago_python_client/customers/clients.py b/lago_python_client/customers/clients.py index a84fe319..15afc331 100644 --- a/lago_python_client/customers/clients.py +++ b/lago_python_client/customers/clients.py @@ -2,11 +2,26 @@ from typing import Any, Mapping, ClassVar, Type, Union from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, +) from ..models.customer import CustomerResponse from ..models.customer_usage import CustomerUsageResponse -from ..services.request import make_headers, make_url, send_get_request, send_post_request -from ..services.response import get_response_data, prepare_index_response, prepare_object_response, Response +from ..services.request import ( + make_headers, + make_url, + send_get_request, + send_post_request, +) +from ..services.response import ( + get_response_data, + prepare_index_response, + prepare_object_response, + Response, +) if sys.version_info >= (3, 9): from collections.abc import Mapping @@ -21,17 +36,19 @@ class CustomerClient( FindCommandMixin[CustomerResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'customers' + API_RESOURCE: ClassVar[str] = "customers" RESPONSE_MODEL: ClassVar[Type[CustomerResponse]] = CustomerResponse - ROOT_NAME: ClassVar[str] = 'customer' + ROOT_NAME: ClassVar[str] = "customer" - def current_usage(self, resource_id: str, external_subscription_id: str) -> CustomerUsageResponse: + def current_usage( + self, resource_id: str, external_subscription_id: str + ) -> CustomerUsageResponse: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'current_usage'), + path_parts=(self.API_RESOURCE, resource_id, "current_usage"), query_pairs={ - 'external_subscription_id': external_subscription_id, + "external_subscription_id": external_subscription_id, }, ), headers=make_headers(api_key=self.api_key), @@ -39,16 +56,21 @@ def current_usage(self, resource_id: str, external_subscription_id: str) -> Cust return prepare_object_response( response_model=CustomerUsageResponse, - data=get_response_data(response=api_response, key='customer_usage'), + data=get_response_data(response=api_response, key="customer_usage"), ) - def past_usage(self, resource_id: str, external_subscription_id: str, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def past_usage( + self, + resource_id: str, + external_subscription_id: str, + options: Mapping[str, Union[int, str]] = {}, + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'past_usage'), + path_parts=(self.API_RESOURCE, resource_id, "past_usage"), query_pairs={ - 'external_subscription_id': external_subscription_id, + "external_subscription_id": external_subscription_id, **options, }, ), @@ -56,32 +78,39 @@ def past_usage(self, resource_id: str, external_subscription_id: str, options: M ) return prepare_index_response( - api_resource='usage_periods', + api_resource="usage_periods", response_model=CustomerUsageResponse, data=get_response_data(response=api_response), ) - def portal_url(self, resource_id: str) -> str: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'portal_url'), + path_parts=(self.API_RESOURCE, resource_id, "portal_url"), ), headers=make_headers(api_key=self.api_key), ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) - return response_data.get('portal_url', '') if isinstance(response_data, Mapping) else '' + return ( + response_data.get("portal_url", "") + if isinstance(response_data, Mapping) + else "" + ) def checkout_url(self, resource_id: str) -> str: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'checkout_url'), + path_parts=(self.API_RESOURCE, resource_id, "checkout_url"), ), headers=make_headers(api_key=self.api_key), ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) - return response_data.get('checkout_url', '') if isinstance(response_data, Mapping) else '' \ No newline at end of file + return ( + response_data.get("checkout_url", "") + if isinstance(response_data, Mapping) + else "" + ) diff --git a/lago_python_client/events/clients.py b/lago_python_client/events/clients.py index d3f43da5..ae5221db 100644 --- a/lago_python_client/events/clients.py +++ b/lago_python_client/events/clients.py @@ -1,5 +1,5 @@ import sys -from typing import Any, ClassVar, Optional, Type +from typing import Any, ClassVar, Type from lago_python_client.base_model import BaseModel @@ -10,7 +10,12 @@ from ..models.fee import FeeResponse from ..services.json import to_json from ..services.request import make_headers, make_url, send_post_request -from ..services.response import get_response_data, prepare_object_list_response, verify_response, Response +from ..services.response import ( + get_response_data, + prepare_object_list_response, + verify_response, + Response, +) if sys.version_info >= (3, 9): from collections.abc import Mapping @@ -18,16 +23,18 @@ from typing import Mapping -class EventClient(CreateCommandMixin[EventResponse], FindCommandMixin[EventResponse], BaseClient): - API_RESOURCE: ClassVar[str] = 'events' +class EventClient( + CreateCommandMixin[EventResponse], FindCommandMixin[EventResponse], BaseClient +): + API_RESOURCE: ClassVar[str] = "events" RESPONSE_MODEL: ClassVar[Type[EventResponse]] = EventResponse - ROOT_NAME: ClassVar[str] = 'event' + ROOT_NAME: ClassVar[str] = "event" def batch_create(self, input_object: BaseModel) -> None: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, 'batch'), + path_parts=(self.API_RESOURCE, "batch"), ), content=to_json(input_object.dict()), headers=make_headers(api_key=self.api_key), @@ -38,18 +45,20 @@ def batch_create(self, input_object: BaseModel) -> None: def estimate_fees(self, input_object: BaseModel) -> Mapping[str, Any]: api_response: Response = send_post_request( - url= make_url( + url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, 'estimate_fees'), + path_parts=(self.API_RESOURCE, "estimate_fees"), + ), + content=to_json( + { + self.ROOT_NAME: input_object.dict(), + } ), - content=to_json({ - self.ROOT_NAME: input_object.dict(), - }), headers=make_headers(api_key=self.api_key), ) return prepare_object_list_response( api_resource=FeeClient.API_RESOURCE, response_model=FeeResponse, - data=get_response_data(response=api_response, key='fees'), + data=get_response_data(response=api_response, key="fees"), ) diff --git a/lago_python_client/fees/clients.py b/lago_python_client/fees/clients.py index d2342894..5c762606 100644 --- a/lago_python_client/fees/clients.py +++ b/lago_python_client/fees/clients.py @@ -1,7 +1,12 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import FindCommandMixin, FindAllCommandMixin, UpdateCommandMixin, DestroyCommandMixin +from ..mixins import ( + FindCommandMixin, + FindAllCommandMixin, + UpdateCommandMixin, + DestroyCommandMixin, +) from ..models.fee import FeeResponse @@ -12,6 +17,6 @@ class FeeClient( DestroyCommandMixin[FeeResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'fees' + API_RESOURCE: ClassVar[str] = "fees" RESPONSE_MODEL: ClassVar[Type[FeeResponse]] = FeeResponse - ROOT_NAME: ClassVar[str] = 'fee' + ROOT_NAME: ClassVar[str] = "fee" diff --git a/lago_python_client/functools_ext.py b/lago_python_client/functools_ext.py index d5b4deae..aa11b162 100644 --- a/lago_python_client/functools_ext.py +++ b/lago_python_client/functools_ext.py @@ -1,6 +1,7 @@ import sys from typing import Any, TypeVar import warnings + try: from functools import cached_property except ImportError: @@ -30,11 +31,13 @@ def __setattr__(self, name, value) -> None: def __call__(self) -> Any: warnings.warn( - ''.join(( - 'We are going to deprecate callable properties (`client.()`) in future. ', - 'Please, remove braces. ', - 'Use `client..(...)` instead of `client.().(...)`', - )), + "".join( + ( + "We are going to deprecate callable properties (`client.()`) in future. ", + "Please, remove braces. ", + "Use `client..(...)` instead of `client.().(...)`", + ) + ), PendingDeprecationWarning, ) return self._obj @@ -53,8 +56,10 @@ def __hash__(self) -> int: if sys.version_info >= (3, 9): + def callable_cached_property(func: Callable[P, T]) -> cached_property[T]: return cached_property(lambda s: Proxy(func(s))) # type: ignore else: + def callable_cached_property(func): return cached_property(lambda s: Proxy(func(s))) diff --git a/lago_python_client/gross_revenues/clients.py b/lago_python_client/gross_revenues/clients.py index 0b134983..d20c26df 100644 --- a/lago_python_client/gross_revenues/clients.py +++ b/lago_python_client/gross_revenues/clients.py @@ -17,15 +17,17 @@ class GrossRevenueClient( FindAllCommandMixin[GrossRevenueResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'gross_revenues' + API_RESOURCE: ClassVar[str] = "gross_revenues" RESPONSE_MODEL: ClassVar[Type[GrossRevenueResponse]] = GrossRevenueResponse - ROOT_NAME: ClassVar[str] = 'gross_revenue' + ROOT_NAME: ClassVar[str] = "gross_revenue" - def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, options: Mapping[str, Union[int, str]] = {} + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('analytics', 'gross_revenue'), + path_parts=("analytics", "gross_revenue"), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/invoice_collections/clients.py b/lago_python_client/invoice_collections/clients.py index dc974d6f..dbe2d587 100644 --- a/lago_python_client/invoice_collections/clients.py +++ b/lago_python_client/invoice_collections/clients.py @@ -17,15 +17,19 @@ class InvoiceCollectionClient( FindAllCommandMixin[InvoiceCollectionResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'invoice_collections' - RESPONSE_MODEL: ClassVar[Type[InvoiceCollectionResponse]] = InvoiceCollectionResponse - ROOT_NAME: ClassVar[str] = 'invoice_collection' + API_RESOURCE: ClassVar[str] = "invoice_collections" + RESPONSE_MODEL: ClassVar[Type[InvoiceCollectionResponse]] = ( + InvoiceCollectionResponse + ) + ROOT_NAME: ClassVar[str] = "invoice_collection" - def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, options: Mapping[str, Union[int, str]] = {} + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('analytics', 'invoice_collection'), + path_parts=("analytics", "invoice_collection"), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/invoiced_usages/clients.py b/lago_python_client/invoiced_usages/clients.py index c0694385..86c6544d 100644 --- a/lago_python_client/invoiced_usages/clients.py +++ b/lago_python_client/invoiced_usages/clients.py @@ -17,15 +17,17 @@ class InvoicedUsageClient( FindAllCommandMixin[InvoicedUsageResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'invoiced_usages' + API_RESOURCE: ClassVar[str] = "invoiced_usages" RESPONSE_MODEL: ClassVar[Type[InvoicedUsageResponse]] = InvoicedUsageResponse - ROOT_NAME: ClassVar[str] = 'invoiced_usage' + ROOT_NAME: ClassVar[str] = "invoiced_usage" - def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, options: Mapping[str, Union[int, str]] = {} + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('analytics', 'invoiced_usage'), + path_parts=("analytics", "invoiced_usage"), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/invoices/clients.py b/lago_python_client/invoices/clients.py index 671ec23b..9ba66880 100644 --- a/lago_python_client/invoices/clients.py +++ b/lago_python_client/invoices/clients.py @@ -1,9 +1,19 @@ -from typing import ClassVar, Optional, Type, Union, Mapping +from typing import ClassVar, Optional, Type, Mapping from ..base_client import BaseClient -from ..mixins import FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin, CreateCommandMixin +from ..mixins import ( + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, + CreateCommandMixin, +) from ..models.invoice import InvoiceResponse -from ..services.request import make_headers, make_url, send_post_request, send_put_request +from ..services.request import ( + make_headers, + make_url, + send_post_request, + send_put_request, +) from ..services.response import get_response_data, prepare_object_response, Response @@ -14,15 +24,15 @@ class InvoiceClient( CreateCommandMixin[InvoiceResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'invoices' + API_RESOURCE: ClassVar[str] = "invoices" RESPONSE_MODEL: ClassVar[Type[InvoiceResponse]] = InvoiceResponse - ROOT_NAME: ClassVar[str] = 'invoice' + ROOT_NAME: ClassVar[str] = "invoice" def download(self, resource_id: str) -> Optional[InvoiceResponse]: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'download'), + path_parts=(self.API_RESOURCE, resource_id, "download"), ), headers=make_headers(api_key=self.api_key), ) @@ -40,7 +50,7 @@ def retry_payment(self, resource_id: str) -> Optional[InvoiceResponse]: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'retry_payment'), + path_parts=(self.API_RESOURCE, resource_id, "retry_payment"), ), headers=make_headers(api_key=self.api_key), ) @@ -58,7 +68,7 @@ def refresh(self, resource_id: str) -> InvoiceResponse: api_response: Response = send_put_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'refresh'), + path_parts=(self.API_RESOURCE, resource_id, "refresh"), ), headers=make_headers(api_key=self.api_key), ) @@ -72,7 +82,7 @@ def finalize(self, resource_id: str) -> InvoiceResponse: api_response: Response = send_put_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'finalize'), + path_parts=(self.API_RESOURCE, resource_id, "finalize"), ), headers=make_headers(api_key=self.api_key), ) @@ -86,7 +96,7 @@ def lose_dispute(self, resource_id: str) -> InvoiceResponse: api_response: Response = send_put_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'lose_dispute'), + path_parts=(self.API_RESOURCE, resource_id, "lose_dispute"), ), headers=make_headers(api_key=self.api_key), ) @@ -100,10 +110,16 @@ def payment_url(self, resource_id: str) -> str: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, resource_id, 'payment_url'), + path_parts=(self.API_RESOURCE, resource_id, "payment_url"), ), headers=make_headers(api_key=self.api_key), ) - response_data = get_response_data(response=api_response, key='invoice_payment_details') - return response_data.get('payment_url', '') if isinstance(response_data, Mapping) else '' + response_data = get_response_data( + response=api_response, key="invoice_payment_details" + ) + return ( + response_data.get("payment_url", "") + if isinstance(response_data, Mapping) + else "" + ) diff --git a/lago_python_client/mixins.py b/lago_python_client/mixins.py index 898fa002..2e2e76f3 100644 --- a/lago_python_client/mixins.py +++ b/lago_python_client/mixins.py @@ -11,8 +11,20 @@ from lago_python_client.base_model import BaseModel from .services.json import to_json -from .services.request import make_headers, make_url, send_delete_request, send_get_request, send_post_request, send_put_request -from .services.response import get_response_data, prepare_index_response, prepare_object_response, Response +from .services.request import ( + make_headers, + make_url, + send_delete_request, + send_get_request, + send_post_request, + send_put_request, +) +from .services.response import ( + get_response_data, + prepare_index_response, + prepare_object_response, + Response, +) if sys.version_info >= (3, 9): from collections.abc import Mapping @@ -39,17 +51,23 @@ def base_url(self) -> str: ... class CreateCommandMixin(Generic[_M]): """Client mixin with `create` command.""" - def create(self: _ClientMixin[_M], input_object: BaseModel, timeout: Optional[httpx.Timeout] = None) -> Optional[_M]: + def create( + self: _ClientMixin[_M], + input_object: BaseModel, + timeout: Optional[httpx.Timeout] = None, + ) -> Optional[_M]: """Execute `create` command.""" # Send request and save response api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, ), + path_parts=(self.API_RESOURCE,), + ), + content=to_json( + { + self.ROOT_NAME: input_object.dict(), + } ), - content=to_json({ - self.ROOT_NAME: input_object.dict(), - }), headers=make_headers(api_key=self.api_key), timeout=timeout, ) @@ -68,7 +86,12 @@ def create(self: _ClientMixin[_M], input_object: BaseModel, timeout: Optional[ht class DestroyCommandMixin(Generic[_M]): """Client mixin with `destroy` command.""" - def destroy(self: _ClientMixin[_M], resource_id: str, options: Mapping[str, Union[int, str]] = {}, timeout: Optional[httpx.Timeout] = None) -> BaseModel: + def destroy( + self: _ClientMixin[_M], + resource_id: str, + options: Mapping[str, Union[int, str]] = {}, + timeout: Optional[httpx.Timeout] = None, + ) -> BaseModel: """Execute `destroy` command.""" # Send request and save response api_response: Response = send_delete_request( @@ -91,13 +114,17 @@ def destroy(self: _ClientMixin[_M], resource_id: str, options: Mapping[str, Unio class FindAllCommandMixin(Generic[_M]): """Client mixin with `find_all` command.""" - def find_all(self: _ClientMixin[_M], options: Mapping[str, Union[int, str]] = {}, timeout: Optional[httpx.Timeout] = None) -> Mapping[str, Any]: + def find_all( + self: _ClientMixin[_M], + options: Mapping[str, Union[int, str]] = {}, + timeout: Optional[httpx.Timeout] = None, + ) -> Mapping[str, Any]: """Execute `find all` command.""" # Send request and save response api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, ), + path_parts=(self.API_RESOURCE,), query_pairs=options, ), headers=make_headers(api_key=self.api_key), @@ -115,7 +142,12 @@ def find_all(self: _ClientMixin[_M], options: Mapping[str, Union[int, str]] = {} class FindCommandMixin(Generic[_M]): """Client mixin with `find` command.""" - def find(self: _ClientMixin[_M], resource_id: str, params: Mapping[str, str] = {}, timeout: Optional[httpx.Timeout] = None) -> _M: + def find( + self: _ClientMixin[_M], + resource_id: str, + params: Mapping[str, str] = {}, + timeout: Optional[httpx.Timeout] = None, + ) -> _M: """Execute `find` command.""" # Send request and save response api_response: Response = send_get_request( @@ -138,17 +170,26 @@ def find(self: _ClientMixin[_M], resource_id: str, params: Mapping[str, str] = { class UpdateCommandMixin(Generic[_M]): """Client mixin with `update` command.""" - def update(self: _ClientMixin[_M], input_object: BaseModel, identifier: Optional[str] = None, timeout: Optional[httpx.Timeout] = None) -> _M: + def update( + self: _ClientMixin[_M], + input_object: BaseModel, + identifier: Optional[str] = None, + timeout: Optional[httpx.Timeout] = None, + ) -> _M: """Execute `update` command.""" # Send request and save response api_response: Response = send_put_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, identifier) if identifier else (self.API_RESOURCE, ), + path_parts=(self.API_RESOURCE, identifier) + if identifier + else (self.API_RESOURCE,), + ), + content=to_json( + { + self.ROOT_NAME: input_object.dict(exclude_none=True), + } ), - content=to_json({ - self.ROOT_NAME: input_object.dict(exclude_none=True), - }), headers=make_headers(api_key=self.api_key), timeout=timeout, ) diff --git a/lago_python_client/models/add_on.py b/lago_python_client/models/add_on.py index 738b189a..e35e093d 100644 --- a/lago_python_client/models/add_on.py +++ b/lago_python_client/models/add_on.py @@ -6,6 +6,7 @@ from .tax import TaxesResponse + class AddOn(BaseModel): name: Optional[str] invoice_display_name: Optional[str] diff --git a/lago_python_client/models/billable_metric.py b/lago_python_client/models/billable_metric.py index 74e624ec..b76bc9eb 100644 --- a/lago_python_client/models/billable_metric.py +++ b/lago_python_client/models/billable_metric.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Optional, Union +from typing import List, Optional from lago_python_client.base_model import BaseModel diff --git a/lago_python_client/models/customer_usage.py b/lago_python_client/models/customer_usage.py index b87fe3d9..6ba0096d 100644 --- a/lago_python_client/models/customer_usage.py +++ b/lago_python_client/models/customer_usage.py @@ -25,6 +25,7 @@ class ChargeObject(BaseModel): charge_model: str invoice_display_name: Optional[str] + class GroupedUsage(BaseModel): amount_cents: int events_count: int @@ -32,6 +33,7 @@ class GroupedUsage(BaseModel): grouped_by: Dict[str, str] filters: List[ChargeFilterUsage] + class ChargeUsage(BaseModel): units: float events_count: int diff --git a/lago_python_client/models/event.py b/lago_python_client/models/event.py index 08c84b91..f01374e3 100644 --- a/lago_python_client/models/event.py +++ b/lago_python_client/models/event.py @@ -16,6 +16,7 @@ class Event(BaseModel): class BatchEvent(BaseModel): events: List[Event] + class EventResponse(BaseResponseModel): lago_id: str transaction_id: str @@ -27,5 +28,6 @@ class EventResponse(BaseResponseModel): properties: Optional[Dict[str, Any]] created_at: str + class BatchEventResponse(BaseResponseModel): events: List[EventResponse] diff --git a/lago_python_client/models/fee.py b/lago_python_client/models/fee.py index 28eae8f2..f7901bf7 100644 --- a/lago_python_client/models/fee.py +++ b/lago_python_client/models/fee.py @@ -3,6 +3,7 @@ from .invoice_item import InvoiceItemResponse from ..base_model import BaseModel, BaseResponseModel + class Fee(BaseModel): payment_status: Optional[str] invoice_display_name: Optional[str] @@ -37,7 +38,7 @@ class FeeResponse(BaseResponseModel): taxes_amount_cents: Optional[int] taxes_rate: Optional[float] total_amount_cents: Optional[int] - unit_amount_cents: Optional[int] # deprecated + unit_amount_cents: Optional[int] # deprecated precise_unit_amount: Optional[str] total_amount_currency: Optional[str] units: Optional[float] diff --git a/lago_python_client/models/gross_revenue.py b/lago_python_client/models/gross_revenue.py index b7508c61..3be6d5a3 100644 --- a/lago_python_client/models/gross_revenue.py +++ b/lago_python_client/models/gross_revenue.py @@ -1,6 +1,6 @@ from typing import List, Optional -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel class GrossRevenueResponse(BaseResponseModel): diff --git a/lago_python_client/models/invoice_collection.py b/lago_python_client/models/invoice_collection.py index eb50be07..5c5ed48d 100644 --- a/lago_python_client/models/invoice_collection.py +++ b/lago_python_client/models/invoice_collection.py @@ -1,6 +1,6 @@ from typing import List, Optional -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel class InvoiceCollectionResponse(BaseResponseModel): diff --git a/lago_python_client/models/invoiced_usage.py b/lago_python_client/models/invoiced_usage.py index 95d861e5..9e52798e 100644 --- a/lago_python_client/models/invoiced_usage.py +++ b/lago_python_client/models/invoiced_usage.py @@ -1,6 +1,6 @@ from typing import List, Optional -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel class InvoicedUsageResponse(BaseResponseModel): diff --git a/lago_python_client/models/minimum_commitment.py b/lago_python_client/models/minimum_commitment.py index 409a498e..69ac50b0 100644 --- a/lago_python_client/models/minimum_commitment.py +++ b/lago_python_client/models/minimum_commitment.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Optional +from typing import List, Optional from lago_python_client.base_model import BaseModel @@ -6,12 +6,12 @@ from ..base_model import BaseResponseModel - class MinimumCommitment(BaseModel): amount_cents: Optional[int] invoice_display_name: Optional[str] tax_codes: Optional[List[str]] + class MinimumCommitmentResponse(BaseResponseModel): lago_id: str amount_cents: int diff --git a/lago_python_client/models/mrr.py b/lago_python_client/models/mrr.py index dad632c3..7a04822d 100644 --- a/lago_python_client/models/mrr.py +++ b/lago_python_client/models/mrr.py @@ -1,6 +1,6 @@ from typing import List, Optional -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel class MrrResponse(BaseResponseModel): diff --git a/lago_python_client/models/overdue_balance.py b/lago_python_client/models/overdue_balance.py index 942bc371..81ecb64f 100644 --- a/lago_python_client/models/overdue_balance.py +++ b/lago_python_client/models/overdue_balance.py @@ -1,6 +1,6 @@ -from typing import List, Optional +from typing import List -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel class OverdueBalanceResponse(BaseResponseModel): diff --git a/lago_python_client/models/payment_request.py b/lago_python_client/models/payment_request.py index 2bd67469..e79198f7 100644 --- a/lago_python_client/models/payment_request.py +++ b/lago_python_client/models/payment_request.py @@ -1,8 +1,9 @@ from typing import List, Optional -from ..base_model import BaseModel, BaseResponseModel +from ..base_model import BaseResponseModel from .customer import CustomerResponse + class PaymentRequestInvoiceResponse(BaseResponseModel): lago_id: str sequential_id: Optional[int] diff --git a/lago_python_client/mrrs/clients.py b/lago_python_client/mrrs/clients.py index 2652aaa5..8184d7dd 100644 --- a/lago_python_client/mrrs/clients.py +++ b/lago_python_client/mrrs/clients.py @@ -17,15 +17,17 @@ class MrrClient( FindAllCommandMixin[MrrResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'mrrs' + API_RESOURCE: ClassVar[str] = "mrrs" RESPONSE_MODEL: ClassVar[Type[MrrResponse]] = MrrResponse - ROOT_NAME: ClassVar[str] = 'mrr' + ROOT_NAME: ClassVar[str] = "mrr" - def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, options: Mapping[str, Union[int, str]] = {} + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('analytics', 'mrr'), + path_parts=("analytics", "mrr"), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/organizations/clients.py b/lago_python_client/organizations/clients.py index 0c8c2752..89720265 100644 --- a/lago_python_client/organizations/clients.py +++ b/lago_python_client/organizations/clients.py @@ -6,6 +6,6 @@ class OrganizationClient(UpdateCommandMixin[OrganizationResponse], BaseClient): - API_RESOURCE: ClassVar[str] = 'organizations' + API_RESOURCE: ClassVar[str] = "organizations" RESPONSE_MODEL: ClassVar[Type[OrganizationResponse]] = OrganizationResponse - ROOT_NAME: ClassVar[str] = 'organization' + ROOT_NAME: ClassVar[str] = "organization" diff --git a/lago_python_client/overdue_balances/clients.py b/lago_python_client/overdue_balances/clients.py index 73f863d0..436d751b 100644 --- a/lago_python_client/overdue_balances/clients.py +++ b/lago_python_client/overdue_balances/clients.py @@ -1,5 +1,7 @@ import sys -from typing import Any, ClassVar, Type, Union +from typing import Any, ClassVar, Type, Union, Optional + +import httpx from ..base_client import BaseClient from ..mixins import FindAllCommandMixin @@ -17,15 +19,19 @@ class OverdueBalanceClient( FindAllCommandMixin[OverdueBalanceResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'overdue_balances' + API_RESOURCE: ClassVar[str] = "overdue_balances" RESPONSE_MODEL: ClassVar[Type[OverdueBalanceResponse]] = OverdueBalanceResponse - ROOT_NAME: ClassVar[str] = 'overdue_balance' + ROOT_NAME: ClassVar[str] = "overdue_balance" - def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, + options: Mapping[str, Union[int, str]] = {}, + timeout: Optional[httpx.Timeout] = None, + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('analytics', 'overdue_balance'), + path_parts=("analytics", "overdue_balance"), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/payment_requests/clients.py b/lago_python_client/payment_requests/clients.py index 0eb2e73d..cb8bf779 100644 --- a/lago_python_client/payment_requests/clients.py +++ b/lago_python_client/payment_requests/clients.py @@ -1,22 +1,14 @@ -import sys -from typing import Any, ClassVar, Type, Union +from typing import ClassVar, Type from ..base_client import BaseClient from ..mixins import FindAllCommandMixin from ..models.payment_request import PaymentRequestResponse -from ..services.request import make_headers, make_url, send_get_request -from ..services.response import get_response_data, prepare_index_response, Response - -if sys.version_info >= (3, 9): - from collections.abc import Mapping -else: - from typing import Mapping class PaymentRequestClient( FindAllCommandMixin[PaymentRequestResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'payment_requests' + API_RESOURCE: ClassVar[str] = "payment_requests" RESPONSE_MODEL: ClassVar[Type[PaymentRequestResponse]] = PaymentRequestResponse - ROOT_NAME: ClassVar[str] = 'payment_request' + ROOT_NAME: ClassVar[str] = "payment_request" diff --git a/lago_python_client/plans/clients.py b/lago_python_client/plans/clients.py index ba53a4d6..16a053e7 100644 --- a/lago_python_client/plans/clients.py +++ b/lago_python_client/plans/clients.py @@ -1,7 +1,13 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.plan import PlanResponse @@ -13,6 +19,6 @@ class PlanClient( UpdateCommandMixin[PlanResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'plans' + API_RESOURCE: ClassVar[str] = "plans" RESPONSE_MODEL: ClassVar[Type[PlanResponse]] = PlanResponse - ROOT_NAME: ClassVar[str] = 'plan' + ROOT_NAME: ClassVar[str] = "plan" diff --git a/lago_python_client/services/json.py b/lago_python_client/services/json.py index 3f832341..165f514e 100644 --- a/lago_python_client/services/json.py +++ b/lago_python_client/services/json.py @@ -16,7 +16,18 @@ from typing import Mapping, Sequence Serializable = Union[ - str, Mapping[Any, Any], Sequence[Any], Tuple[Any], int, float, bool, datetime, date, time, UUID, None, + str, + Mapping[Any, Any], + Sequence[Any], + Tuple[Any], + int, + float, + bool, + datetime, + date, + time, + UUID, + None, ] # And dataclass, TypedDict and ndarray Deserializable = Union[bytes, bytearray, memoryview, str] DeserializedData = Union[Mapping[str, Any], Sequence[Any], int, float, str, bool, None] @@ -30,7 +41,7 @@ def to_json(data_container: Serializable) -> bytes: @typeclass # type: ignore def from_json(json_container) -> DeserializedData: """Deserialize data from json format.""" - raise TypeError('Type {0} is not supported'.format(type(json_container))) + raise TypeError("Type {0} is not supported".format(type(json_container))) @from_json.instance(bytes) # type: ignore @@ -58,7 +69,7 @@ def _from_json_none(json_container: None) -> NoReturn: status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 url=None, response=None, - detail='Input must be bytes, bytearray, memoryview, or str', + detail="Input must be bytes, bytearray, memoryview, or str", headers=None, ) diff --git a/lago_python_client/services/request.py b/lago_python_client/services/request.py index c3ad5ef2..ed6ef8c9 100644 --- a/lago_python_client/services/request.py +++ b/lago_python_client/services/request.py @@ -1,5 +1,6 @@ import sys from typing import Union + try: from typing import Final except ImportError: # Python 3.7 @@ -15,19 +16,26 @@ else: from typing import Mapping, Sequence -URI_TEMPLATE: Final[str] = '{uri_path}{uri_query}' -QUERY_TEMPLATE: Final[str] = '?{query}' +URI_TEMPLATE: Final[str] = "{uri_path}{uri_query}" +QUERY_TEMPLATE: Final[str] = "?{query}" -def make_url(*, origin: str, path_parts: Sequence[str], query_pairs: Mapping[str, Union[int, str]] = {}) -> str: +def make_url( + *, + origin: str, + path_parts: Sequence[str], + query_pairs: Mapping[str, Union[int, str]] = {}, +) -> str: """Return url.""" return urljoin( origin, URI_TEMPLATE.format( - uri_path='/'.join(path_parts), + uri_path="/".join(path_parts), uri_query=QUERY_TEMPLATE.format( query=urlencode(query_pairs), - ) if query_pairs else '', + ) + if query_pairs + else "", ), ) @@ -35,9 +43,9 @@ def make_url(*, origin: str, path_parts: Sequence[str], query_pairs: Mapping[str def make_headers(*, api_key: str) -> Mapping[str, str]: """Return headers.""" return { - 'Content-type': 'application/json', - 'Authorization': "Bearer {api_key}".format(api_key=api_key), - 'User-agent': 'Lago Python v{version}'.format(version=LAGO_VERSION), + "Content-type": "application/json", + "Authorization": "Bearer {api_key}".format(api_key=api_key), + "User-agent": "Lago Python v{version}".format(version=LAGO_VERSION), } diff --git a/lago_python_client/services/response.py b/lago_python_client/services/response.py index 59b438bf..30292293 100644 --- a/lago_python_client/services/response.py +++ b/lago_python_client/services/response.py @@ -1,6 +1,7 @@ from http import HTTPStatus import sys from typing import Any, Optional, Set, Type, TypeVar, Union + try: from typing import assert_never except ImportError: # Python 3.7, 3.8, 3.9, 3.10 @@ -54,7 +55,7 @@ def verify_response(response: Response) -> Optional[Response]: status_code=response.status_code, url=str(response.request.url), response=response_data, - detail=getattr(response_data, 'error', None), + detail=getattr(response_data, "error", None), headers=response.headers, ) @@ -64,7 +65,9 @@ def verify_response(response: Response) -> Optional[Response]: return response -def get_response_data(*, response: Response, key: str = '') -> Optional[_MappingOrSequence]: +def get_response_data( + *, response: Response, key: str = "" +) -> Optional[_MappingOrSequence]: """Return verified and unpacked response data.""" response_or_None: Optional[Response] = verify_response(response) if not response_or_None: @@ -73,7 +76,9 @@ def get_response_data(*, response: Response, key: str = '') -> Optional[_Mapping # Ensure deserialized_data has correct type: sequence or mapping or raise LagoApiError try: - mapping_or_sequence_data = typeguard.check_type(deserialized_data, _MappingOrSequence) + mapping_or_sequence_data = typeguard.check_type( + deserialized_data, _MappingOrSequence + ) except typeguard.TypeCheckError as exc: raise LagoApiError( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 @@ -84,33 +89,43 @@ def get_response_data(*, response: Response, key: str = '') -> Optional[_Mapping ) if isinstance(mapping_or_sequence_data, Mapping): - return mapping_or_sequence_data.get(key, {}) if key else mapping_or_sequence_data + return ( + mapping_or_sequence_data.get(key, {}) if key else mapping_or_sequence_data + ) elif isinstance(mapping_or_sequence_data, Sequence): return mapping_or_sequence_data else: assert_never(mapping_or_sequence_data) -def prepare_object_response(response_model: Type[_M], data: Optional[_MappingOrSequence]) -> _M: +def prepare_object_response( + response_model: Type[_M], data: Optional[_MappingOrSequence] +) -> _M: """Return single object response - Pydantic model instance with provided data.""" if not data: raise LagoApiError( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 url=None, response=None, - detail='Data is required to create instance of {model}'.format(model=response_model), + detail="Data is required to create instance of {model}".format( + model=response_model + ), headers=None, ) return response_model.parse_obj(data) -def prepare_index_response(api_resource: str, response_model: Type[_M], data: Optional[_MappingOrSequence]) -> Mapping[str, Any]: +def prepare_index_response( + api_resource: str, response_model: Type[_M], data: Optional[_MappingOrSequence] +) -> Mapping[str, Any]: """Return index response with meta based on mapping data object.""" # Ensure deserialized_data has correct type: mapping with mapping or sequence inside or raise LagoApiError try: - response_data: Mapping[str, _MappingOrSequence] = typeguard.check_type(data, Mapping[str, _MappingOrSequence]) + response_data: Mapping[str, _MappingOrSequence] = typeguard.check_type( + data, Mapping[str, _MappingOrSequence] + ) except typeguard.TypeCheckError as exc: raise LagoApiError( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 @@ -125,20 +140,22 @@ def prepare_index_response(api_resource: str, response_model: Type[_M], data: Op prepare_object_response(response_model=response_model, data=el) for el in response_data[api_resource] ], - 'meta': response_data['meta'], + "meta": response_data["meta"], } def prepare_object_list_response( - api_resource: str, - response_model: Type[_M], - data: Optional[Union[Mapping[str, object], Sequence[object]]], + api_resource: str, + response_model: Type[_M], + data: Optional[Union[Mapping[str, object], Sequence[object]]], ) -> Mapping[str, Any]: """Return response based on sequence of data objects.""" # The only usage - ``WalletTransactionClient.create`` # Ensure deserialized_data has correct type: sequence with mapping or sequence inside or raise LagoApiError try: - response_data: Sequence[_MappingOrSequence] = typeguard.check_type(data, Sequence[_MappingOrSequence]) + response_data: Sequence[_MappingOrSequence] = typeguard.check_type( + data, Sequence[_MappingOrSequence] + ) except typeguard.TypeCheckError as exc: raise LagoApiError( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 diff --git a/lago_python_client/subscriptions/clients.py b/lago_python_client/subscriptions/clients.py index d4071d0b..7e9a34dc 100644 --- a/lago_python_client/subscriptions/clients.py +++ b/lago_python_client/subscriptions/clients.py @@ -1,7 +1,13 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.subscription import SubscriptionResponse @@ -13,6 +19,6 @@ class SubscriptionClient( UpdateCommandMixin[SubscriptionResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'subscriptions' + API_RESOURCE: ClassVar[str] = "subscriptions" RESPONSE_MODEL: ClassVar[Type[SubscriptionResponse]] = SubscriptionResponse - ROOT_NAME: ClassVar[str] = 'subscription' + ROOT_NAME: ClassVar[str] = "subscription" diff --git a/lago_python_client/taxes/clients.py b/lago_python_client/taxes/clients.py index be45a450..4fc1b691 100644 --- a/lago_python_client/taxes/clients.py +++ b/lago_python_client/taxes/clients.py @@ -1,7 +1,13 @@ from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.tax import TaxResponse @@ -13,6 +19,6 @@ class TaxClient( UpdateCommandMixin[TaxResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'taxes' + API_RESOURCE: ClassVar[str] = "taxes" RESPONSE_MODEL: ClassVar[Type[TaxResponse]] = TaxResponse - ROOT_NAME: ClassVar[str] = 'tax' + ROOT_NAME: ClassVar[str] = "tax" diff --git a/lago_python_client/version.py b/lago_python_client/version.py index f7b4450a..52de6a1a 100644 --- a/lago_python_client/version.py +++ b/lago_python_client/version.py @@ -1 +1 @@ -LAGO_VERSION = '1.9.0' +LAGO_VERSION = "1.9.0" diff --git a/lago_python_client/wallets/clients.py b/lago_python_client/wallets/clients.py index b896f64a..776f8f94 100644 --- a/lago_python_client/wallets/clients.py +++ b/lago_python_client/wallets/clients.py @@ -4,12 +4,28 @@ from lago_python_client.base_model import BaseModel from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.wallet import WalletResponse from ..models.wallet_transaction import WalletTransactionResponse from ..services.json import to_json -from ..services.request import make_headers, make_url, send_get_request, send_post_request -from ..services.response import get_response_data, prepare_object_list_response, prepare_index_response, Response +from ..services.request import ( + make_headers, + make_url, + send_get_request, + send_post_request, +) +from ..services.response import ( + get_response_data, + prepare_object_list_response, + prepare_index_response, + Response, +) if sys.version_info >= (3, 9): from collections.abc import Mapping @@ -25,25 +41,25 @@ class WalletClient( UpdateCommandMixin[WalletResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'wallets' + API_RESOURCE: ClassVar[str] = "wallets" RESPONSE_MODEL: ClassVar[Type[WalletResponse]] = WalletResponse - ROOT_NAME: ClassVar[str] = 'wallet' + ROOT_NAME: ClassVar[str] = "wallet" class WalletTransactionClient(BaseClient): - API_RESOURCE: ClassVar[str] = 'wallet_transactions' - RESPONSE_MODEL: ClassVar[Type[WalletTransactionResponse]] = WalletTransactionResponse - ROOT_NAME: ClassVar[str] = 'wallet_transactions' + API_RESOURCE: ClassVar[str] = "wallet_transactions" + RESPONSE_MODEL: ClassVar[Type[WalletTransactionResponse]] = ( + WalletTransactionResponse + ) + ROOT_NAME: ClassVar[str] = "wallet_transactions" def create(self, input_object: BaseModel) -> Mapping[str, Any]: api_response: Response = send_post_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, ), + path_parts=(self.API_RESOURCE,), ), - content=to_json({ - 'wallet_transaction': input_object.dict() - }), + content=to_json({"wallet_transaction": input_object.dict()}), headers=make_headers(api_key=self.api_key), ) @@ -53,11 +69,13 @@ def create(self, input_object: BaseModel) -> Mapping[str, Any]: data=get_response_data(response=api_response, key=self.ROOT_NAME), ) - def find_all(self, wallet_id: str, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]: + def find_all( + self, wallet_id: str, options: Mapping[str, Union[int, str]] = {} + ) -> Mapping[str, Any]: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=('wallets', wallet_id, self.API_RESOURCE), + path_parts=("wallets", wallet_id, self.API_RESOURCE), query_pairs=options, ), headers=make_headers(api_key=self.api_key), diff --git a/lago_python_client/webhook_endpoints/clients.py b/lago_python_client/webhook_endpoints/clients.py index 95f076af..74d4a2cb 100644 --- a/lago_python_client/webhook_endpoints/clients.py +++ b/lago_python_client/webhook_endpoints/clients.py @@ -1,19 +1,14 @@ -import sys -from typing import Any, ClassVar, Type, Union - -from lago_python_client.base_model import BaseModel +from typing import ClassVar, Type from ..base_client import BaseClient -from ..mixins import CreateCommandMixin, DestroyCommandMixin, FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin +from ..mixins import ( + CreateCommandMixin, + DestroyCommandMixin, + FindAllCommandMixin, + FindCommandMixin, + UpdateCommandMixin, +) from ..models.webhook_endpoint import WebhookEndpointResponse -from ..services.json import to_json -from ..services.request import make_headers, make_url, send_get_request, send_post_request -from ..services.response import get_response_data, prepare_object_list_response, prepare_index_response, Response - -if sys.version_info >= (3, 9): - from collections.abc import Mapping -else: - from typing import Mapping class WebhookEndpointClient( @@ -24,6 +19,6 @@ class WebhookEndpointClient( UpdateCommandMixin[WebhookEndpointResponse], BaseClient, ): - API_RESOURCE: ClassVar[str] = 'webhook_endpoints' + API_RESOURCE: ClassVar[str] = "webhook_endpoints" RESPONSE_MODEL: ClassVar[Type[WebhookEndpointResponse]] = WebhookEndpointResponse - ROOT_NAME: ClassVar[str] = 'webhook_endpoint' + ROOT_NAME: ClassVar[str] = "webhook_endpoint" diff --git a/lago_python_client/webhooks/clients.py b/lago_python_client/webhooks/clients.py index 243e684c..1aef2373 100644 --- a/lago_python_client/webhooks/clients.py +++ b/lago_python_client/webhooks/clients.py @@ -2,6 +2,7 @@ from http import HTTPStatus import sys from typing import Any, ClassVar, Optional, Type, Union + try: from typing import TypedDict except ImportError: @@ -28,29 +29,33 @@ class _ResponseWithPublicKeyInside(TypedDict): class WebhookClient(BaseClient): - API_RESOURCE: ClassVar[str] = 'webhooks' + API_RESOURCE: ClassVar[str] = "webhooks" RESPONSE_MODEL: ClassVar[Type[BaseModel]] = NotImplemented - ROOT_NAME: ClassVar[str] = 'webhook' + ROOT_NAME: ClassVar[str] = "webhook" def public_key(self) -> bytes: api_response: Response = send_get_request( url=make_url( origin=self.base_url, - path_parts=(self.API_RESOURCE, 'json_public_key'), + path_parts=(self.API_RESOURCE, "json_public_key"), ), headers=make_headers(api_key=self.api_key), ) - response_data: Optional[Union[Mapping[str, Any], Sequence[Any]]] = get_response_data(response=api_response, key=self.ROOT_NAME) + response_data: Optional[Union[Mapping[str, Any], Sequence[Any]]] = ( + get_response_data(response=api_response, key=self.ROOT_NAME) + ) try: - checked_response_data: _ResponseWithPublicKeyInside = typeguard.check_type(response_data, _ResponseWithPublicKeyInside) # type: ignore + checked_response_data: _ResponseWithPublicKeyInside = typeguard.check_type( + response_data, _ResponseWithPublicKeyInside + ) # type: ignore except typeguard.TypeCheckError: raise LagoApiError( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500 url=None, response=None, - detail='Incorrect response data', + detail="Incorrect response data", headers=None, ) - return base64.b64decode(checked_response_data['public_key']) + return base64.b64decode(checked_response_data["public_key"]) diff --git a/tests/test_add_on_client.py b/tests/test_add_on_client.py index 6914e50e..354bad20 100644 --- a/tests/test_add_on_client.py +++ b/tests/test_add_on_client.py @@ -10,143 +10,191 @@ def add_on_object(): return AddOn( - name='name', - invoice_display_name='add_on_invoice_display_name', - code='add_on_first', + name="name", + invoice_display_name="add_on_invoice_display_name", + code="add_on_first", amount_cents=1000, - amount_currency='EUR', - description='desc' + amount_currency="EUR", + description="desc", ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/add_on.json') + data_path = os.path.join(this_dir, "fixtures/add_on.json") - with open(data_path, 'rb') as add_on_response: + with open(data_path, "rb") as add_on_response: return add_on_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/add_on_index.json') + data_path = os.path.join(this_dir, "fixtures/add_on_index.json") - with open(data_path, 'rb') as add_on_response: + with open(data_path, "rb") as add_on_response: return add_on_response.read() def test_valid_create_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/add_ons', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/add_ons", + content=mock_response(), + ) response = client.add_ons.create(add_on_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.code == 'add_on_code' - assert response.invoice_display_name == 'add_on_invoice_display_name' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.code == "add_on_code" + assert response.invoice_display_name == "add_on_invoice_display_name" def test_invalid_create_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/add_ons', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/add_ons", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.add_ons.create(add_on_object()) def test_valid_update_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'add_on_code' - invoice_display_name = 'add_on_invoice_display_name' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/add_ons/' + code, content=mock_response()) + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "add_on_code" + invoice_display_name = "add_on_invoice_display_name" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/add_ons/" + code, + content=mock_response(), + ) response = client.add_ons.update(add_on_object(), code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code assert response.invoice_display_name == invoice_display_name def test_invalid_update_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/add_ons/' + code, status_code=401, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/add_ons/" + code, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.add_ons.update(add_on_object(), code) def test_valid_find_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'add_on_code' - invoice_display_name = 'add_on_invoice_display_name' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/add_ons/' + code, content=mock_response()) + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "add_on_code" + invoice_display_name = "add_on_invoice_display_name" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/add_ons/" + code, + content=mock_response(), + ) response = client.add_ons.find(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code assert response.invoice_display_name == invoice_display_name - def test_invalid_find_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/add_ons/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/add_ons/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.add_ons.find(code) def test_valid_destroy_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'add_on_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "add_on_code" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/add_ons/' + code, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/add_ons/" + code, + content=mock_response(), + ) response = client.add_ons.destroy(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_destroy_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/add_ons/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/add_ons/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.add_ons.destroy(code) def test_valid_find_all_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/add_ons', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/add_ons", + content=mock_collection_response(), + ) response = client.add_ons.find_all() - assert response['add_ons'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert response["add_ons"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1111" + assert response["meta"]["current_page"] == 1 def test_valid_find_all_add_on_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/add_ons?per_page=2&page=1', content=mock_collection_response()) - response = client.add_ons.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/add_ons?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.add_ons.find_all({"per_page": 2, "page": 1}) - assert response['add_ons'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1222' - assert response['meta']['current_page'] == 1 + assert response["add_ons"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1222" + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_add_on_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/add_ons', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/add_ons", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.add_ons.find_all() diff --git a/tests/test_applied_coupon_client.py b/tests/test_applied_coupon_client.py index e5573d1f..ca21c010 100644 --- a/tests/test_applied_coupon_client.py +++ b/tests/test_applied_coupon_client.py @@ -10,99 +10,131 @@ def create_applied_coupon(): return AppliedCoupon( - external_customer_id='5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', - coupon_code='Free-Lemon-Juice' + external_customer_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + coupon_code="Free-Lemon-Juice", ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/applied_coupon.json') + data_path = os.path.join(this_dir, "fixtures/applied_coupon.json") - with open(data_path, 'rb') as applied_coupon_response: + with open(data_path, "rb") as applied_coupon_response: return applied_coupon_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/applied_coupon_index.json') + data_path = os.path.join(this_dir, "fixtures/applied_coupon_index.json") - with open(data_path, 'rb') as applied_coupon_response: + with open(data_path, "rb") as applied_coupon_response: return applied_coupon_response.read() def test_valid_create_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/applied_coupons', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/applied_coupons", + content=mock_response(), + ) response = client.applied_coupons.create(create_applied_coupon()) - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_invalid_create_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/applied_coupons', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/applied_coupons", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.applied_coupons.create(create_applied_coupon()) def test_valid_find_all_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/applied_coupons', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/applied_coupons", + content=mock_collection_response(), + ) response = client.applied_coupons.find_all() - assert response['applied_coupons'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response['meta']['current_page'] == 1 + assert ( + response["applied_coupons"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + ) + assert response["meta"]["current_page"] == 1 def test_valid_find_all_applied_coupon_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/applied_coupons?per_page=2&page=1', content=mock_collection_response()) - response = client.applied_coupons.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/applied_coupons?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.applied_coupons.find_all({"per_page": 2, "page": 1}) - assert response['applied_coupons'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac2222' - assert response['meta']['current_page'] == 1 + assert ( + response["applied_coupons"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac2222" + ) + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/applied_coupons', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/applied_coupons", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.applied_coupons.find_all() def test_valid_destroy_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - external_customer_id = 'external_customer_id' - applied_coupon_id = '_ID_' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + external_customer_id = "external_customer_id" + applied_coupon_id = "_ID_" httpx_mock.add_response( - method='DELETE', - url='https://api.getlago.com/api/v1/customers/' + external_customer_id + '/applied_coupons/' + applied_coupon_id, + method="DELETE", + url="https://api.getlago.com/api/v1/customers/" + + external_customer_id + + "/applied_coupons/" + + applied_coupon_id, content=mock_response(), ) response = client.applied_coupons.destroy(external_customer_id, applied_coupon_id) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_destroy_applied_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - external_customer_id = 'external_customer_id' - applied_coupon_id = '_ID_' + client = Client(api_key="invalid") + external_customer_id = "external_customer_id" + applied_coupon_id = "_ID_" httpx_mock.add_response( - method='DELETE', - url='https://api.getlago.com/api/v1/customers/' + external_customer_id + '/applied_coupons/' + applied_coupon_id, + method="DELETE", + url="https://api.getlago.com/api/v1/customers/" + + external_customer_id + + "/applied_coupons/" + + applied_coupon_id, status_code=404, - content=b'', + content=b"", ) with pytest.raises(LagoApiError): client.applied_coupons.destroy(external_customer_id, applied_coupon_id) diff --git a/tests/test_billable_metric_client.py b/tests/test_billable_metric_client.py index 03a512be..e08eed97 100644 --- a/tests/test_billable_metric_client.py +++ b/tests/test_billable_metric_client.py @@ -5,149 +5,212 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import BillableMetric, BillableMetricFilter, BillableMetricFilters +from lago_python_client.models import ( + BillableMetric, + BillableMetricFilter, + BillableMetricFilters, +) def billable_metric_object(): return BillableMetric( - name='name', - code='code_first', - description='desc', - aggregation_type='sum_agg', - field_name='amount_sum', + name="name", + code="code_first", + description="desc", + aggregation_type="sum_agg", + field_name="amount_sum", recurring=False, filters=filters(), - weighted_interval='seconds' + weighted_interval="seconds", ) def filters(): - return BillableMetricFilters(__root__=[BillableMetricFilter(key='country', values=['france', 'italy', 'spain'])]) + return BillableMetricFilters( + __root__=[ + BillableMetricFilter(key="country", values=["france", "italy", "spain"]) + ] + ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/billable_metric.json') + data_path = os.path.join(this_dir, "fixtures/billable_metric.json") - with open(data_path, 'rb') as billable_metric_response: + with open(data_path, "rb") as billable_metric_response: return billable_metric_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/billable_metric_index.json') + data_path = os.path.join(this_dir, "fixtures/billable_metric_index.json") - with open(data_path, 'rb') as billable_metric_response: + with open(data_path, "rb") as billable_metric_response: return billable_metric_response.read() def test_valid_create_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/billable_metrics', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/billable_metrics", + content=mock_response(), + ) response = client.billable_metrics.create(billable_metric_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.code == 'bm_code' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.code == "bm_code" assert response.filters == filters() def test_invalid_create_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/billable_metrics', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/billable_metrics", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.billable_metrics.create(billable_metric_object()) def test_valid_update_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'bm_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "bm_code" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/billable_metrics/' + code, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + content=mock_response(), + ) response = client.billable_metrics.update(billable_metric_object(), code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_update_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/billable_metrics/' + code, status_code=401, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.billable_metrics.update(billable_metric_object(), code) def test_valid_find_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'bm_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "bm_code" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/billable_metrics/' + code, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + content=mock_response(), + ) response = client.billable_metrics.find(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_find_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/billable_metrics/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.billable_metrics.find(code) def test_valid_destroy_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'bm_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "bm_code" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/billable_metrics/' + code, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + content=mock_response(), + ) response = client.billable_metrics.destroy(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_destroy_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/billable_metrics/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/billable_metrics/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.billable_metrics.destroy(code) def test_valid_find_all_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/billable_metrics', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/billable_metrics", + content=mock_collection_response(), + ) response = client.billable_metrics.find_all() - assert response['billable_metrics'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1000' - assert response['meta']['current_page'] == 1 + assert ( + response["billable_metrics"][0].lago_id + == "b7ab2926-1de8-4428-9bcd-779314ac1000" + ) + assert response["meta"]["current_page"] == 1 def test_valid_find_all_billable_metric_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/billable_metrics?per_page=2&page=1', content=mock_collection_response()) - response = client.billable_metrics.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/billable_metrics?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.billable_metrics.find_all({"per_page": 2, "page": 1}) - assert response['billable_metrics'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314a11111' - assert response['meta']['current_page'] == 1 + assert ( + response["billable_metrics"][1].lago_id + == "b7ab2926-1de8-4428-9bcd-779314a11111" + ) + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_billable_metric_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/billable_metrics', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/billable_metrics", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.billable_metrics.find_all() diff --git a/tests/test_client.py b/tests/test_client.py index 4713209e..c674311f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -2,13 +2,14 @@ def test_base_url_when_api_url_is_given(): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d', - api_url='https://example.com/') + client = Client( + api_key="886fe239-927d-4072-ab72-6dd345e8dd0d", api_url="https://example.com/" + ) - assert client.base_api_url == 'https://example.com/api/v1/' + assert client.base_api_url == "https://example.com/api/v1/" def test_base_url_when_api_url_is_not_given(): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - assert client.base_api_url == 'https://api.getlago.com/api/v1/' + assert client.base_api_url == "https://api.getlago.com/api/v1/" diff --git a/tests/test_coupon_client.py b/tests/test_coupon_client.py index efbc3bd0..56ff127c 100644 --- a/tests/test_coupon_client.py +++ b/tests/test_coupon_client.py @@ -10,143 +10,192 @@ def coupon_object(): return Coupon( - name='name', - code='code_first', + name="name", + code="code_first", description="coupon_description", amount_cents=1000, - amount_currency='EUR', - expiration='no_expiration', + amount_currency="EUR", + expiration="no_expiration", expiration_at="2022-08-08T23:59:59Z", coupon_type="fixed_amount", reusable=False, frequency="once", - applies_to=LimitationConfiguration(plan_codes=['plan1']) + applies_to=LimitationConfiguration(plan_codes=["plan1"]), ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/coupon.json') + data_path = os.path.join(this_dir, "fixtures/coupon.json") - with open(data_path, 'rb') as coupon_response: + with open(data_path, "rb") as coupon_response: return coupon_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/coupon_index.json') + data_path = os.path.join(this_dir, "fixtures/coupon_index.json") - with open(data_path, 'rb') as coupon_response: + with open(data_path, "rb") as coupon_response: return coupon_response.read() def test_valid_create_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/coupons', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/coupons", + content=mock_response(), + ) response = client.coupons.create(coupon_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.code == 'coupon_code' - assert response.description == 'coupon_description' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.code == "coupon_code" + assert response.description == "coupon_description" def test_invalid_create_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/coupons', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/coupons", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.coupons.create(coupon_object()) def test_valid_update_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'coupon_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "coupon_code" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/coupons/' + code, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/coupons/" + code, + content=mock_response(), + ) response = client.coupons.update(coupon_object(), code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_update_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/coupons/' + code, status_code=401, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/coupons/" + code, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.coupons.update(coupon_object(), code) def test_valid_find_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'coupon_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "coupon_code" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/coupons/' + code, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/coupons/" + code, + content=mock_response(), + ) response = client.coupons.find(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_find_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/coupons/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/coupons/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.coupons.find(code) def test_valid_destroy_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'coupon_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "coupon_code" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/coupons/' + code, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/coupons/" + code, + content=mock_response(), + ) response = client.coupons.destroy(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_destroy_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/coupons/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/coupons/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.coupons.destroy(code) def test_valid_find_all_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/coupons', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/coupons", + content=mock_collection_response(), + ) response = client.coupons.find_all() - assert response['coupons'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert response["coupons"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1111" + assert response["meta"]["current_page"] == 1 def test_valid_find_all_coupon_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/coupons?per_page=2&page=1', content=mock_collection_response()) - response = client.coupons.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/coupons?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.coupons.find_all({"per_page": 2, "page": 1}) - assert response['coupons'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1222' - assert response['meta']['current_page'] == 1 + assert response["coupons"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1222" + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_coupon_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/coupons', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/coupons", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.coupons.find_all() diff --git a/tests/test_credit_note_client.py b/tests/test_credit_note_client.py index 29b1cc6d..0dacb132 100644 --- a/tests/test_credit_note_client.py +++ b/tests/test_credit_note_client.py @@ -5,30 +5,27 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.fee import FeeResponse -from lago_python_client.models.credit_note import Item, Items, CreditNote, CreditNoteUpdate, CreditNoteEstimate +from lago_python_client.models.credit_note import ( + Item, + Items, + CreditNote, + CreditNoteUpdate, + CreditNoteEstimate, +) def credit_note_object(): - item1 = Item( - fee_id="fee_id_1", - amount_cents=10 - ) + item1 = Item(fee_id="fee_id_1", amount_cents=10) - item2 = Item( - fee="fee_id_2", - amount_cents=5 - ) + item2 = Item(fee="fee_id_2", amount_cents=5) return CreditNote( - lago_id="credit_note_id", - reason= 'other', - items= Items(__root__=[item1, item2]) + lago_id="credit_note_id", reason="other", items=Items(__root__=[item1, item2]) ) def credit_note_update_object(): - return CreditNoteUpdate(refund_status='pending') + return CreditNoteUpdate(refund_status="pending") def estimate_credit_note(): @@ -43,123 +40,157 @@ def estimate_credit_note(): ) return CreditNoteEstimate( - invoice_id='1a901a90-1a90-1a90-1a90-1a901a901a90', - items= Items(__root__=[item1, item2]) + invoice_id="1a901a90-1a90-1a90-1a90-1a901a901a90", + items=Items(__root__=[item1, item2]), ) def mock_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/credit_note.json') + data_path = os.path.join(current_dir, "fixtures/credit_note.json") - with open(data_path, 'rb') as credit_note_response: + with open(data_path, "rb") as credit_note_response: return credit_note_response.read() def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/credit_note_index.json') + data_path = os.path.join(current_dir, "fixtures/credit_note_index.json") - with open(data_path, 'rb') as credit_notes_response: + with open(data_path, "rb") as credit_notes_response: return credit_notes_response.read() def mock_estimated_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/credit_note_estimated.json') + data_path = os.path.join(current_dir, "fixtures/credit_note_estimated.json") - with open(data_path, 'rb') as credit_note_estimated_response: + with open(data_path, "rb") as credit_note_estimated_response: return credit_note_estimated_response.read() def test_valid_find_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/credit_notes/' + identifier, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/credit_notes/" + identifier, + content=mock_response(), + ) response = client.credit_notes.find(identifier) assert response.lago_id == identifier def test_invalid_find_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - identifier = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="invalid") + identifier = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/credit_notes/' + identifier, status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/credit_notes/" + identifier, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.credit_notes.find(identifier) def test_valid_find_all_credit_notes_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/credit_notes?per_page=2&page=1', content=mock_collection_response()) - response = client.credit_notes.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/credit_notes?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.credit_notes.find_all({"per_page": 2, "page": 1}) - assert response['credit_notes'][0].lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response['meta']['current_page'] == 1 + assert response["credit_notes"][0].lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response["meta"]["current_page"] == 1 def test_valid_download_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='POST', - url='https://api.getlago.com/api/v1/credit_notes/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/download', + method="POST", + url="https://api.getlago.com/api/v1/credit_notes/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/download", content=mock_response(), ) - response = client.credit_notes.download('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + response = client.credit_notes.download("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_create_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/credit_notes', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/credit_notes", + content=mock_response(), + ) response = client.credit_notes.create(credit_note_object()) assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - assert response.refund_status == 'pending' + assert response.refund_status == "pending" def test_invalid_create_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/credit_notes', status_code=422, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/credit_notes", + status_code=422, + content=b"", + ) with pytest.raises(LagoApiError): client.credit_notes.create(credit_note_object()) def test_valid_update_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - credit_note_id = 'credit-note-id' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + credit_note_id = "credit-note-id" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/credit_notes/' + credit_note_id, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/credit_notes/" + credit_note_id, + content=mock_response(), + ) response = client.credit_notes.update(credit_note_update_object(), credit_note_id) assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - assert response.refund_status == 'pending' + assert response.refund_status == "pending" def test_valid_void_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - credit_note_id = 'credit-note-id' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + credit_note_id = "credit-note-id" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/credit_notes/' + credit_note_id + '/void', content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/credit_notes/" + credit_note_id + "/void", + content=mock_response(), + ) response = client.credit_notes.void(credit_note_id) assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - assert response.refund_status == 'pending' + assert response.refund_status == "pending" def test_valid_estimate_credit_note_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/credit_notes/estimate', content=mock_estimated_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/credit_notes/estimate", + content=mock_estimated_response(), + ) response = client.credit_notes.estimate(estimate_credit_note()) - assert response.lago_invoice_id == '1a901a90-1a90-1a90-1a90-1a901a901a90' + assert response.lago_invoice_id == "1a901a90-1a90-1a90-1a90-1a901a901a90" diff --git a/tests/test_customer_client.py b/tests/test_customer_client.py index 3ce72902..5c6ef4d3 100644 --- a/tests/test_customer_client.py +++ b/tests/test_customer_client.py @@ -5,201 +5,234 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import Customer, IntegrationCustomer, IntegrationCustomersList,\ - CustomerBillingConfiguration, Metadata, MetadataList, Address +from lago_python_client.models import ( + Customer, + IntegrationCustomer, + IntegrationCustomersList, + CustomerBillingConfiguration, + Metadata, + MetadataList, + Address, +) def create_customer(): - metadata = Metadata( - display_in_invoice=True, - key='key', - value='value' - ) + metadata = Metadata(display_in_invoice=True, key="key", value="value") metadata_list = MetadataList(__root__=[metadata]) integration_customer = IntegrationCustomer( - integration_type='netsuite', - integration_code='test-123', - subsidiary_id='2', - sync_with_provider=True + integration_type="netsuite", + integration_code="test-123", + subsidiary_id="2", + sync_with_provider=True, + ) + integration_customers_list = IntegrationCustomersList( + __root__=[integration_customer] ) - integration_customers_list = IntegrationCustomersList(__root__=[integration_customer]) return Customer( - external_id='5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', - name='Gavin Belson', - currency='EUR', - tax_identification_number='EU123456789', + external_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + name="Gavin Belson", + currency="EUR", + tax_identification_number="EU123456789", net_payment_term=None, billing_configuration=CustomerBillingConfiguration( invoice_grace_period=3, - payment_provider='stripe', - payment_provider_code='stripe-eu-1', - provider_customer_id='cus_12345', + payment_provider="stripe", + payment_provider_code="stripe-eu-1", + provider_customer_id="cus_12345", sync_with_provider=True, document_locale="fr", provider_payment_methods=["card", "sepa_debit"], ), shipping_address=Address( - city='Paris', - zipcode='123', - country='FR', - address_line1='Test Ave', - state='XZ' -, ), + city="Paris", + zipcode="123", + country="FR", + address_line1="Test Ave", + state="XZ", + ), integration_customers=integration_customers_list, - metadata=metadata_list + metadata=metadata_list, ) -def mock_response(mock='customer'): +def mock_response(mock="customer"): this_dir = os.path.dirname(os.path.abspath(__file__)) - my_data_path = os.path.join(this_dir, 'fixtures/' + mock + '.json') + my_data_path = os.path.join(this_dir, "fixtures/" + mock + ".json") - with open(my_data_path, 'rb') as customer_response: + with open(my_data_path, "rb") as customer_response: return customer_response.read() def test_valid_create_customers_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/customers', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/customers", + content=mock_response(), + ) response = client.customers.create(create_customer()) - assert response.external_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.name == 'Gavin Belson' - assert response.email == 'dinesh@piedpiper.test' - assert response.currency == 'EUR' - assert response.tax_identification_number == 'EU123456789' - assert response.net_payment_term == None + assert response.external_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.name == "Gavin Belson" + assert response.email == "dinesh@piedpiper.test" + assert response.currency == "EUR" + assert response.tax_identification_number == "EU123456789" + assert response.net_payment_term is None assert response.billing_configuration.invoice_grace_period == 3 - assert response.billing_configuration.payment_provider == 'stripe' - assert response.billing_configuration.payment_provider_code == 'stripe-eu-1' - assert response.billing_configuration.provider_customer_id == 'cus_12345' - assert response.billing_configuration.sync_with_provider == True + assert response.billing_configuration.payment_provider == "stripe" + assert response.billing_configuration.payment_provider_code == "stripe-eu-1" + assert response.billing_configuration.provider_customer_id == "cus_12345" + assert response.billing_configuration.sync_with_provider is True assert response.billing_configuration.document_locale == "fr" - assert response.shipping_address.city == 'Paris' - assert response.shipping_address.country == 'FR' - assert response.shipping_address.zipcode == '123' - assert response.shipping_address.address_line1 == 'Test Ave' - assert response.shipping_address.address_line2 == None - assert response.shipping_address.state == 'XZ' - assert response.integration_customers.__root__[0].external_customer_id == 'test-12345' + assert response.shipping_address.city == "Paris" + assert response.shipping_address.country == "FR" + assert response.shipping_address.zipcode == "123" + assert response.shipping_address.address_line1 == "Test Ave" + assert response.shipping_address.address_line2 is None + assert response.shipping_address.state == "XZ" + assert ( + response.integration_customers.__root__[0].external_customer_id == "test-12345" + ) assert response.integration_customers.__root__[0].type == "netsuite" - assert response.metadata.__root__[0].lago_id == '12345' - assert response.metadata.__root__[0].key == 'key' - assert response.metadata.__root__[0].value == 'value' + assert response.metadata.__root__[0].lago_id == "12345" + assert response.metadata.__root__[0].key == "key" + assert response.metadata.__root__[0].value == "value" def test_invalid_create_customers_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/customers', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/customers", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.customers.create(create_customer()) def test_valid_current_usage(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/customers/external_customer_id/current_usage?external_subscription_id=123', - content=mock_response('customer_usage'), + method="GET", + url="https://api.getlago.com/api/v1/customers/external_customer_id/current_usage?external_subscription_id=123", + content=mock_response("customer_usage"), ) - response = client.customers.current_usage('external_customer_id', '123') + response = client.customers.current_usage("external_customer_id", "123") - assert response.from_datetime == '2022-07-01T00:00:00Z' + assert response.from_datetime == "2022-07-01T00:00:00Z" assert len(response.charges_usage) == 1 assert response.charges_usage[0].units == 1.0 assert len(response.charges_usage[0].filters) == 1 - assert response.charges_usage[0].filters[0].values['country'] == ['france'] + assert response.charges_usage[0].filters[0].values["country"] == ["france"] def test_invalid_current_usage(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/customers/invalid_customer/current_usage?external_subscription_id=123', + method="GET", + url="https://api.getlago.com/api/v1/customers/invalid_customer/current_usage?external_subscription_id=123", status_code=404, - content=b'', + content=b"", ) with pytest.raises(LagoApiError): - client.customers.current_usage('invalid_customer', '123') + client.customers.current_usage("invalid_customer", "123") def test_valid_past_usage(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/customers/external_customer_id/past_usage?external_subscription_id=123', - content=mock_response('customer_past_usage'), + method="GET", + url="https://api.getlago.com/api/v1/customers/external_customer_id/past_usage?external_subscription_id=123", + content=mock_response("customer_past_usage"), ) - response = client.customers.past_usage('external_customer_id', '123') + response = client.customers.past_usage("external_customer_id", "123") - assert len(response['usage_periods']) == 1 - assert response['usage_periods'][0].from_datetime == '2022-07-01T00:00:00Z' - assert len(response['usage_periods'][0].charges_usage) == 1 - assert response['usage_periods'][0].charges_usage[0].units == 1.0 - assert len(response['usage_periods'][0].charges_usage[0].filters) == 1 + assert len(response["usage_periods"]) == 1 + assert response["usage_periods"][0].from_datetime == "2022-07-01T00:00:00Z" + assert len(response["usage_periods"][0].charges_usage) == 1 + assert response["usage_periods"][0].charges_usage[0].units == 1.0 + assert len(response["usage_periods"][0].charges_usage[0].filters) == 1 def test_invalid_past_usage(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/customers/invalid_customer/past_usage?external_subscription_id=123', + method="GET", + url="https://api.getlago.com/api/v1/customers/invalid_customer/past_usage?external_subscription_id=123", status_code=404, - content=b'', + content=b"", ) with pytest.raises(LagoApiError): - client.customers.past_usage('invalid_customer', '123') + client.customers.past_usage("invalid_customer", "123") def test_valid_portal_url(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/customers/external_customer_id/portal_url', - content=mock_response('customer_portal_url'), + method="GET", + url="https://api.getlago.com/api/v1/customers/external_customer_id/portal_url", + content=mock_response("customer_portal_url"), ) - response = client.customers.portal_url('external_customer_id') + response = client.customers.portal_url("external_customer_id") - assert response == "https://app.lago.dev/customer-portal/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaWt3WkdGbE1qWmxZUzFqWlRnekxUUTJZell0T1dRNFl5MHdabVF4TURabFlqY3dNVElHT2daRlZBPT0iLCJleHAiOiIyMDIzLTAzLTIzVDIzOjAzOjAwLjM2NloiLCJwdXIiOm51bGx9fQ==--7128c6e541adc7b4c14249b1b18509f92e652d17" + assert ( + response + == "https://app.lago.dev/customer-portal/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaWt3WkdGbE1qWmxZUzFqWlRnekxUUTJZell0T1dRNFl5MHdabVF4TURabFlqY3dNVElHT2daRlZBPT0iLCJleHAiOiIyMDIzLTAzLTIzVDIzOjAzOjAwLjM2NloiLCJwdXIiOm51bGx9fQ==--7128c6e541adc7b4c14249b1b18509f92e652d17" + ) def test_invalid_portal_url(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/customers/invalid_customer/portal_url', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/customers/invalid_customer/portal_url", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): - client.customers.portal_url('invalid_customer') + client.customers.portal_url("invalid_customer") def test_valid_destroy_customer_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - external_id = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + external_id = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/customers/' + external_id, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/customers/" + external_id, + content=mock_response(), + ) response = client.customers.destroy(external_id) - assert response.lago_id == '99a6094e-199b-4101-896a-54e927ce7bd7' + assert response.lago_id == "99a6094e-199b-4101-896a-54e927ce7bd7" assert response.external_id == external_id def test_invalid_destroy_customer_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - external_id = 'external_id' + client = Client(api_key="invalid") + external_id = "external_id" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/customers/' + external_id, status_code=404, content=b'') + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/customers/" + external_id, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.customers.destroy(external_id) diff --git a/tests/test_event_client.py b/tests/test_event_client.py index 5587c74d..fb840309 100644 --- a/tests/test_event_client.py +++ b/tests/test_event_client.py @@ -9,108 +9,162 @@ def create_event(): - return Event(external_subscription_id='5eb02857-a71e-4ea2-bcf9-57d8885990ba', code='123', transaction_id='123') + return Event( + external_subscription_id="5eb02857-a71e-4ea2-bcf9-57d8885990ba", + code="123", + transaction_id="123", + ) def create_event_with_string_timestamp(): - return Event(external_subscription_id='5eb02857-a71e-4ea2-bcf9-57d8885990ba', code='123', transaction_id='123', timestamp='1651240791.123') + return Event( + external_subscription_id="5eb02857-a71e-4ea2-bcf9-57d8885990ba", + code="123", + transaction_id="123", + timestamp="1651240791.123", + ) def create_batch_event(): return BatchEvent( events=[ - Event(external_subscription_id='5eb02857-a71e-4ea2-bcf9-57d8885990ba', code='123', transaction_id='123'), + Event( + external_subscription_id="5eb02857-a71e-4ea2-bcf9-57d8885990ba", + code="123", + transaction_id="123", + ), ], ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/event.json') + data_path = os.path.join(this_dir, "fixtures/event.json") - with open(data_path, 'rb') as event_response: + with open(data_path, "rb") as event_response: return event_response.read() + def mock_fees_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/fees.json') + data_path = os.path.join(this_dir, "fixtures/fees.json") - with open(data_path, 'rb') as fees_response: + with open(data_path, "rb") as fees_response: return fees_response.read() def test_valid_create_events_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events', content=b'') - client.events.create(create_event()) # Any response means success, any exception - failure + httpx_mock.add_response( + method="POST", url="https://api.getlago.com/api/v1/events", content=b"" + ) + client.events.create( + create_event() + ) # Any response means success, any exception - failure def test_valid_create_events_request_with_string_timestamp(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events', content=b'') - client.events.create(create_event_with_string_timestamp()) # Any response means success, any exception - failure + httpx_mock.add_response( + method="POST", url="https://api.getlago.com/api/v1/events", content=b"" + ) + client.events.create( + create_event_with_string_timestamp() + ) # Any response means success, any exception - failure def test_invalid_create_events_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/events", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.events.create(create_event()) def test_valid_create_batch_events_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events/batch', content=b'') - client.events.batch_create(create_batch_event()) # Any response means success, any exception - failure + httpx_mock.add_response( + method="POST", url="https://api.getlago.com/api/v1/events/batch", content=b"" + ) + client.events.batch_create( + create_batch_event() + ) # Any response means success, any exception - failure def test_invalid_create_batch_events_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events/batch', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/events/batch", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.events.batch_create(create_batch_event()) def test_valid_find_event_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - event_id = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + event_id = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/events/' + event_id, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/events/" + event_id, + content=mock_response(), + ) response = client.events.find(event_id) assert response.lago_id == event_id def test_invalid_find_events_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - event_id = 'INVALID_EVENT' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/events/' + event_id, status_code=404, content=b'') + client = Client(api_key="invalid") + event_id = "INVALID_EVENT" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/events/" + event_id, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.events.find(event_id) def test_valid_estimate_fees_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events/estimate_fees', content=mock_fees_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/events/estimate_fees", + content=mock_fees_response(), + ) response = client.events.estimate_fees(create_event()) - assert response['fees'][0].item.type == 'instant_charge' + assert response["fees"][0].item.type == "instant_charge" def test_invalid_estimate_fees_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/events/estimate_fees', status_code=404, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/events/estimate_fees", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.events.estimate_fees(create_event()) diff --git a/tests/test_fee_client.py b/tests/test_fee_client.py index 23543c62..5f590b70 100644 --- a/tests/test_fee_client.py +++ b/tests/test_fee_client.py @@ -7,19 +7,23 @@ def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - my_data_path = os.path.join(this_dir, 'fixtures/fee.json') + my_data_path = os.path.join(this_dir, "fixtures/fee.json") - with open(my_data_path, 'rb') as fee_response: + with open(my_data_path, "rb") as fee_response: return fee_response.read() def test_valid_find_fee_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - fee_invoice_display_name = 'fee_invoice_display_name' - charge_invoice_display_name = 'charge_invoice_display_name' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/fees/' + identifier, content=mock_response()) + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + fee_invoice_display_name = "fee_invoice_display_name" + charge_invoice_display_name = "charge_invoice_display_name" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/fees/" + identifier, + content=mock_response(), + ) response = client.fees.find(identifier) assert response.lago_id == identifier diff --git a/tests/test_functools_ext.py b/tests/test_functools_ext.py index 3e0d43de..78805991 100644 --- a/tests/test_functools_ext.py +++ b/tests/test_functools_ext.py @@ -16,7 +16,9 @@ def collection(self) -> InternalCollectionClient: return InternalCollectionClient() -@pytest.mark.filterwarnings("ignore:We are going to deprecate callable properties") # we need test both cases to ensure we keep compatibility +@pytest.mark.filterwarnings( + "ignore:We are going to deprecate callable properties" +) # we need test both cases to ensure we keep compatibility def test_callable_cached_property(): # Given `InternalCollectionClient` class # And `Client` class with `callable_cached_property` decorator on `collection` method diff --git a/tests/test_gross_revenue_client.py b/tests/test_gross_revenue_client.py index 49a51257..f0d79f33 100644 --- a/tests/test_gross_revenue_client.py +++ b/tests/test_gross_revenue_client.py @@ -1,28 +1,29 @@ import os -import pytest from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.gross_revenue import GrossRevenueResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/gross_revenue_index.json') + data_path = os.path.join(current_dir, "fixtures/gross_revenue_index.json") - with open(data_path, 'rb') as gross_revenues_response: + with open(data_path, "rb") as gross_revenues_response: return gross_revenues_response.read() def test_valid_find_all_gross_revenues_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/analytics/gross_revenue', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/analytics/gross_revenue", + content=mock_collection_response(), + ) response = client.gross_revenues.find_all() - assert response['gross_revenues'][0].currency == 'EUR' - assert response['gross_revenues'][0].amount_cents == 100 - assert response['gross_revenues'][0].month == '2023-11-01T00:00:00.000Z' - assert response['gross_revenues'][0].invoices_count == 1 + assert response["gross_revenues"][0].currency == "EUR" + assert response["gross_revenues"][0].amount_cents == 100 + assert response["gross_revenues"][0].month == "2023-11-01T00:00:00.000Z" + assert response["gross_revenues"][0].invoices_count == 1 diff --git a/tests/test_invoice_client.py b/tests/test_invoice_client.py index 403efad3..3ccb03ff 100644 --- a/tests/test_invoice_client.py +++ b/tests/test_invoice_client.py @@ -5,191 +5,262 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import Invoice, InvoiceMetadata, InvoiceMetadataList,\ - OneOffInvoice, InvoiceFeesList, InvoiceFee +from lago_python_client.models import ( + Invoice, + InvoiceMetadata, + InvoiceMetadataList, + OneOffInvoice, + InvoiceFeesList, + InvoiceFee, +) def update_invoice_object(): - metadata = InvoiceMetadata(key='key', value='value') + metadata = InvoiceMetadata(key="key", value="value") metadata_list = InvoiceMetadataList(__root__=[metadata]) - return Invoice(payment_status='failed', metadata=metadata_list) + return Invoice(payment_status="failed", metadata=metadata_list) def one_off_invoice_object(): - fee = InvoiceFee(add_on_code='123', description='desc') + fee = InvoiceFee(add_on_code="123", description="desc") fees_list = InvoiceFeesList(__root__=[fee]) - return OneOffInvoice(customer_external_id='external', currency='EUR', fees=fees_list) + return OneOffInvoice( + customer_external_id="external", currency="EUR", fees=fees_list + ) -def mock_response(mock='invoice'): +def mock_response(mock="invoice"): this_dir = os.path.dirname(os.path.abspath(__file__)) - my_data_path = os.path.join(this_dir, 'fixtures/' + mock + '.json') + my_data_path = os.path.join(this_dir, "fixtures/" + mock + ".json") - with open(my_data_path, 'rb') as invoice_response: + with open(my_data_path, "rb") as invoice_response: return invoice_response.read() def mock_payment_url_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/payment_url.json') + data_path = os.path.join(this_dir, "fixtures/payment_url.json") - with open(data_path, 'rb') as payment_url_response: + with open(data_path, "rb") as payment_url_response: return payment_url_response.read() def test_valid_update_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', content=mock_response()) - response = client.invoices.update(update_invoice_object(), '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + content=mock_response(), + ) + response = client.invoices.update( + update_invoice_object(), "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + ) - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.status == 'finalized' - assert response.payment_status == 'failed' - assert response.metadata.__root__[0].key == 'key' - assert response.metadata.__root__[0].value == 'value' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.status == "finalized" + assert response.payment_status == "failed" + assert response.metadata.__root__[0].key == "key" + assert response.metadata.__root__[0].value == "value" def test_valid_create_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/invoices', content=mock_response(mock='one_off_invoice')) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices", + content=mock_response(mock="one_off_invoice"), + ) response = client.invoices.create(one_off_invoice_object()) - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.invoice_type == 'one_off' - assert response.fees.__root__[0].invoice_display_name == 'fee_invoice_display_name' - assert response.fees.__root__[0].precise_unit_amount == '9.52' - assert response.fees.__root__[0].item.invoice_display_name == 'one_off_invoice_display_name' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.invoice_type == "one_off" + assert response.fees.__root__[0].invoice_display_name == "fee_invoice_display_name" + assert response.fees.__root__[0].precise_unit_amount == "9.52" + assert ( + response.fees.__root__[0].item.invoice_display_name + == "one_off_invoice_display_name" + ) assert response.fees.__root__[0].amount_details == {} def test_invalid_create_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/invoices', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.invoices.create(one_off_invoice_object()) def test_invalid_update_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', status_code=401, content=b'') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): - client.invoices.update(update_invoice_object(), '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + client.invoices.update( + update_invoice_object(), "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + ) def test_valid_find_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/invoices/' + identifier, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices/" + identifier, + content=mock_response(), + ) response = client.invoices.find(identifier) assert response.lago_id == identifier def test_invalid_find_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - identifier = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + client = Client(api_key="invalid") + identifier = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/invoices/' + identifier, status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices/" + identifier, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.invoices.find(identifier) def test_valid_find_all_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/invoices', content=mock_response(mock='invoice_index')) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices", + content=mock_response(mock="invoice_index"), + ) response = client.invoices.find_all() - assert response['invoices'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert response["invoices"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1111" + assert response["meta"]["current_page"] == 1 def test_valid_find_all_invoice_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/invoices?per_page=2&page=1', content=mock_response(mock='invoice_index')) - response = client.invoices.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices?per_page=2&page=1", + content=mock_response(mock="invoice_index"), + ) + response = client.invoices.find_all({"per_page": 2, "page": 1}) - assert response['invoices'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1222' - assert response['meta']['current_page'] == 1 + assert response["invoices"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1222" + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/invoices', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.invoices.find_all() def test_valid_download_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/download', content=mock_response()) - response = client.invoices.download('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/download", + content=mock_response(), + ) + response = client.invoices.download("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_refresh_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/refresh', content=mock_response()) - response = client.invoices.refresh('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/refresh", + content=mock_response(), + ) + response = client.invoices.refresh("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_lose_dispute_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/lose_dispute', content=mock_response()) - response = client.invoices.lose_dispute('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/lose_dispute", + content=mock_response(), + ) + response = client.invoices.lose_dispute("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_finalize_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/finalize', content=mock_response()) - response = client.invoices.finalize('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/finalize", + content=mock_response(), + ) + response = client.invoices.finalize("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_retry_payment_invoice_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='POST', - url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/retry_payment', + method="POST", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/retry_payment", content=mock_response(), ) - response = client.invoices.retry_payment('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + response = client.invoices.retry_payment("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_valid_payment_url_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='POST', - url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/payment_url', + method="POST", + url="https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/payment_url", content=mock_payment_url_response(), ) - response = client.invoices.payment_url('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + response = client.invoices.payment_url("5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba") - assert response == 'https://checkout.stripe.com/c/pay/cs_test_a1cuqFkXvH' + assert response == "https://checkout.stripe.com/c/pay/cs_test_a1cuqFkXvH" diff --git a/tests/test_invoice_collection_client.py b/tests/test_invoice_collection_client.py index b218b2a5..e5dc8886 100644 --- a/tests/test_invoice_collection_client.py +++ b/tests/test_invoice_collection_client.py @@ -1,29 +1,30 @@ import os -import pytest from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.invoice_collection import InvoiceCollectionResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/invoice_collection_index.json') + data_path = os.path.join(current_dir, "fixtures/invoice_collection_index.json") - with open(data_path, 'rb') as invoice_collections_response: + with open(data_path, "rb") as invoice_collections_response: return invoice_collections_response.read() def test_valid_find_all_invoice_collections_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/analytics/invoice_collection', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/analytics/invoice_collection", + content=mock_collection_response(), + ) response = client.invoice_collections.find_all() - assert response['invoice_collections'][0].currency == 'EUR' - assert response['invoice_collections'][0].amount_cents == 100 - assert response['invoice_collections'][0].month == '2023-11-01T00:00:00.000Z' - assert response['invoice_collections'][0].invoices_count == 10 - assert response['invoice_collections'][0].payment_status == 'pending' + assert response["invoice_collections"][0].currency == "EUR" + assert response["invoice_collections"][0].amount_cents == 100 + assert response["invoice_collections"][0].month == "2023-11-01T00:00:00.000Z" + assert response["invoice_collections"][0].invoices_count == 10 + assert response["invoice_collections"][0].payment_status == "pending" diff --git a/tests/test_invoiced_usage_client.py b/tests/test_invoiced_usage_client.py index dcf40fa5..700c7d6b 100644 --- a/tests/test_invoiced_usage_client.py +++ b/tests/test_invoiced_usage_client.py @@ -1,28 +1,29 @@ import os -import pytest from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.invoiced_usage import InvoicedUsageResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/invoiced_usage_index.json') + data_path = os.path.join(current_dir, "fixtures/invoiced_usage_index.json") - with open(data_path, 'rb') as invoiced_usages_response: + with open(data_path, "rb") as invoiced_usages_response: return invoiced_usages_response.read() def test_valid_find_all_invoiced_usages_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/analytics/invoiced_usage', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/analytics/invoiced_usage", + content=mock_collection_response(), + ) response = client.invoiced_usages.find_all() - assert response['invoiced_usages'][0].currency == 'EUR' - assert response['invoiced_usages'][0].amount_cents == 100 - assert response['invoiced_usages'][0].code == 'code' - assert response['invoiced_usages'][0].month == '2023-11-01T00:00:00.000Z' + assert response["invoiced_usages"][0].currency == "EUR" + assert response["invoiced_usages"][0].amount_cents == 100 + assert response["invoiced_usages"][0].code == "code" + assert response["invoiced_usages"][0].month == "2023-11-01T00:00:00.000Z" diff --git a/tests/test_json_services.py b/tests/test_json_services.py index 246b18a4..9825ca1f 100644 --- a/tests/test_json_services.py +++ b/tests/test_json_services.py @@ -1,4 +1,5 @@ """Test JSON services.""" + from httpx import Response import pytest @@ -9,7 +10,7 @@ def test_from_json(): """Deserialize data from json string.""" # Given some data - expected_data = {'a': {'b': 'c'}} + expected_data = {"a": {"b": "c"}} # And same data serialized in json format stored as string, json_string = '{"a":{"b":"c"}}' # ... or bytes, @@ -29,7 +30,7 @@ def test_from_json(): # ... or raise exception with pytest.raises(LagoApiError) as cm: from_json(json_none) - assert cm.value.detail == 'Input must be bytes, bytearray, memoryview, or str' + assert cm.value.detail == "Input must be bytes, bytearray, memoryview, or str" with pytest.raises(LagoApiError): from_json(json_string_shit_happens) @@ -37,7 +38,7 @@ def test_from_json(): def test_to_json(): """Serialize data to json bytestring.""" # Given some data - initial_data = {'a': {'b': 'c'}} + initial_data = {"a": {"b": "c"}} # And same data serialized in json format expected_json_string = b'{"a":{"b":"c"}}' diff --git a/tests/test_mrr_client.py b/tests/test_mrr_client.py index c35a61e8..97c96b8f 100644 --- a/tests/test_mrr_client.py +++ b/tests/test_mrr_client.py @@ -1,27 +1,28 @@ import os -import pytest from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.mrr import MrrResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/mrr_index.json') + data_path = os.path.join(current_dir, "fixtures/mrr_index.json") - with open(data_path, 'rb') as mrrs_response: + with open(data_path, "rb") as mrrs_response: return mrrs_response.read() def test_valid_find_all_mrrs_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/analytics/mrr', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/analytics/mrr", + content=mock_collection_response(), + ) response = client.mrrs.find_all() - assert response['mrrs'][0].currency == 'EUR' - assert response['mrrs'][0].amount_cents == 100 - assert response['mrrs'][0].month == '2023-11-01T00:00:00.000Z' + assert response["mrrs"][0].currency == "EUR" + assert response["mrrs"][0].amount_cents == 100 + assert response["mrrs"][0].month == "2023-11-01T00:00:00.000Z" diff --git a/tests/test_organization_client.py b/tests/test_organization_client.py index dc5399cb..b51df79e 100644 --- a/tests/test_organization_client.py +++ b/tests/test_organization_client.py @@ -12,42 +12,49 @@ def organization_object(): return Organization( webhook_url="https://test-example.example", webhook_urls=["https://test-example.example", "https://test-example2.example"], - tax_identification_number='EU123456789', + tax_identification_number="EU123456789", net_payment_term=0, - default_currency='EUR', - document_number_prefix='ORG-1234', + default_currency="EUR", + document_number_prefix="ORG-1234", billing_configuration=OrganizationBillingConfiguration( - invoice_footer='footer', - invoice_grace_period=3, - document_locale="fr" - ) + invoice_footer="footer", invoice_grace_period=3, document_locale="fr" + ), ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/organization.json') + data_path = os.path.join(this_dir, "fixtures/organization.json") - with open(data_path, 'rb') as organization_response: + with open(data_path, "rb") as organization_response: return organization_response.read() def test_valid_update_organization_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/organizations', content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/organizations", + content=mock_response(), + ) response = client.organizations.update(organization_object()) - assert response.name == 'Hooli' - assert response.tax_identification_number == 'EU123456789' - assert response.document_number_prefix == 'ORG-1234' + assert response.name == "Hooli" + assert response.tax_identification_number == "EU123456789" + assert response.document_number_prefix == "ORG-1234" assert response.net_payment_term == 0 def test_invalid_update_organization_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/organizations', status_code=401, content=b'') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/organizations", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.organizations.update(organization_object()) diff --git a/tests/test_overdue_balance_client.py b/tests/test_overdue_balance_client.py index afed7ee6..7576b8e9 100644 --- a/tests/test_overdue_balance_client.py +++ b/tests/test_overdue_balance_client.py @@ -1,28 +1,31 @@ import os -import pytest from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.overdue_balance import OverdueBalanceResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/overdue_balance_index.json') + data_path = os.path.join(current_dir, "fixtures/overdue_balance_index.json") - with open(data_path, 'rb') as overdue_balances_response: + with open(data_path, "rb") as overdue_balances_response: return overdue_balances_response.read() def test_valid_find_all_overdue_balances_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/analytics/overdue_balance', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/analytics/overdue_balance", + content=mock_collection_response(), + ) response = client.overdue_balances.find_all() - assert response['overdue_balances'][0].currency == 'EUR' - assert response['overdue_balances'][0].amount_cents == 100 - assert response['overdue_balances'][0].month == '2023-11-01T00:00:00.000Z' - assert response['overdue_balances'][0].lago_invoice_ids == ['1a901a90-1a90-1a90-1a90-1a901a901a90'] + assert response["overdue_balances"][0].currency == "EUR" + assert response["overdue_balances"][0].amount_cents == 100 + assert response["overdue_balances"][0].month == "2023-11-01T00:00:00.000Z" + assert response["overdue_balances"][0].lago_invoice_ids == [ + "1a901a90-1a90-1a90-1a90-1a901a901a90" + ] diff --git a/tests/test_payment_request_client.py b/tests/test_payment_request_client.py index 6e04f2a7..1ffb3d6c 100644 --- a/tests/test_payment_request_client.py +++ b/tests/test_payment_request_client.py @@ -5,41 +5,59 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models.payment_request import PaymentRequestResponse def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/payment_request_index.json') + data_path = os.path.join(current_dir, "fixtures/payment_request_index.json") - with open(data_path, 'rb') as payment_requests_response: + with open(data_path, "rb") as payment_requests_response: return payment_requests_response.read() def test_valid_find_all_payment_requests_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/payment_requests', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/payment_requests", + content=mock_collection_response(), + ) response = client.payment_requests.find_all() - assert response['payment_requests'][0].lago_id == '89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d' - assert response['meta']['current_page'] == 1 + assert ( + response["payment_requests"][0].lago_id + == "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d" + ) + assert response["meta"]["current_page"] == 1 def test_valid_find_all_payment_requests_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/payment_requests?per_page=2&page=1', content=mock_collection_response()) - response = client.payment_requests.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/payment_requests?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.payment_requests.find_all({"per_page": 2, "page": 1}) - assert response['payment_requests'][0].lago_id == '89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d' - assert response['meta']['current_page'] == 1 + assert ( + response["payment_requests"][0].lago_id + == "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d" + ) + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_payment_requests_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/payment_requests', status_code=404, content=b'') + client = Client(api_key="invalid") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/payment_requests", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.payment_requests.find_all() diff --git a/tests/test_request_services.py b/tests/test_request_services.py index 2d7ba241..9c2195cc 100644 --- a/tests/test_request_services.py +++ b/tests/test_request_services.py @@ -1,7 +1,15 @@ """Test request services.""" + import httpx -from lago_python_client.services.request import make_headers, make_url, send_delete_request, send_get_request, send_post_request, send_put_request +from lago_python_client.services.request import ( + make_headers, + make_url, + send_delete_request, + send_get_request, + send_post_request, + send_put_request, +) from lago_python_client.version import LAGO_VERSION @@ -13,38 +21,43 @@ def test_make_headers(): result = make_headers(api_key=api_key) # Then assert result == { - 'Content-type': 'application/json', - 'Authorization': "Bearer test", - 'User-agent': 'Lago Python v{version}'.format(version=LAGO_VERSION), + "Content-type": "application/json", + "Authorization": "Bearer test", + "User-agent": "Lago Python v{version}".format(version=LAGO_VERSION), } def test_make_url(): """Make url.""" # Given - api_url = 'https://api.getlago.com/api/v1/' - some_path_parts = ('team', 'anhtho', 'congratulate') + api_url = "https://api.getlago.com/api/v1/" + some_path_parts = ("team", "anhtho", "congratulate") query_name_value = { - 'message': "The future belongs to those who believe in the beauty of their dreams. Happy International Women's Day!", - 'day': 8, + "message": "The future belongs to those who believe in the beauty of their dreams. Happy International Women's Day!", + "day": 8, } # When service is applied - result = make_url(origin=api_url, path_parts=some_path_parts, query_pairs=query_name_value) + result = make_url( + origin=api_url, path_parts=some_path_parts, query_pairs=query_name_value + ) # Then - assert result == 'https://api.getlago.com/api/v1/team/anhtho/congratulate?message=The+future+belongs+to+those+who+believe+in+the+beauty+of+their+dreams.+Happy+International+Women%27s+Day%21&day=8' + assert ( + result + == "https://api.getlago.com/api/v1/team/anhtho/congratulate?message=The+future+belongs+to+those+who+believe+in+the+beauty+of+their+dreams.+Happy+International+Women%27s+Day%21&day=8" + ) def test_make_url_no_query(): """Make url without query name-value pairs.""" # Given - api_url = 'https://api.getlago.com/api/v1/' - some_path_parts = ('hello', ) + api_url = "https://api.getlago.com/api/v1/" + some_path_parts = ("hello",) # When service is applied result = make_url(origin=api_url, path_parts=some_path_parts) # Then - assert result == 'https://api.getlago.com/api/v1/hello' + assert result == "https://api.getlago.com/api/v1/hello" def test_send_delete_request(): diff --git a/tests/test_response_services.py b/tests/test_response_services.py index 1731f857..55ddf58b 100644 --- a/tests/test_response_services.py +++ b/tests/test_response_services.py @@ -1,4 +1,5 @@ """Test response services.""" + from copy import deepcopy from lago_python_client.base_model import BaseModel @@ -8,8 +9,14 @@ from lago_python_client.exceptions import LagoApiError from lago_python_client.services.response import ( Response as ServiceResponse, - RESPONSE_SUCCESS_CODES, _is_status_code_successful, _is_content_exists, verify_response, - get_response_data, prepare_object_list_response, prepare_index_response, prepare_object_response, + RESPONSE_SUCCESS_CODES, + _is_status_code_successful, + _is_content_exists, + verify_response, + get_response_data, + prepare_object_list_response, + prepare_index_response, + prepare_object_response, ) @@ -29,7 +36,7 @@ def test_is_status_code_successful(): response = Response(content=b'{"a":{"b":"c"}}', status_code=200) # ... and with error status code response404 = deepcopy(response) - response404._content = b'' + response404._content = b"" response404.status_code = 404 # When check helper function is applied @@ -45,7 +52,7 @@ def test_is_content_exists(): response = Response(content=b'{"a":{"b":"c"}}', status_code=200) # ... and with empty content response204 = deepcopy(response) - response204._content = b'' + response204._content = b"" response204.status_code = 204 # When check helper function is applied @@ -61,12 +68,12 @@ def test_verify_response(): response = Response(content=b'{"a":{"b":"c"}}', status_code=200) # ... and with successful status code and empty content response204 = deepcopy(response) - response204._content = b'' + response204._content = b"" response204.status_code = 204 # ... and with error status code response404 = deepcopy(response) response404.status_code = 404 - response404.request = Request(method='POST', url='') + response404.request = Request(method="POST", url="") # When service is applied # Then @@ -85,26 +92,26 @@ def test_get_response_data(): response200_2._content = b'[{"a": "b"}]' # ... and with successful status code and empty content response204 = deepcopy(response) - response204._content = b'' + response204._content = b"" response204.status_code = 204 # ... and with error status code response404 = deepcopy(response) response404.status_code = 404 - response404.request = Request(method='POST', url='') + response404.request = Request(method="POST", url="") # When service is applied # Then assert get_response_data(response=response) == { - 'a': { - 'b': 'c', + "a": { + "b": "c", }, } - assert get_response_data(response=response, key='a') == { - 'b': 'c', + assert get_response_data(response=response, key="a") == { + "b": "c", } assert get_response_data(response=response200_2) == [ { - 'a': 'b', + "a": "b", }, ] assert get_response_data(response=response204) is None @@ -119,7 +126,7 @@ class SomeHumanModel(BaseModel): def test_prepare_object_response(): """Verify ``prepare_object_response`` service returns Pydantic model instance.""" # Given Pydantic model and some data - data = {'name': 'Aurelia'} + data = {"name": "Aurelia"} # When service is applied result = prepare_object_response(response_model=SomeHumanModel, data=data) @@ -131,35 +138,37 @@ def test_prepare_index_response(): """Verify ``prepare_index_response`` service returns valid mapping object.""" # Given Pydantic model and some data data = { - 'human': [ - {'name': 'Aurelia'}, - {'name': 'Aleksandra'}, - {'name': 'John'}, + "human": [ + {"name": "Aurelia"}, + {"name": "Aleksandra"}, + {"name": "John"}, ], - 'meta': { - 'something': 'is here' - }, + "meta": {"something": "is here"}, } # When service is applied - result = prepare_index_response(api_resource='human', response_model=SomeHumanModel, data=data) + result = prepare_index_response( + api_resource="human", response_model=SomeHumanModel, data=data + ) # Then - assert SomeHumanModel(**data['human'][0]) in result['human'] - assert len(result['human']) == 3 - assert 'meta' in result + assert SomeHumanModel(**data["human"][0]) in result["human"] + assert len(result["human"]) == 3 + assert "meta" in result def test_prepare_object_list_response(): """Verify ``prepare_object_list_response`` service returns valid mapping object.""" # Given Pydantic model and some data data = [ - {'name': 'Aurelia'}, - {'name': 'Aleksandra'}, - {'name': 'John'}, + {"name": "Aurelia"}, + {"name": "Aleksandra"}, + {"name": "John"}, ] # When service is applied - result = prepare_object_list_response(api_resource='human', response_model=SomeHumanModel, data=data) + result = prepare_object_list_response( + api_resource="human", response_model=SomeHumanModel, data=data + ) # Then - assert SomeHumanModel(**data[0]) == result['human'][0] - assert len(result['human']) == 3 + assert SomeHumanModel(**data[0]) == result["human"][0] + assert len(result["human"]) == 3 diff --git a/tests/test_subscription_client.py b/tests/test_subscription_client.py index 18f7c9d3..c6048c59 100644 --- a/tests/test_subscription_client.py +++ b/tests/test_subscription_client.py @@ -9,154 +9,208 @@ def create_subscription(): - return Subscription(external_customer_id='5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', plan_code='eartha lynch', - external_id='code', billing_time='anniversary', subscription_at='2022-04-29T08:59:51Z', - ending_at='2022-08-29T08:59:51Z') + return Subscription( + external_customer_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + plan_code="eartha lynch", + external_id="code", + billing_time="anniversary", + subscription_at="2022-04-29T08:59:51Z", + ending_at="2022-08-29T08:59:51Z", + ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - my_data_path = os.path.join(this_dir, 'fixtures/subscription.json') + my_data_path = os.path.join(this_dir, "fixtures/subscription.json") - with open(my_data_path, 'rb') as subscription_response: + with open(my_data_path, "rb") as subscription_response: return subscription_response.read() + def mock_response_for_pending(): this_dir = os.path.dirname(os.path.abspath(__file__)) - my_data_path = os.path.join(this_dir, 'fixtures/pending_subscription.json') + my_data_path = os.path.join(this_dir, "fixtures/pending_subscription.json") - with open(my_data_path, 'rb') as subscription_response: + with open(my_data_path, "rb") as subscription_response: return subscription_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/subscription_index.json') + data_path = os.path.join(this_dir, "fixtures/subscription_index.json") - with open(data_path, 'rb') as subscription_response: + with open(data_path, "rb") as subscription_response: return subscription_response.read() + def test_valid_create_subscriptions_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/subscriptions', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/subscriptions", + content=mock_response(), + ) response = client.subscriptions.create(create_subscription()) - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.status == 'active' - assert response.plan_code == 'eartha lynch' - assert response.billing_time == 'anniversary' - assert response.subscription_at == '2022-04-29T08:59:51Z' - assert response.ending_at == '2022-08-29T08:59:51Z' + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.status == "active" + assert response.plan_code == "eartha lynch" + assert response.billing_time == "anniversary" + assert response.subscription_at == "2022-04-29T08:59:51Z" + assert response.ending_at == "2022-08-29T08:59:51Z" def test_invalid_create_subscriptions_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/subscriptions', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/subscriptions", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.subscriptions.create(create_subscription()) def test_valid_update_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = 'sub_id' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "sub_id" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/subscriptions/' + identifier, content=mock_response()) - response = client.subscriptions.update(Subscription(name='name'), identifier) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + content=mock_response(), + ) + response = client.subscriptions.update(Subscription(name="name"), identifier) - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.status == 'active' - assert response.plan_code == 'eartha lynch' - assert response.billing_time == 'anniversary' - assert response.subscription_at == '2022-04-29T08:59:51Z' + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.status == "active" + assert response.plan_code == "eartha lynch" + assert response.billing_time == "anniversary" + assert response.subscription_at == "2022-04-29T08:59:51Z" def test_invalid_update_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - identifier = 'invalid' + client = Client(api_key="invalid") + identifier = "invalid" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/subscriptions/' + identifier, status_code=401, content=b'') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): - client.subscriptions.update(Subscription(name='name'), identifier) + client.subscriptions.update(Subscription(name="name"), identifier) def test_valid_destroy_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = 'sub_id' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/subscriptions/' + identifier, content=mock_response()) + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "sub_id" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + content=mock_response(), + ) response = client.subscriptions.destroy(identifier) - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.status == 'active' - assert response.plan_code == 'eartha lynch' + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.status == "active" + assert response.plan_code == "eartha lynch" -def test_valid_destroy_pending_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - identifier = 'sub_id' - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/subscriptions/' + identifier + '?status=pending', content=mock_response_for_pending()) - response = client.subscriptions.destroy(identifier, { 'status': 'pending' }) - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - assert response.status == 'pending' - assert response.plan_code == 'eartha lynch' +def test_valid_destroy_pending_subscription_request(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "sub_id" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/subscriptions/" + + identifier + + "?status=pending", + content=mock_response_for_pending(), + ) + response = client.subscriptions.destroy(identifier, {"status": "pending"}) + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + assert response.status == "pending" + assert response.plan_code == "eartha lynch" def test_invalid_destroy_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - identifier = 'invalid' + client = Client(api_key="invalid") + identifier = "invalid" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/subscriptions/' + identifier, status_code=404, content=b'') + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.subscriptions.destroy(identifier) -def test_valid_find_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - external_id = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/subscriptions/' + external_id, content=mock_response()) +def test_valid_find_subscription_request(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + external_id = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/subscriptions/" + external_id, + content=mock_response(), + ) response = client.subscriptions.find(external_id) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.external_customer_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" def test_invalid_find_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - external_id = 'invalid' + client = Client(api_key="invalid") + external_id = "invalid" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/subscriptions/' + external_id, status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/subscriptions/" + external_id, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.subscriptions.find(external_id) -def test_valid_find_all_subscription_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/subscriptions?external_customer_id=123', content=mock_collection_response()) - response = client.subscriptions.find_all({'external_customer_id': '123'}) - - assert response['subscriptions'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response['meta']['current_page'] == 1 def test_valid_find_all_subscription_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/subscriptions?external_customer_id=123&status[]=pending', content=mock_collection_response()) - response = client.subscriptions.find_all({'external_customer_id': '123', 'status[]': 'pending'}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/subscriptions?external_customer_id=123", + content=mock_collection_response(), + ) + response = client.subscriptions.find_all({"external_customer_id": "123"}) - assert response['subscriptions'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response['meta']['current_page'] == 1 + assert ( + response["subscriptions"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + ) + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_subscription_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/subscriptions', status_code=404, content=b'') + client = Client(api_key="invalid") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/subscriptions", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.subscriptions.find_all() diff --git a/tests/test_tax_client.py b/tests/test_tax_client.py index 1469bf42..d3b0e575 100644 --- a/tests/test_tax_client.py +++ b/tests/test_tax_client.py @@ -10,136 +10,185 @@ def tax_object(): return Tax( - name='name', - code='tax_first', + name="name", + code="tax_first", rate=15.0, - description='desc', - applied_to_organization=False + description="desc", + applied_to_organization=False, ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/tax.json') + data_path = os.path.join(this_dir, "fixtures/tax.json") - with open(data_path, 'rb') as tax_response: + with open(data_path, "rb") as tax_response: return tax_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/tax_index.json') + data_path = os.path.join(this_dir, "fixtures/tax_index.json") - with open(data_path, 'rb') as tax_response: + with open(data_path, "rb") as tax_response: return tax_response.read() def test_valid_create_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/taxes', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/taxes", + content=mock_response(), + ) response = client.taxes.create(tax_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.code == 'tax_code' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.code == "tax_code" def test_invalid_create_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/taxes', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/taxes", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.taxes.create(tax_object()) def test_valid_update_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'tax_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "tax_code" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/taxes/' + code, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/taxes/" + code, + content=mock_response(), + ) response = client.taxes.update(tax_object(), code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_update_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/taxes/' + code, status_code=401, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/taxes/" + code, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.taxes.update(tax_object(), code) def test_valid_find_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'tax_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "tax_code" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/taxes/' + code, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/taxes/" + code, + content=mock_response(), + ) response = client.taxes.find(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_find_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/taxes/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/taxes/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.taxes.find(code) def test_valid_destroy_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - code = 'tax_code' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + code = "tax_code" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/taxes/' + code, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/taxes/" + code, + content=mock_response(), + ) response = client.taxes.destroy(code) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.code == code def test_invalid_destroy_tax_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - code = 'invalid' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/taxes/' + code, status_code=404, content=b'') + client = Client(api_key="invalid") + code = "invalid" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/taxes/" + code, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.taxes.destroy(code) def test_valid_find_all_taxes_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/taxes', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/taxes", + content=mock_collection_response(), + ) response = client.taxes.find_all() - assert response['taxes'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert response["taxes"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1111" + assert response["meta"]["current_page"] == 1 def test_valid_find_all_taxes_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/taxes?per_page=2&page=1', content=mock_collection_response()) - response = client.taxes.find_all({'per_page': 2, 'page': 1}) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/taxes?per_page=2&page=1", + content=mock_collection_response(), + ) + response = client.taxes.find_all({"per_page": 2, "page": 1}) - assert response['taxes'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1222' - assert response['meta']['current_page'] == 1 + assert response["taxes"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1222" + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_taxes_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/taxes', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/taxes", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.taxes.find_all() diff --git a/tests/test_wallet_client.py b/tests/test_wallet_client.py index 3efd1721..b5268dad 100644 --- a/tests/test_wallet_client.py +++ b/tests/test_wallet_client.py @@ -5,154 +5,205 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import Wallet, RecurringTransactionRule, RecurringTransactionRuleList +from lago_python_client.models import ( + Wallet, + RecurringTransactionRule, + RecurringTransactionRuleList, +) def wallet_object(): rule = RecurringTransactionRule( - trigger='interval', - interval='monthly', - paid_credits='105.0', - granted_credits='105.0', - method='target', - target_ongoing_balance='105.0' + trigger="interval", + interval="monthly", + paid_credits="105.0", + granted_credits="105.0", + method="target", + target_ongoing_balance="105.0", ) rules_list = RecurringTransactionRuleList(__root__=[rule]) return Wallet( - name='name', - external_customer_id='12345', - rate_amount='1', - paid_credits='10', - granted_credits='10', - recurring_transaction_rules=rules_list + name="name", + external_customer_id="12345", + rate_amount="1", + paid_credits="10", + granted_credits="10", + recurring_transaction_rules=rules_list, ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/wallet.json') + data_path = os.path.join(this_dir, "fixtures/wallet.json") - with open(data_path, 'rb') as wallet_response: + with open(data_path, "rb") as wallet_response: return wallet_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/wallet_index.json') + data_path = os.path.join(this_dir, "fixtures/wallet_index.json") - with open(data_path, 'rb') as wallet_response: + with open(data_path, "rb") as wallet_response: return wallet_response.read() def test_valid_create_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/wallets', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/wallets", + content=mock_response(), + ) response = client.wallets.create(wallet_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response.recurring_transaction_rules.__root__[0].lago_id == '12345' - assert response.recurring_transaction_rules.__root__[0].trigger == 'interval' - assert response.recurring_transaction_rules.__root__[0].interval == 'monthly' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response.recurring_transaction_rules.__root__[0].lago_id == "12345" + assert response.recurring_transaction_rules.__root__[0].trigger == "interval" + assert response.recurring_transaction_rules.__root__[0].interval == "monthly" def test_invalid_create_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/wallets', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/wallets", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.wallets.create(wallet_object()) def test_valid_update_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/wallets/' + arg, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/wallets/" + arg, + content=mock_response(), + ) response = client.wallets.update(wallet_object(), arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_update_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' + client = Client(api_key="invalid") + arg = "invalid" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/wallets/' + arg, status_code=401, content=b'') + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/wallets/" + arg, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.wallets.update(wallet_object(), arg) def test_valid_find_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/wallets/' + arg, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/wallets/" + arg, + content=mock_response(), + ) response = client.wallets.find(arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_find_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' + client = Client(api_key="invalid") + arg = "invalid" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/wallets/' + arg, status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/wallets/" + arg, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.wallets.find(arg) def test_valid_destroy_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/wallets/' + arg, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/wallets/" + arg, + content=mock_response(), + ) response = client.wallets.destroy(arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_destroy_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' + client = Client(api_key="invalid") + arg = "invalid" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/wallets/' + arg, status_code=404, content=b'') + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/wallets/" + arg, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.wallets.destroy(arg) def test_valid_find_all_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/wallets', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/wallets", + content=mock_collection_response(), + ) response = client.wallets.find_all() - assert response['wallets'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response['meta']['current_page'] == 1 + assert response["wallets"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" + assert response["meta"]["current_page"] == 1 def test_valid_find_all_wallet_request_with_options(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") httpx_mock.add_response( - method='GET', - url='https://api.getlago.com/api/v1/wallets?external_customer_id=123&per_page=2&page=1', + method="GET", + url="https://api.getlago.com/api/v1/wallets?external_customer_id=123&per_page=2&page=1", content=mock_collection_response(), ) - response = client.wallets.find_all({'external_customer_id': 123, 'per_page': 2, 'page': 1}) + response = client.wallets.find_all( + {"external_customer_id": 123, "per_page": 2, "page": 1} + ) - assert response['wallets'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert response["wallets"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1111" + assert response["meta"]["current_page"] == 1 def test_invalid_find_all_wallet_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/wallets', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/wallets", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.wallets.find_all() diff --git a/tests/test_wallet_transaction_client.py b/tests/test_wallet_transaction_client.py index 11604752..a6ce3a92 100644 --- a/tests/test_wallet_transaction_client.py +++ b/tests/test_wallet_transaction_client.py @@ -10,53 +10,72 @@ def wallet_transaction_object(): return WalletTransaction( - wallet_id='123', - paid_credits='10', - granted_credits='10', - voided_credits='0' + wallet_id="123", paid_credits="10", granted_credits="10", voided_credits="0" ) def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/wallet_transaction.json') + data_path = os.path.join(this_dir, "fixtures/wallet_transaction.json") - with open(data_path, 'rb') as wallet_transaction_response: + with open(data_path, "rb") as wallet_transaction_response: return wallet_transaction_response.read() def mock_collection_response(): current_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(current_dir, 'fixtures/wallet_transaction_index.json') + data_path = os.path.join(current_dir, "fixtures/wallet_transaction_index.json") - with open(data_path, 'rb') as wallet_transaction_index_response: + with open(data_path, "rb") as wallet_transaction_index_response: return wallet_transaction_index_response.read() def test_valid_create_wallet_transaction_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/wallet_transactions', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/wallet_transactions", + content=mock_response(), + ) response = client.wallet_transactions.create(wallet_transaction_object()) - assert response['wallet_transactions'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['wallet_transactions'][1].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1222' + assert ( + response["wallet_transactions"][0].lago_id + == "b7ab2926-1de8-4428-9bcd-779314ac1111" + ) + assert ( + response["wallet_transactions"][1].lago_id + == "b7ab2926-1de8-4428-9bcd-779314ac1222" + ) def test_invalid_create_wallet_transaction_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/wallet_transactions', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/wallet_transactions", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.wallet_transactions.create(wallet_transaction_object()) def test_valid_find_all_wallet_transactions_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/wallets/555/wallet_transactions', content=mock_collection_response()) - response = client.wallet_transactions.find_all('555') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/wallets/555/wallet_transactions", + content=mock_collection_response(), + ) + response = client.wallet_transactions.find_all("555") - assert response['wallet_transactions'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac1111' - assert response['meta']['current_page'] == 1 + assert ( + response["wallet_transactions"][0].lago_id + == "b7ab2926-1de8-4428-9bcd-779314ac1111" + ) + assert response["meta"]["current_page"] == 1 diff --git a/tests/test_webhook_client.py b/tests/test_webhook_client.py index ffaac6c1..b33e1d73 100644 --- a/tests/test_webhook_client.py +++ b/tests/test_webhook_client.py @@ -9,25 +9,34 @@ def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/webhook.json') + data_path = os.path.join(this_dir, "fixtures/webhook.json") - with open(data_path, 'rb') as webhook_response: + with open(data_path, "rb") as webhook_response: return webhook_response.read() def test_valid_public_key_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhooks/json_public_key', content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhooks/json_public_key", + content=mock_response(), + ) response = client.webhooks.public_key() - assert response == b'key' + assert response == b"key" def test_invalid_public_key_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhooks/json_public_key', status_code=401, content=b'') + client = Client(api_key="invalid") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhooks/json_public_key", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.webhooks.public_key() diff --git a/tests/test_webhook_endpoint_client.py b/tests/test_webhook_endpoint_client.py index 6e80b696..d6c60ec8 100644 --- a/tests/test_webhook_endpoint_client.py +++ b/tests/test_webhook_endpoint_client.py @@ -9,119 +9,165 @@ def webhook_endpoint_object(): - return WebhookEndpoint( - webhook_url='https://foo.bar', - signature_algo='hmac' - ) + return WebhookEndpoint(webhook_url="https://foo.bar", signature_algo="hmac") def mock_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/webhook_endpoint.json') + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint.json") - with open(data_path, 'rb') as webhook_endpoint_response: + with open(data_path, "rb") as webhook_endpoint_response: return webhook_endpoint_response.read() def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) - data_path = os.path.join(this_dir, 'fixtures/webhook_endpoint_index.json') + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_index.json") - with open(data_path, 'rb') as webhook_endpoint_response: + with open(data_path, "rb") as webhook_endpoint_response: return webhook_endpoint_response.read() def test_valid_create_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/webhook_endpoints', content=mock_response()) + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_response(), + ) response = client.webhook_endpoints.create(webhook_endpoint_object()) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_create_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='POST', url='https://api.getlago.com/api/v1/webhook_endpoints', status_code=401, content=b'') + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/webhook_endpoints", + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.webhook_endpoints.create(webhook_endpoint_object()) def test_valid_update_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, content=mock_response()) + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_response(), + ) response = client.webhook_endpoints.update(webhook_endpoint_object(), arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_update_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' - - httpx_mock.add_response(method='PUT', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, status_code=401, content=b'') + client = Client(api_key="invalid") + arg = "invalid" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + status_code=401, + content=b"", + ) with pytest.raises(LagoApiError): client.webhook_endpoints.update(webhook_endpoint_object(), arg) def test_valid_find_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, content=mock_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_response(), + ) response = client.webhook_endpoints.find(arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_find_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' - - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, status_code=404, content=b'') + client = Client(api_key="invalid") + arg = "invalid" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.webhook_endpoints.find(arg) def test_valid_destroy_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') - arg = 'b7ab2926-1de8-4428-9bcd-779314ac129b' + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, content=mock_response()) + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_response(), + ) response = client.webhook_endpoints.destroy(arg) - assert response.lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' + assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" def test_invalid_destroy_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') - arg = 'invalid' - - httpx_mock.add_response(method='DELETE', url='https://api.getlago.com/api/v1/webhook_endpoints/' + arg, status_code=404, content=b'') + client = Client(api_key="invalid") + arg = "invalid" + + httpx_mock.add_response( + method="DELETE", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.webhook_endpoints.destroy(arg) def test_valid_find_all_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhook_endpoints', content=mock_collection_response()) + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_collection_response(), + ) response = client.webhook_endpoints.find_all() - assert response['webhook_endpoints'][0].lago_id == 'b7ab2926-1de8-4428-9bcd-779314ac129b' - assert response['meta']['current_page'] == 1 + assert ( + response["webhook_endpoints"][0].lago_id + == "b7ab2926-1de8-4428-9bcd-779314ac129b" + ) + assert response["meta"]["current_page"] == 1 + def test_invalid_find_all_webhook_endpoint_request(httpx_mock: HTTPXMock): - client = Client(api_key='invalid') + client = Client(api_key="invalid") - httpx_mock.add_response(method='GET', url='https://api.getlago.com/api/v1/webhook_endpoints', status_code=404, content=b'') + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + status_code=404, + content=b"", + ) with pytest.raises(LagoApiError): client.webhook_endpoints.find_all()