Skip to content

Commit

Permalink
Refactor(Client_Services): updated the function update_client to be l…
Browse files Browse the repository at this point in the history
…ess redundant with handling the parameters of the clients data
  • Loading branch information
MoMo2Win committed Nov 20, 2024
1 parent 1d93995 commit 249ec6d
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 65 deletions.
1 change: 0 additions & 1 deletion example/list_invoices.py

This file was deleted.

Binary file modified myfinances/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified myfinances/clients/__pycache__/service.cpython-312.pyc
Binary file not shown.
53 changes: 24 additions & 29 deletions myfinances/clients/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def create_client(
return MyFinancesResponse(**response.dict())

def get_client(self, client_id: int) -> MyFinancesResponse[Client]:
"""
Retrieving a singular client via their client_id.
Args:
client_id (int): The ID of the client to retrieve.
Returns:
MyFinancesResponse[Client]: Data of the client's details.
"""
response = self._client._get(f"/clients/{client_id}")
return MyFinancesResponse(**response.dict())

Expand All @@ -62,38 +72,23 @@ def update_client(self,
email: Optional[EmailStr] = None,
company: Optional[str] = None,
contact_method: Optional[str] = None,
is_representative: bool = None,
is_representative: Optional[bool] = None,
address: Optional[str] = None,
city: Optional[str] = None,
country: Optional[str] = None) -> MyFinancesResponse[Client]:
params = {}

if name is not None:
params["name"] = name

if phone_number is not None:
params["phone_number"] = phone_number

if email is not None:
params["email"] = email

if company is not None:
params["company"] = company

if contact_method is not None:
params["contact_method"] = contact_method

if is_representative is not None:
params["is_representative"] = is_representative

if address is not None:
params["address"] = address

if city is not None:
params["city"] = city

if country is not None:
params["country"] = country
params = {
key: value for key, value in{
"name": name,
"phone_number": phone_number,
"email": email,
"company": company,
"contact_method": contact_method,
"is_representative": is_representative,
"address": address,
"city": city,
"country": country,
}.items() if value is not None
}

response = self._client._patch("/clients/update/", json=params)
return MyFinancesResponse(**response.dict())
Expand Down
1 change: 0 additions & 1 deletion myfinances/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class Meta(BaseModel):
success: bool
status_code: int
message: str
content: dict = None


Expand Down
Binary file modified tests/__pycache__/test_clients.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_finance.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_receipts.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
13 changes: 1 addition & 12 deletions tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_create_client(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": {"client_id": 123}
}
Expand All @@ -45,11 +44,10 @@ def test_create_client(clients_service, mock_client):

response = clients_service.create_client(**clients_data)

mock_client._post.assert_called_once_with("/clients/create/", clients_data)
mock_client._post.assert_called_once_with("/clients/create/", json=clients_data)
assert response.data["client_id"] == 123
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"


def test_list_clients(clients_service, mock_client):
Expand All @@ -66,7 +64,6 @@ def test_list_clients(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": clients_data
}
Expand All @@ -80,7 +77,6 @@ def test_list_clients(clients_service, mock_client):
mock_client._get.assert_called_once_with("/clients/", params={})
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

assert isinstance(response.data, list)
assert len(response.data) == 2
Expand All @@ -105,7 +101,6 @@ def test_client_by_id(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": clients_data
}
Expand All @@ -120,7 +115,6 @@ def test_client_by_id(clients_service, mock_client):
mock_client._get.assert_called_once_with(f"/clients/{clients_id}")
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

assert response.data["id"] == clients_data["id"]
assert response.data["name"] == clients_data["name"]
Expand All @@ -142,7 +136,6 @@ def test_delete_clients(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": clients_data
}
Expand All @@ -157,7 +150,6 @@ def test_delete_clients(clients_service, mock_client):
mock_client._delete.assert_called_once_with(f"/clients/{Client_Id}/delete")
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

remaining_clients = [clients for clients in clients_data if clients["id"] == Client_Id]
assert len(remaining_clients) == 1
Expand All @@ -182,7 +174,6 @@ def test_update_clients_name(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": clients_data
}
Expand All @@ -197,7 +188,6 @@ def test_update_clients_name(clients_service, mock_client):
mock_client._patch.assert_called_once_with(f"/clients/update/", json={"name": new_name})
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

# Updated Version
updated_clients_data = {
Expand All @@ -217,7 +207,6 @@ def test_update_clients_name(clients_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": updated_clients_data
}
Expand Down
13 changes: 0 additions & 13 deletions tests/test_finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_create_invoice(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Successfully created"
},
"data": {
"invoice_id": 102,
Expand All @@ -44,7 +43,6 @@ def test_create_invoice(invoices_service, mock_client):
assert response.data["invoice_id"] == 102
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Successfully created"


def test_list_invoices(invoices_service, mock_client):
Expand All @@ -58,7 +56,6 @@ def test_list_invoices(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Successfully fetched invoices"
},
"data": invoice_list
}
Expand All @@ -73,7 +70,6 @@ def test_list_invoices(invoices_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Successfully fetched invoices"

assert isinstance(response.data, list)
assert len(response.data) == 2
Expand All @@ -93,7 +89,6 @@ def test_delete_invoice(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Successfully deleted invoice"
},
"data": invoice_list
}
Expand All @@ -111,7 +106,6 @@ def test_delete_invoice(invoices_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Successfully deleted invoice"

remaining_invoices = [invoice for invoice in invoice_list if invoice['id'] != invoice_id]

Expand All @@ -132,7 +126,6 @@ def test_get_invoice(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Successfully deleted invoice"
},
"data": invoice_data
}
Expand All @@ -148,7 +141,6 @@ def test_get_invoice(invoices_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Successfully deleted invoice"

assert response.data["id"] == invoice_data["id"]
assert response.data["customer_id"] == invoice_data["customer_id"]
Expand All @@ -167,7 +159,6 @@ def test_search_invoices_by_id(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Invoices fetched successfully"
},
"data": invoice_data
}
Expand All @@ -183,7 +174,6 @@ def test_search_invoices_by_id(invoices_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Invoices fetched successfully"

assert response.data[1]["id"] == 2
assert response.data[1]["customer_id"] == 124
Expand All @@ -203,7 +193,6 @@ def test_update_invoice(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Invoices successfully updated"
},
"data": invoice_data
}
Expand All @@ -220,7 +209,6 @@ def test_update_invoice(invoices_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Invoices successfully updated"

# updated version
updated_invoice_data = {
Expand All @@ -236,7 +224,6 @@ def test_update_invoice(invoices_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Invoice fetched successfully"
},
"data": updated_invoice_data
}
Expand Down
10 changes: 1 addition & 9 deletions tests/test_receipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from myfinances import MyFinancesClient
from myfinances.finance.receipts.service import ReceiptService


@pytest.fixture
def mock_client():
mock = Mock(spec=MyFinancesClient)
Expand Down Expand Up @@ -30,7 +31,6 @@ def test_create_receipt(receipts_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": receipts_data
}
Expand All @@ -44,7 +44,6 @@ def test_create_receipt(receipts_service, mock_client):
mock_client._post.assert_called_once_with("/receipts/create/", json=receipts_data)
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

assert response.data["name"] == receipts_data["name"]
assert response.data["image"] == receipts_data["image"]
Expand All @@ -62,7 +61,6 @@ def test_list_receipts(receipts_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": list_of_receipts
}
Expand All @@ -71,12 +69,10 @@ def test_list_receipts(receipts_service, mock_client):
mock_response.dict.return_value = mock_response_data
mock_client._get.return_value = mock_response


response = receipts_service.list_receipts()
mock_client._get.assert_called_once_with("/receipts/")
assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

assert isinstance(response.data, list)
assert len(response.data) == 2
Expand All @@ -94,7 +90,6 @@ def test_delete_receipt(receipts_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Successfully deleted invoice"
},
"data": list_of_receipts
}
Expand All @@ -112,7 +107,6 @@ def test_delete_receipt(receipts_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Successfully deleted invoice"

remaining_receipts = [receipt for receipt in list_of_receipts if receipt["id"] != receipt_id]
assert len(remaining_receipts) == 1
Expand All @@ -131,7 +125,6 @@ def test_update_receipt(receipts_service, mock_client):
"meta": {
"success": True,
"status_code": 200,
"message": "Success"
},
"data": list_of_receipts
}
Expand All @@ -146,6 +139,5 @@ def test_update_receipt(receipts_service, mock_client):

assert response.meta.success is True
assert response.meta.status_code == 200
assert response.meta.message == "Success"

assert response.data[1]["name"] == search_name

0 comments on commit 249ec6d

Please sign in to comment.