Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

🚀 Raise ValueError if response is empty in CouldForCustomers source #900

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
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
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 rasie ValueError if response from data source is empty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description doesn't match with code modification

### 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 the cloud for customers for specified parameters is empty!",
):
endpoint = "ServiceRequestCollection"
c4c = CloudForCustomers(endpoint=endpoint, params={"$top": "2"})
df = c4c.to_df(
fields=["ProductRecipientPartyName", "CreationDateTime", "CreatedBy"]
)
6 changes: 5 additions & 1 deletion viadot/sources/cloud_for_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def _to_records_report(self, url: str) -> List[Dict[str, Any]]:
records.extend(new_records)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return records

def _to_records_other(self, url: str) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -222,6 +221,11 @@ def to_df(
"""
records = self.to_records()
df = pd.DataFrame(data=records, **kwargs)

if df.empty:
raise ValueError(
"Response from cloud for customers for specified parameters were empty!"
)
if dtype:
df = df.astype(dtype)
if fields:
Expand Down
Loading