diff --git a/CHANGELOG.md b/CHANGELOG.md index fb82fdead..73f3cd709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### Removed diff --git a/tests/integration/test_cloud_for_customers.py b/tests/integration/test_cloud_for_customers.py index aa4404215..512410646 100644 --- a/tests/integration/test_cloud_for_customers.py +++ b/tests/integration/test_cloud_for_customers.py @@ -1,4 +1,6 @@ +import pytest from viadot.sources import CloudForCustomers +from unittest import mock def test_credentials(): @@ -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"] + ) diff --git a/viadot/sources/cloud_for_customers.py b/viadot/sources/cloud_for_customers.py index 1bb68375e..bffb1a9b0 100644 --- a/viadot/sources/cloud_for_customers.py +++ b/viadot/sources/cloud_for_customers.py @@ -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") - return records def _to_records_other(self, url: str) -> List[Dict[str, Any]]: @@ -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: