This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* inicia transação! https://docs.python.org/3/library/dataclasses.html * adiciona complexidade de objetos nesteados * adiciona testes * altera o BaseImopayObj para utilizar coisas mais mágicas de datacalss * altera testes * faz algumas refatorações e testes
- Loading branch information
1 parent
ac762d4
commit 4f2e5e1
Showing
16 changed files
with
335 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import List | ||
from dataclasses import dataclass, field | ||
|
||
from .base import BaseImopayObj | ||
|
||
|
||
@dataclass | ||
class BaseTransaction(BaseImopayObj): | ||
payer: str | ||
receiver: str | ||
reference_id: str | ||
amount: int | ||
description: str | ||
|
||
|
||
@dataclass | ||
class Configuration(BaseImopayObj): | ||
value: str | ||
type: str | ||
charge_type: str | ||
days: str | ||
|
||
|
||
@dataclass | ||
class InvoiceConfigurations(BaseImopayObj): | ||
fine: Configuration | ||
interest: Configuration | ||
discounts: List[Configuration] = field(default=list) | ||
|
||
def __post_init__(self): | ||
if isinstance(self.fine, dict): | ||
self.fine = Configuration.from_dict(self.fine) | ||
if isinstance(self.interest, dict): | ||
self.interest = Configuration.from_dict(self.interest) | ||
if self.discounts: | ||
for i, discount in enumerate(self.discounts): | ||
if isinstance(discount, dict): | ||
self.discounts[i] = Configuration.from_dict(discount) | ||
|
||
def to_dict(self): | ||
""" | ||
Por causa do typehint 'List' o to_dict original não funciona! | ||
Ao invés de solucionar isso, mais fácil sobreescrever o método | ||
no momento. | ||
""" | ||
data = { | ||
"fine": self.fine.to_dict(), | ||
"interest": self.interest.to_dict(), | ||
"discounts": [discount.to_dict() for discount in self.discounts], | ||
} | ||
return data | ||
|
||
|
||
@dataclass | ||
class Invoice(BaseImopayObj): | ||
expiration_date: str | ||
limit_date: str | ||
configurations: InvoiceConfigurations = field(default_factory=dict) | ||
|
||
def __post_init__(self): | ||
if isinstance(self.configurations, dict): | ||
self.configurations = InvoiceConfigurations.from_dict(self.configurations) | ||
|
||
|
||
@dataclass | ||
class InvoiceTransaction(BaseTransaction): | ||
payment_method: Invoice | ||
|
||
def __post_init__(self): | ||
if isinstance(self.payment_method, dict): | ||
self.payment_method = Invoice(**self.payment_method) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from .address import AddressWrapper | ||
from .company import CompanyWrapper | ||
from .person import PersonWrapper | ||
from .transaction import TransactionWrapper | ||
|
||
|
||
class ImopayWrapper: | ||
def __init__(self, *args, **kwargs): | ||
self.address = AddressWrapper(*args, **kwargs) | ||
self.company = CompanyWrapper(*args, **kwargs) | ||
self.person = PersonWrapper(*args, **kwargs) | ||
self.transaction = TransactionWrapper(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from .base import BaseImopayWrapper, RetrieveMixin | ||
from ..models.transaction import InvoiceTransaction | ||
|
||
|
||
class TransactionWrapper(BaseImopayWrapper, RetrieveMixin): | ||
""" | ||
Wrapper para os métodos de transaction | ||
""" | ||
|
||
@property | ||
def action(self): | ||
return "transactions" | ||
|
||
def create_invoice(self, data: dict): | ||
instance = InvoiceTransaction(**data) | ||
url = self._construct_url(action=self.action, subaction="create_invoice") | ||
return self._post(url, instance.to_dict()) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from unittest import TestCase | ||
|
||
from imopay_wrapper.models.transaction import Configuration | ||
|
||
|
||
class ConfigurationTestCase(TestCase): | ||
def test_1(self): | ||
t = Configuration.from_dict( | ||
{"value": 1, "type": "foo", "charge_type": "foo", "days": 0} | ||
) | ||
self.assertEqual(t.value, 1) | ||
self.assertEqual(t.type, "foo") | ||
self.assertEqual(t.charge_type, "foo") | ||
self.assertEqual(t.days, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from unittest import TestCase | ||
|
||
from imopay_wrapper.models.transaction import Invoice | ||
|
||
|
||
class InvoiceTestCase(TestCase): | ||
def test_1(self): | ||
t = Invoice.from_dict( | ||
{ | ||
"configurations": { | ||
"fine": { | ||
"value": 1, | ||
"type": "foo", | ||
"charge_type": "foo", | ||
"days": 0, | ||
}, | ||
"interest": { | ||
"value": 1, | ||
"type": "foo", | ||
"charge_type": "foo", | ||
"days": 0, | ||
}, | ||
"discounts": [ | ||
{"value": 1, "type": "foo", "charge_type": "foo", "days": 0} | ||
], | ||
} | ||
} | ||
) | ||
self.assertEqual(t.configurations.fine.value, 1) | ||
self.assertEqual(t.configurations.interest.value, 1) | ||
self.assertEqual(t.configurations.discounts[0].value, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest import TestCase | ||
|
||
from imopay_wrapper.models.transaction import InvoiceConfigurations, Configuration | ||
|
||
|
||
class InvoiceConfigurationsTestCase(TestCase): | ||
def test_1(self): | ||
t = InvoiceConfigurations.from_dict( | ||
{ | ||
"fine": Configuration.from_dict( | ||
{"value": 1, "type": "foo", "charge_type": "foo", "days": 0} | ||
) | ||
} | ||
) | ||
self.assertEqual(t.fine.value, 1) | ||
|
||
def test_2(self): | ||
t = InvoiceConfigurations.from_dict( | ||
{"fine": {"value": 1, "type": "foo", "charge_type": "foo", "days": 0}} | ||
) | ||
self.assertEqual(t.fine.value, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from unittest import TestCase | ||
|
||
from imopay_wrapper.models.transaction import InvoiceTransaction, BaseTransaction | ||
|
||
|
||
class InvoiceTransactionTestCase(TestCase): | ||
def test_0(self): | ||
expected = set(BaseTransaction.get_fields().keys()) | ||
|
||
result = set(InvoiceTransaction.get_fields().keys()) | ||
|
||
for item in expected: | ||
with self.subTest(item): | ||
self.assertIn(item, result) | ||
|
||
def test_1(self): | ||
t = InvoiceTransaction.from_dict({}) | ||
self.assertEqual(t.payer, None) | ||
|
||
def test_2(self): | ||
t = InvoiceTransaction.from_dict( | ||
{ | ||
"payment_method": { | ||
"expiration_date": "1", | ||
"limit_date": "2", | ||
"configurations": { | ||
"fine": { | ||
"value": 1, | ||
"type": "foo", | ||
"charge_type": "foo", | ||
"days": 0, | ||
}, | ||
"interest": { | ||
"value": 1, | ||
"type": "foo", | ||
"charge_type": "foo", | ||
"days": 0, | ||
}, | ||
"discounts": [ | ||
{"value": 1, "type": "foo", "charge_type": "foo", "days": 0} | ||
], | ||
}, | ||
} | ||
} | ||
) | ||
self.assertEqual(t.payment_method.configurations.fine.value, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unittest import TestCase | ||
|
||
from imopay_wrapper.models.transaction import BaseTransaction | ||
|
||
|
||
class BaseTransactionTestCase(TestCase): | ||
def test_(self): | ||
t = BaseTransaction.from_dict({}) | ||
self.assertEqual(t.payer, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.