Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix charge list endpoint and reponse models #16

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions barte/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Buyer,
BuyerList,
Order,
ChargeList,
)


Expand Down Expand Up @@ -67,15 +68,14 @@ def get_charge(self, charge_id: str) -> Charge:
response.raise_for_status()
return from_dict(data_class=Charge, data=response.json(), config=DACITE_CONFIG)

def list_charges(self, params: Optional[Dict[str, Any]] = None) -> List[Charge]:
def list_charges(self, params: Optional[Dict[str, Any]] = None) -> ChargeList:
"""List all charges with optional filters"""
endpoint = f"{self.base_url}/v1/charges"
endpoint = f"{self.base_url}/v2/charges"
response = requests.get(endpoint, headers=self.headers, params=params)
response.raise_for_status()
return [
from_dict(data_class=Charge, data=item, config=DACITE_CONFIG)
for item in response.json()["data"]
]
return from_dict(
data_class=ChargeList, data=response.json(), config=DACITE_CONFIG
)

def cancel_charge(self, charge_id: str) -> None:
"""Cancel a specific charge"""
Expand Down
14 changes: 12 additions & 2 deletions barte/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class Charge:
uuid: str
title: str
expirationDate: datetime
paidDate: datetime
value: float
paymentMethod: str
status: str
customer: ChargerCustomer
paidDate: Optional[datetime]
authorizationCode: Optional[str]
authorizationNsu: Optional[str]

Expand Down Expand Up @@ -190,7 +190,7 @@ class Pageable:


@dataclass
class BuyerList:
class BaseListReponse:
content: List[Buyer]
pageable: Pageable
totalPages: int
Expand All @@ -202,3 +202,13 @@ class BuyerList:
sort: SortInfo
first: bool
empty: bool


@dataclass
class BuyerList(BaseListReponse):
content: List[Buyer]


@dataclass
class ChargeList(BaseListReponse):
content: List[Charge]
41 changes: 33 additions & 8 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ def mock_pix_charge_response():
}


@pytest.fixture
def mock_list_response():
return {
"content": [],
"pageable": {
"sort": {"unsorted": True, "sorted": False, "empty": True},
"pageNumber": 0,
"pageSize": 20,
"offset": 0,
"paged": True,
"unpaged": False,
},
"totalElements": 1,
"totalPages": 1,
"last": False,
"numberOfElements": 20,
"size": 20,
"number": 0,
"sort": {"unsorted": True, "sorted": False, "empty": True},
"first": True,
"empty": False,
}


class TestBarteClient:
def test_client_initialization(self):
"""Test client initialization with valid environment"""
Expand Down Expand Up @@ -350,29 +374,30 @@ def test_get_charge(self, mock_get, barte_client, mock_charge_response):
)

@patch("requests.get")
def test_list_charges(self, mock_get, barte_client, mock_charge_response):
def test_list_charges(
self, mock_get, barte_client, mock_charge_response, mock_list_response
):
"""Test listing all charges"""
mock_response = {
"data": [
**mock_list_response,
"content": [
mock_charge_response,
{**mock_charge_response, "id": "chr_987654321"},
],
"has_more": False,
}
mock_get.return_value.json.return_value = mock_response
mock_get.return_value.raise_for_status = Mock()

params = {"limit": 2, "starting_after": "chr_0"}
charges = barte_client.list_charges(params)
params = {"customerDocument": "19340911032"}
charges = barte_client.list_charges(params).content

assert len(charges) == 2
assert len(charges) == 1
assert all(isinstance(charge, Charge) for charge in charges)
assert charges[0].uuid == "8b6b2ddc-7ccb-4d1f-8832-ef0adc62ed31"
assert charges[1].uuid == "8b6b2ddc-7ccb-4d1f-8832-ef0adc62ed31"
assert all(isinstance(charge.paidDate, datetime) for charge in charges)

mock_get.assert_called_once_with(
f"{barte_client.base_url}/v1/charges",
f"{barte_client.base_url}/v2/charges",
headers=barte_client.headers,
params=params,
)
Expand Down
1 change: 0 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading