Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

# Version 0.0.24
- Updated unit test cases pipeline
- Added support to override uploaded blob access tier

# Version 0.0.23
- Updated unit test cases pipeline
- Added support to upload test cases results on Azure blob
Expand Down
2 changes: 1 addition & 1 deletion src/python_ms_core/core/storage/abstract/file_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_body_text(self):
pass

@abstractmethod
def upload(self, upload_stream):
def upload(self, upload_stream, overwrite=False):
pass

@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def get_body_text(self):
return self.blob_client.download_blob().content_as_text()

@ExceptionHandler.decorated
def upload(self, upload_stream):
upload_file = self.blob_client.upload_blob(self.file_path, upload_stream)
def upload(self, upload_stream, overwrite=False):
upload_file = self.blob_client.upload_blob(
self.file_path, upload_stream, overwrite=overwrite
)
self._get_remote_url = upload_file.url

@ExceptionHandler.decorated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_body_text(self):
return StringIO(self.get_stream()).read()

@ExceptionHandler.decorated
def upload(self, upload_stream):
def upload(self, upload_stream, overwrite=False):
upload_path = f'{self.path}/{self.name}'
upload_relative_path = f'{self.config.connection_string}{self.upload_path}{upload_path}'
requests.post(upload_relative_path, files={'uploadFile': upload_stream})
Expand Down
2 changes: 1 addition & 1 deletion src/python_ms_core/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.23'
__version__ = '0.0.24'
34 changes: 31 additions & 3 deletions tests/unit_tests/test_storage/test_azure_file_entity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from unittest.mock import MagicMock
from azure.storage.blob import BlobClient
from azure.core.exceptions import ResourceExistsError
from src.python_ms_core.core.resource_errors.errors import UnProcessableError
from src.python_ms_core.core.storage.providers.azure.azure_file_entity import AzureFileEntity


Expand Down Expand Up @@ -41,7 +42,7 @@ def test_get_body_text(self):
self.assertEqual(result, 'Test content')
self.blob_client.download_blob.assert_called_once()

def test_upload(self):
def test_upload_without_overwrite(self):
# Create an instance of the AzureFileEntity
file_entity = AzureFileEntity(self.name, self.blob_client)

Expand All @@ -56,7 +57,34 @@ def test_upload(self):

# Assert the result
self.assertEqual(file_entity._get_remote_url, 'http://example.com/file')
self.blob_client.upload_blob.assert_called_once_with(file_entity.file_path, upload_stream)
self.blob_client.upload_blob.assert_called_once_with(file_entity.file_path, upload_stream, overwrite=False)

def test_upload_with_overwrite_true(self):
file_entity = AzureFileEntity(self.name, self.blob_client)

mock_upload_blob = MagicMock()
mock_upload_blob.url = 'http://example.com/file'
self.blob_client.upload_blob.return_value = mock_upload_blob

upload_stream = b'Test content'
file_entity.upload(upload_stream, overwrite=True)

self.assertEqual(file_entity._get_remote_url, 'http://example.com/file')
self.blob_client.upload_blob.assert_called_once_with(
file_entity.file_path, upload_stream, overwrite=True
)

def test_upload_without_overwrite_raises_error_when_blob_exists(self):
file_entity = AzureFileEntity(self.name, self.blob_client)
self.blob_client.upload_blob.side_effect = ResourceExistsError("already exists")

with self.assertRaises(UnProcessableError):
file_entity.upload(b'Test content')

self.blob_client.upload_blob.assert_called_once_with(
file_entity.file_path, b'Test content', overwrite=False
)
self.assertIsNone(file_entity._get_remote_url)

def test_get_remote_url(self):
# Create an instance of the AzureFileEntity
Expand Down