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

Asyncio Listing and Inventory Report Integration #573

Merged
merged 18 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion gcsfs/inventory_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,20 @@ class (see 'core.py').
Exception: If there is an error while fetching the inventory
report configuration.
"""
pass
project = gcs_file_system.project
url = "{}/projects/{}/locations/{}/reportConfigs/{}"
url = url.format(
InventoryReport.BASE_URL,
project,
location,
id
)
try:
raw_inventory_report_config = await gcs_file_system \
._call("GET", url, json_out=True)
return raw_inventory_report_config
except Exception as e:
print(f"Error countered when fetching inventory report config: {e}.")
martindurant marked this conversation as resolved.
Show resolved Hide resolved

def _parse_raw_inventory_report_config(
raw_inventory_report_config, use_snapshot_listing):
Expand Down
41 changes: 40 additions & 1 deletion gcsfs/tests/test_inventory_report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import asyncio
from gcsfs.inventory_report import InventoryReport

class TestInventoryReport(object):
Expand Down Expand Up @@ -32,4 +33,42 @@ def test_validate_inventory_report_info(
# If no error is expected, we simply call the function
# to ensure no exception is raised.
InventoryReport._validate_inventory_report_info(
inventory_report_info=inventory_report_info)
inventory_report_info=inventory_report_info)

@pytest.mark.asyncio
@pytest.mark.parametrize("location, id, exception, expected_result", [
# Test no error fetching proceeds normally.
("us-west", "id1", None, {"config": "config1"}),
# Test if the exception is caught successfully.
("us-west", "id2", Exception("fetch error"), None),
])
async def test_fetch_raw_inventory_report_config(
self, location, id, exception, expected_result, mocker):

# Mocking the gcs_file_system.
gcs_file_system = mocker.MagicMock()
gcs_file_system.project = "project"

# Mocking gcs_file_system._call.
if exception is not None:
gcs_file_system._call = mocker.MagicMock(side_effect=exception)
else:
return_value = asyncio.Future()
return_value.set_result(expected_result)
gcs_file_system._call = mocker.MagicMock(return_value=return_value)

if exception is not None:
with pytest.raises(Exception) as e_info:
await InventoryReport._fetch_raw_inventory_report_config(
gcs_file_system=gcs_file_system,
location=location,
id=id)
assert str(e_info.value) == str(exception)
else:
result = await InventoryReport._fetch_raw_inventory_report_config(
gcs_file_system=gcs_file_system,
location=location,
id=id)
gcs_file_system._call.assert_called_once_with(
"GET", mocker.ANY, json_out=True)
assert result == expected_result