Skip to content

Commit

Permalink
Merge branch 'main' into emma/add-json-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
emmastephenson authored May 12, 2023
2 parents 160be47 + 6eedc14 commit a1090e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions phdi/cloud/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,20 @@ def list_objects(self, container_name: str, prefix: str = "") -> List[str]:
blob_name_list.append(blob_propreties.name)

return blob_name_list

def blob_exists(self, container_name: str, filename: str) -> bool:
"""
Check if a blob exists within a container given its name and the name of the
container.
:param container_name: The name of the container to look for the blob in.
:param filename: The name of the blob to check the existence of.
:param prefix: Filter the objects returned to filenames beginning
with this value.
:return: A boolean of true if the file exists and false if it does not.
"""

container_location = f"{self.storage_account_url}/{container_name}"
container_client = self._get_container_client(container_location)
blob_client = container_client.get_blob_client(filename)
return blob_client.exists()
28 changes: 28 additions & 0 deletions tests/cloud/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,34 @@ def test_azure_list_objects(mock_get_client):
assert blob_list == ["blob1", "blob2"]


@mock.patch.object(AzureCloudContainerConnection, "_get_container_client")
def test_azure_blob_exists(mock_get_client):
mocked_blob_client = mock.Mock()
mocked_blob_client.exists.return_value = True

mocked_container_client = mock.Mock()
mocked_container_client.get_blob_client.return_value = mocked_blob_client

mock_get_client.return_value = mocked_container_client

object_storage_account = "some-resource-location"
object_container = "some-container-name"
filename = "some-file-name"

mock_cred_manager = mock.Mock()

phdi_container_client = AzureCloudContainerConnection(
object_storage_account, mock_cred_manager
)

exists = phdi_container_client.blob_exists(object_container, filename)

mock_get_client.assert_called_with(f"{object_storage_account}/{object_container}")
mocked_container_client.get_blob_client.assert_called_with(filename)
mocked_blob_client.exists.assert_called()
assert exists is True


def test_gcp_storage_connect_init():
phdi_container_client = GcpCloudStorageConnection()
assert phdi_container_client._GcpCloudStorageConnection__storage_client is None
Expand Down

0 comments on commit a1090e7

Please sign in to comment.