Skip to content

Commit

Permalink
Bugfix/handle empty archives (#421)
Browse files Browse the repository at this point in the history
* distinguish between `{"data": []}"` and `{"no_data": "something"}` in response

* add a test

* clearer logic

* Changelog
  • Loading branch information
timabrmsn committed Apr 27, 2022
1 parent d4e456c commit 3167d8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
- `add_cloud_aliases()`
- `delete_cloud_aliases()`

### Fixed

- Bug where attempting to restore from an empty archive would throw a confusing `TypeError`, we now raise appropriate `Py42ArchiveFileNotFoundError`.

## 1.22.0 - 2022-04-01

### Added
Expand Down
5 changes: 4 additions & 1 deletion src/py42/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def _data_root(self):
if not self._data:
response_dict = json.loads(self._response.text)
if type(response_dict) == dict:
self._data = response_dict.get("data") or response_dict
if "data" in response_dict:
self._data = response_dict["data"]
else:
self._data = response_dict
else:
self._data = response_dict
except ValueError:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

JSON_LIST_NO_DATA_NODE = '{"item_list_key": [{"foo": "foo_val"}, {"bar": "bar_val"}]}'
JSON_DICT_NO_DATA_NODE = '{"item_list_key": {"foo": "foo_val", "bar": "bar_val"}}'
JSON_DICT_EMPTY_DATA_NODE = '{"data": []}'

PLAIN_TEXT = "TEST_PLAIN_TEXT"

Expand Down Expand Up @@ -44,6 +45,13 @@ def mock_response_dict_no_data_node(self, mocker):
mock_response.text = JSON_DICT_NO_DATA_NODE
return mock_response

@pytest.fixture
def mock_response_dict_empty_data_node(self, mocker):
mock_response = mocker.MagicMock(spec=Response)
mock_response.content = JSON_DICT_EMPTY_DATA_NODE.encode("utf-8")
mock_response.text = JSON_DICT_EMPTY_DATA_NODE
return mock_response

@pytest.fixture
def mock_response_not_json(self, mocker):
mock_response = mocker.MagicMock(spec=Response)
Expand Down Expand Up @@ -77,6 +85,12 @@ def test_getitem_returns_dict_keys_no_data_node(
response = Py42Response(mock_response_dict_no_data_node)
assert type(response["item_list_key"]) == dict

def test_getitem_returns_empty_list_empty_data_node(
self, mock_response_dict_empty_data_node
):
response = Py42Response(mock_response_dict_empty_data_node)
assert response.data == []

def test_setitem_modifies_dict_keys_with_data_node_to_expected_value(
self, mock_response_dict_data_node
):
Expand Down

0 comments on commit 3167d8a

Please sign in to comment.