def test_delete_blobs(self): blob_names = ['to/blob1.txt', 'to/blob2.txt', 'to/blob3.txt'] self.azure_handler.delete_blobs('test', blob_names) self.mock_blob_service.delete_blob.assert_has_calls([ mock.call('test', 'to/blob1.txt'), mock.call('test', 'to/blob2.txt'), mock.call('test', 'to/blob3.txt'), ]) self.mock_azure.reset_mock() self.mock_blob_service.delete_blob = mock.Mock( side_effect=AzureMissingResourceHttpError('A', 'B')) # 判断抛错 six.assertCountEqual( self, self.azure_handler.delete_blobs('test', blob_names), [])
def test_upload(self, mock_create_blob, mock_create_container, mock_list_blobs): blob_pairs = [ ('to/blob1.txt', '/path/to/file1.txt'), ('to\\blob2.txt', '\\path\\to\\file2.txt'), ('to\\blob3.txt', '/path/to/file3.txt'), ] # container and some blobs were created before, # and upload with overwritting self.mock_blob_service.exists = mock.Mock(return_value=True) mock_list_blobs.return_value = ['to/blob1.txt', 'to/blob2.txt'] six.assertCountEqual( self, self.azure_handler.upload('test', blob_pairs, overwrite=False), [ 'to/blob3.txt', ]) mock_create_container.assert_not_called() mock_create_blob.assert_called_with('test', 'to/blob3.txt', '/path/to/file3.txt') # upload with `overwrite` set to True mock_create_blob.reset_mock() six.assertCountEqual( self, self.azure_handler.upload('test', blob_pairs, overwrite=True), ['to/blob1.txt', 'to/blob2.txt', 'to/blob3.txt']) self.azure_handler.upload('test', blob_pairs, overwrite=True) mock_create_container.assert_not_called() mock_create_blob.assert_has_calls([ mock.call('test', 'to/blob1.txt', '/path/to/file1.txt'), mock.call('test', 'to/blob2.txt', '\\path\\to\\file2.txt'), mock.call('test', 'to/blob3.txt', '/path/to/file3.txt') ]) # container was not exist self.mock_blob_service.exists = mock.Mock(return_value=False) self.azure_handler.upload('test', blob_pairs, overwrite=False) mock_create_container.assert_called_with('test', set_public=True)
def test_download(self, mock_get_blob, mock_list_blobs): # case 1. the container does not exist self.mock_blob_service.exists = mock.Mock(return_value=False) self.assertEqual(self.azure_handler.download('test', 'dest'), []) self.assertEqual( self.azure_handler.download('test', 'dest', blob_names=["blob1.txt"]), []) # case 2. the container exists and `blob_names` was not specified self.mock_blob_service.exists = mock.Mock(return_value=True) mock_list_blobs.return_value = [ 'to/blob1.txt', 'to/blob2.txt', 'to/blob3.txt' ] six.assertCountEqual(self, self.azure_handler.download('test', 'dest'), ['to/blob1.txt', 'to/blob2.txt', 'to/blob3.txt']) mock_get_blob.assert_has_calls([ mock.call("test", "to/blob1.txt", expandpath("dest/to/blob1.txt")), mock.call("test", "to/blob2.txt", expandpath("dest/to/blob2.txt")), mock.call("test", "to/blob3.txt", expandpath("dest/to/blob3.txt")), ]) # case 3. the container exists and `blob_names` was specified blob_names = [ 'to/blob1.txt', 'to\\blob2.txt', ] mock_get_blob.reset_mock() six.assertCountEqual( self, self.azure_handler.download('test', 'dest', blob_names=blob_names), ['to/blob1.txt', 'to\\blob2.txt']) mock_get_blob.assert_has_calls([ mock.call("test", "to/blob1.txt", expandpath("dest/to/blob1.txt")), mock.call("test", "to/blob2.txt", expandpath("dest/to/blob2.txt")), ]) missing_blob_names = [ 'to/blob1.txt', 'to/blob4.txt', ] mock_get_blob.reset_mock() six.assertCountEqual( self, self.azure_handler.download('test', 'dest', blob_names=missing_blob_names), ['to/blob1.txt']) mock_get_blob.assert_called_with("test", "to/blob1.txt", expandpath("dest/to/blob1.txt"))
def test_list_blobs(self): # Since “name” is an argument to the Mock constructor, # if you want your mock object to have a “name” attribute # you can’t just pass it in at creation time. mock_obj1 = mock.MagicMock() mock_obj1.name = "a.txt" mock_obj2 = mock.MagicMock() mock_obj2.name = "a.wav" mock_obj3 = mock.MagicMock() mock_obj3.name = "a.metadata" self.mock_blob_service.list_blobs.return_value = [ mock_obj1, mock_obj2, mock_obj3 ] six.assertCountEqual(self, self.azure_handler.list_blobs('test'), ["a.txt", "a.wav", "a.metadata"]) self.mock_blob_service.list_blobs.assert_called_with('test', prefix=None, timeout=300) six.assertCountEqual( self, self.azure_handler.list_blobs('test', prefix="test-", suffix=".wav"), ["a.wav"]) self.mock_blob_service.list_blobs.assert_called_with('test', prefix="test-", timeout=300) six.assertCountEqual( self, self.azure_handler.list_blobs('test', suffix=(".wav", ".txt")), ["a.wav", "a.txt"]) self.mock_blob_service.list_blobs.assert_called_with('test', prefix=None, timeout=300) self.mock_azure.reset_mock() self.mock_blob_service.list_blobs = mock.Mock( side_effect=AzureMissingResourceHttpError('a', 'b')) six.assertCountEqual( self, self.azure_handler.list_blobs('test', suffix=(".wav", ".txt")), [])
def test_copy_blobs(self, mock_list_blobs): # case 1. `blob_names` was set to None but src_container was given mock_list_blobs.return_value = [ 'to/blob1.txt', 'to/blob1.wav', 'to/blob2.txt' ] azure_host = "test.blob.endpoint" six.assertCountEqual( self, self.azure_handler.copy_blobs(None, 'test', src_container='source'), # 可以匹配所有的 ['to/blob1.txt', 'to/blob1.wav', 'to/blob2.txt']) self.mock_blob_service.copy_blob.assert_has_calls([ mock.call('test', 'to/blob1.txt', 'http://{}/source/to/blob1.txt'.format(azure_host)), mock.call('test', 'to/blob1.wav', 'http://{}/source/to/blob1.wav'.format(azure_host)), mock.call('test', 'to/blob2.txt', 'http://{}/source/to/blob2.txt'.format(azure_host)), ]) six.assertCountEqual( self, self.azure_handler.copy_blobs(None, 'test', src_container='source', pattern='*.wav'), ['to/blob1.wav']) six.assertCountEqual( self, self.azure_handler.copy_blobs(None, 'test', src_container='source', pattern='*.txt'), ['to/blob1.txt', 'to/blob2.txt']) # if src_container is None self.mock_azure.reset_mock() with self.assertRaises(ImproperlyConfigured): self.azure_handler.copy_blobs(None, 'test', None)
def test_visit_dst(self): six.assertCountEqual(self, [p[1] for p in self.visit(dst='test')], ['test/a.txt', 'test/B.txt', 'test/b/b.yaml']) six.assertCountEqual(self, [p[1] for p in self.visit(dst='')], ['a.txt', 'B.txt', 'b/b.yaml'])