Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen committed Oct 23, 2023
1 parent 4fe4381 commit 3035037
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 28 deletions.
60 changes: 44 additions & 16 deletions darwin/future/core/items/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
from typing import Dict, List, Tuple, Union

from darwin.datatypes import JSONType
from darwin.future.core.client import ClientCore
from darwin.future.data_objects.item import Item
from darwin.future.data_objects.typing import UnknownType
Expand Down Expand Up @@ -159,7 +158,8 @@ async def async_register_upload(
if isinstance(items_and_paths, tuple):
items_and_paths = [items_and_paths]
assert all(
(isinstance(item, Item) and isinstance(path, Path)) for item, path in items_and_paths
(isinstance(item, Item) and isinstance(path, Path))
for item, path in items_and_paths
), "items must be a list of Items"

payload_items = await _build_payload_items(items_and_paths)
Expand All @@ -177,7 +177,9 @@ async def async_register_upload(
}

try:
response = api_client.post(f"/v2/teams/{team_slug}/items/register_upload", payload)
response = api_client.post(
f"/v2/teams/{team_slug}/items/register_upload", payload
)
except Exception as exc:
logger.error(f"Failed to register upload in {__name__}", exc_info=exc)
raise DarwinException(f"Failed to register upload in {__name__}") from exc
Expand Down Expand Up @@ -210,24 +212,40 @@ async def async_create_signed_upload_url(
The response from the API
"""
try:
response = api_client.post(f"/v2/teams/{team_slug}/items/uploads/{upload_id}/sign", data={})
response = api_client.post(
f"/v2/teams/{team_slug}/items/uploads/{upload_id}/sign", data={}
)
except Exception as exc:
logger.error(f"Failed to create signed upload url in {__name__}", exc_info=exc)
raise DarwinException(f"Failed to create signed upload url in {__name__}") from exc
raise DarwinException(
f"Failed to create signed upload url in {__name__}"
) from exc

assert isinstance(response, dict), "Unexpected return type from create signed upload url"
assert isinstance(
response, dict
), "Unexpected return type from create signed upload url"

if not response:
logger.error(f"Failed to create signed upload url in {__name__}, got no response")
raise DarwinException(f"Failed to create signed upload url in {__name__}, got no response")
logger.error(
f"Failed to create signed upload url in {__name__}, got no response"
)
raise DarwinException(
f"Failed to create signed upload url in {__name__}, got no response"
)

if "errors" in response:
logger.error(f"Failed to create signed upload url in {__name__}, got errors: {response['errors']}")
logger.error(
f"Failed to create signed upload url in {__name__}, got errors: {response['errors']}"
)
raise DarwinException(f"Failed to create signed upload url in {__name__}")

if "upload_url" not in response:
logger.error(f"Failed to create signed upload url in {__name__}, got no upload_url")
raise DarwinException(f"Failed to create signed upload url in {__name__}, got no upload_url")
logger.error(
f"Failed to create signed upload url in {__name__}, got no upload_url"
)
raise DarwinException(
f"Failed to create signed upload url in {__name__}, got no upload_url"
)

return response["upload_url"]

Expand Down Expand Up @@ -281,12 +299,16 @@ async def async_register_and_create_signed_upload_url(
if "errors" in register or not download_id:
raise DarwinException(f"Failed to register upload in {__name__}")

signed_info = await async_create_signed_upload_url(api_client, team_slug, download_id)
signed_info = await async_create_signed_upload_url(
api_client, team_slug, download_id
)

return signed_info


async def async_confirm_upload(api_client: ClientCore, team_slug: str, upload_id: str) -> None:
async def async_confirm_upload(
api_client: ClientCore, team_slug: str, upload_id: str
) -> None:
"""
Asynchronously confirm an upload/uploads was successful by ID
Expand All @@ -306,16 +328,22 @@ async def async_confirm_upload(api_client: ClientCore, team_slug: str, upload_id
"""

try:
response = api_client.post(f"/v2/teams/{team_slug}/items/uploads/{upload_id}/confirm", data={})
response = api_client.post(
f"/v2/teams/{team_slug}/items/uploads/{upload_id}/confirm", data={}
)
except Exception as exc:
logger.error(f"Failed to confirm upload in {__name__}", exc_info=exc)
raise DarwinException(f"Failed to confirm upload in {__name__}") from exc

assert isinstance(response, dict), "Unexpected return type from confirm upload"

if "errors" in response:
logger.error(f"Failed to confirm upload in {__name__}, got errors: {response['errors']}")
raise DarwinException(f"Failed to confirm upload in {__name__}: {str(response['errors'])}")
logger.error(
f"Failed to confirm upload in {__name__}, got errors: {response['errors']}"
)
raise DarwinException(
f"Failed to confirm upload in {__name__}: {str(response['errors'])}"
)


def register_upload(
Expand Down
49 changes: 37 additions & 12 deletions darwin/future/tests/core/items/test_upload_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ class TestBuildPayloadItems:
)
],
)
def test_build_payload_items(self, items_and_paths: List[Tuple[Item, Path]], expected: List[Dict]) -> None:
def test_build_payload_items(
self, items_and_paths: List[Tuple[Item, Path]], expected: List[Dict]
) -> None:
result = asyncio.run(uploads._build_payload_items(items_and_paths))
result_with_uuid_as_str = [{**item, "id": str(item["id"])} for item in result]

Expand Down Expand Up @@ -553,7 +555,9 @@ def test_async_register_upload_happy_path(
]
mock_build_payload_items.return_value = items

responses.add(responses.POST, f"{default_url}/register_upload", json={"status": "success"})
responses.add(
responses.POST, f"{default_url}/register_upload", json={"status": "success"}
)
asyncio.run(
uploads.async_register_upload(
api_client=base_client,
Expand All @@ -580,7 +584,9 @@ def test_async_register_upload_happy_path(
}, "The request body should be empty"

@patch.object(uploads, "_build_payload_items")
def test_async_register_upload_raises(self, mock_build_payload_items: MagicMock, base_client: ClientCore) -> None:
def test_async_register_upload_raises(
self, mock_build_payload_items: MagicMock, base_client: ClientCore
) -> None:
with pytest.raises(DarwinException) as exc:
mock_build_payload_items.side_effect = DarwinException("Error1")

Expand Down Expand Up @@ -618,7 +624,9 @@ def test_async_register_upload_raises(self, mock_build_payload_items: MagicMock,


class TestCreateSignedUploadUrl(SetupTests):
def test_async_create_signed_upload_url(self, default_url: str, base_config: DarwinConfig) -> None:
def test_async_create_signed_upload_url(
self, default_url: str, base_config: DarwinConfig
) -> None:
with responses.RequestsMock() as rsps:
# Mock the API response
expected_response = {"upload_url": "https://signed.url"}
Expand All @@ -630,20 +638,26 @@ def test_async_create_signed_upload_url(self, default_url: str, base_config: Dar

# Call the function with mocked arguments
api_client = ClientCore(base_config)
actual_response = asyncio.run(uploads.async_create_signed_upload_url(api_client, "1", "my-team"))
actual_response = asyncio.run(
uploads.async_create_signed_upload_url(api_client, "1", "my-team")
)

# Check that the response matches the expected response
if not actual_response:
pytest.fail("Response was None")

assert actual_response == expected_response["upload_url"]

def test_async_create_signed_upload_url_raises(self, base_client: ClientCore) -> None:
def test_async_create_signed_upload_url_raises(
self, base_client: ClientCore
) -> None:
base_client.post = MagicMock() # type: ignore
base_client.post.side_effect = DarwinException("Error")

with pytest.raises(DarwinException):
asyncio.run(uploads.async_create_signed_upload_url(base_client, "1", "my-team"))
asyncio.run(
uploads.async_create_signed_upload_url(base_client, "1", "my-team")
)


class TestRegisterAndCreateSignedUploadUrl:
Expand Down Expand Up @@ -724,7 +738,9 @@ def test_async_register_and_create_signed_upload_url_raises(

class TestConfirmUpload(SetupTests):
@responses.activate
def test_async_confirm_upload(self, base_client: ClientCore, default_url: str) -> None:
def test_async_confirm_upload(
self, base_client: ClientCore, default_url: str
) -> None:
# Call the function with mocked arguments
responses.add(
"POST",
Expand All @@ -744,7 +760,9 @@ def test_async_confirm_upload(self, base_client: ClientCore, default_url: str) -
assert actual_response is None # Function doesn't return anything on success

@responses.activate
def test_async_confirm_upload_raises_on_returned_errors(self, base_client: ClientCore, default_url: str) -> None:
def test_async_confirm_upload_raises_on_returned_errors(
self, base_client: ClientCore, default_url: str
) -> None:
# Call the function with mocked arguments
responses.add(
"POST",
Expand All @@ -762,7 +780,10 @@ def test_async_confirm_upload_raises_on_returned_errors(self, base_client: Clien
)
)

assert "Failed to confirm upload in darwin.future.core.items.uploads: ['error1', 'error2']" in str(exc)
assert (
"Failed to confirm upload in darwin.future.core.items.uploads: ['error1', 'error2']"
in str(exc)
)

def test_async_confirm_upload_raises(self, base_client: ClientCore) -> None:
base_client.post = MagicMock() # type: ignore
Expand All @@ -785,7 +806,9 @@ def mock_async_create_signed_upload_url(self) -> Generator:

@pytest.fixture
def mock_async_register_and_create_signed_upload_url(self) -> Generator:
with patch.object(uploads, "async_register_and_create_signed_upload_url") as mock:
with patch.object(
uploads, "async_register_and_create_signed_upload_url"
) as mock:
yield mock

@pytest.fixture
Expand Down Expand Up @@ -816,7 +839,9 @@ def test_register_and_create_signed_upload_url(
mock_async_register_and_create_signed_upload_url: MagicMock,
base_client: ClientCore,
) -> None:
uploads.register_and_create_signed_upload_url(base_client, "team", "dataset", [(Mock(), Mock())])
uploads.register_and_create_signed_upload_url(
base_client, "team", "dataset", [(Mock(), Mock())]
)

mock_async_register_and_create_signed_upload_url.assert_called_once()

Expand Down

0 comments on commit 3035037

Please sign in to comment.