Skip to content

Commit

Permalink
🚀 Added new error handling if response is empty with tests and entry …
Browse files Browse the repository at this point in the history
…in changelog
  • Loading branch information
adrian-wojcik committed May 8, 2024
1 parent 1cb1a4b commit 4fb0132
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Changed

- Changed `CloudForCustomers` methods: `_to_records_report` and `_to_records_other` to handle empty response in proper way
### Removed


Expand Down
28 changes: 28 additions & 0 deletions tests/integration/test_cloud_for_customers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from viadot.sources import CloudForCustomers
from unittest import mock


def test_credentials():
Expand All @@ -8,3 +10,29 @@ def test_credentials():
fields=["ProductRecipientPartyName", "CreationDateTime", "CreatedBy"]
)
assert df.empty == False


class MockResponse:
def __init__(
self,
):
self.json_data = {"d": {"results": []}}
self.status_code = 200

def json(self):
return self.json_data


@mock.patch(
"viadot.sources.CloudForCustomers.get_response", return_value=MockResponse()
)
def test_c4c_empty_response(mock_get):
with pytest.raises(
ValueError,
match="Response from clud for customers for specified parameters were empty!",
):
endpoint = "ServiceRequestCollection"
c4c = CloudForCustomers(endpoint=endpoint, params={"$top": "2"})
df = c4c.to_df(
fields=["ProductRecipientPartyName", "CreationDateTime", "CreatedBy"]
)
11 changes: 11 additions & 0 deletions viadot/sources/cloud_for_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def _to_records_report(self, url: str) -> List[Dict[str, Any]]:

url = response_json["d"].get("__next")

if not response_json.get("d").get("results"):
raise ValueError(
"Response from clud for customers for specified parameters were empty!"
)

return records

def _to_records_other(self, url: str) -> List[Dict[str, Any]]:
Expand All @@ -116,6 +121,12 @@ def _to_records_other(self, url: str) -> List[Dict[str, Any]]:
tmp_params = None
tmp_full_url = url
records.extend(new_records)

if not response_json.get("d").get("results"):
raise ValueError(
"Response from clud for customers for specified parameters were empty!"
)

return records

def to_records(self) -> List[Dict[str, Any]]:
Expand Down

0 comments on commit 4fb0132

Please sign in to comment.