Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyCA committed Sep 25, 2022
1 parent 86bd6d4 commit 781db62
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ def test_get_available_name(self):
self.storage.overwrite_files = False
client_mock = mock.MagicMock()
client_mock.exists.side_effect = [True, False]
custom_client_mock = mock.MagicMock()
self.storage._client.get_blob_client.return_value = client_mock
self.storage._custom_client.get_blob_client.return_value = custom_client_mock
name = self.storage.get_available_name('foo.txt')
self.assertTrue(name.startswith('foo_'))
self.assertTrue(name.endswith('.txt'))
self.assertTrue(len(name) > len('foo.txt'))
self.assertEqual(client_mock.exists.call_count, 2)
self.assertEqual(custom_client_mock.exists.call_count, 0)

def test_get_available_name_first(self):
self.storage.overwrite_files = False
Expand Down Expand Up @@ -228,19 +231,17 @@ def test_container_client_params_account_key(self):
'storages.backends.azure_storage.BlobServiceClient',
autospec=True) as bsc_mocked:
client_mock = mock.MagicMock()
custom_client_mock = mock.MagicMock()
bsc_mocked.return_value.get_container_client.return_value = client_mock
self.assertEqual(storage.client, client_mock)
bsc_mocked.assert_called_once_with(
'https://foo_name.blob.core.windows.net',
credential={'account_name': 'foo_name', 'account_key': 'foo_key'})

with mock.patch(
'storages.backends.azure_storage.BlobServiceClient',
autospec=True) as custom_bsc_mocked:
custom_client_mock = mock.MagicMock()
custom_bsc_mocked.return_value.get_container_client.return_value = custom_client_mock
bsc_mocked.return_value.get_container_client.return_value = custom_client_mock
self.assertEqual(storage.custom_client, custom_client_mock)
custom_bsc_mocked.assert_called_once_with(
self.assertEqual(bsc_mocked.call_count, 2)
bsc_mocked.assert_called_with(
'https://foo_domain',
credential={'account_name': 'foo_name', 'account_key': 'foo_key'})

Expand All @@ -254,19 +255,17 @@ def test_container_client_params_sas_token(self):
'storages.backends.azure_storage.BlobServiceClient',
autospec=True) as bsc_mocked:
client_mock = mock.MagicMock()
custom_client_mock = mock.MagicMock()
bsc_mocked.return_value.get_container_client.return_value = client_mock
self.assertEqual(storage.client, client_mock)
bsc_mocked.assert_called_once_with(
'http://foo_name.blob.core.windows.net',
credential='foo_token')

with mock.patch(
'storages.backends.azure_storage.BlobServiceClient',
autospec=True) as custom_bsc_mocked:
custom_client_mock = mock.MagicMock()
custom_bsc_mocked.return_value.get_container_client.return_value = custom_client_mock
bsc_mocked.return_value.get_container_client.return_value = custom_client_mock
self.assertEqual(storage.custom_client, custom_client_mock)
custom_bsc_mocked.assert_called_once_with(
self.assertEqual(bsc_mocked.call_count, 2)
bsc_mocked.assert_called_with(
'http://foo_domain',
credential='foo_token')

Expand Down Expand Up @@ -318,6 +317,7 @@ def test_storage_save(self):
content_type='text/plain',
content_encoding=None,
cache_control=None)
self.storage._custom_client.upload_blob.assert_not_called()

def test_storage_open_write(self):
"""
Expand All @@ -337,18 +337,23 @@ def test_storage_open_write(self):
max_concurrency=2,
timeout=20,
overwrite=True)
self.storage._custom_client.upload_blob.assert_not_called()

def test_storage_exists(self):
blob_name = "blob"
client_mock = mock.MagicMock()
custom_client_mock = mock.MagicMock()
self.storage._client.get_blob_client.return_value = client_mock
self.storage._custom_client.get_blob_client.return_value = client_mock
self.assertTrue(self.storage.exists(blob_name))
self.assertEqual(client_mock.exists.call_count, 1)
self.assertEqual(custom_client_mock.exists.call_count, 0)

def test_delete_blob(self):
self.storage.delete("name")
self.storage._client.delete_blob.assert_called_once_with(
"name", timeout=20)
self.storage._custom_client.delete_blob.assert_not_called()

def test_storage_listdir_base(self):
file_names = ["some/path/1.txt", "2.txt", "other/path/3.txt", "4.txt"]
Expand All @@ -363,6 +368,7 @@ def test_storage_listdir_base(self):
dirs, files = self.storage.listdir("")
self.storage._client.list_blobs.assert_called_with(
name_starts_with="", timeout=20)
self.storage._custom_client.list_blobs.assert_not_called()

self.assertEqual(len(dirs), 2)
for directory in ["some", "other"]:
Expand All @@ -385,6 +391,7 @@ def test_storage_listdir_subdir(self):
obj.name = p
result.append(obj)
self.storage._client.list_blobs.return_value = iter(result)
self.storage._custom_client.list_blobs.assert_not_called()

dirs, files = self.storage.listdir("some/")
self.storage._client.list_blobs.assert_called_with(
Expand Down Expand Up @@ -444,3 +451,37 @@ def test_override_init_argument(self):
self.assertEqual(storage.azure_container, 'foo1')
storage = azure_storage.AzureStorage(azure_container='foo2')
self.assertEqual(storage.azure_container, 'foo2')

@mock.patch('storages.backends.azure_storage.AzureStorage._get_service_client',)
def test_get_service_client_use_custom_domain(self, gsc_mocked):
storage = azure_storage.AzureStorage()
storage.account_name = self.account_name

_ = storage.service_client
gsc_mocked.assert_called_once_with(use_custom_domain=False)

_ = storage.custom_service_client
gsc_mocked.assert_called_with(use_custom_domain=True)

def test_blobserviceclient_no_custom_domain(self):
storage = azure_storage.AzureStorage()
storage.account_name = 'foo_name'
storage.custom_domain = None
storage.account_key = 'foo_key'
with mock.patch(
'storages.backends.azure_storage.BlobServiceClient',
autospec=True) as bsc_mocked:
client_mock = mock.MagicMock()
custom_client_mock = mock.MagicMock()
bsc_mocked.return_value.get_container_client.return_value = client_mock
self.assertEqual(storage.client, client_mock)
bsc_mocked.assert_called_once_with(
'https://foo_name.blob.core.windows.net',
credential={'account_name': 'foo_name', 'account_key': 'foo_key'})

bsc_mocked.return_value.get_container_client.return_value = custom_client_mock
self.assertEqual(storage.custom_client, custom_client_mock)
self.assertEqual(bsc_mocked.call_count, 2)
bsc_mocked.assert_called_with(
'https://foo_name.blob.core.windows.net',
credential={'account_name': 'foo_name', 'account_key': 'foo_key'})

0 comments on commit 781db62

Please sign in to comment.