github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/io/azure/blobstorageio_test.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """Tests for Azure Blob Storage client.
    19  """
    20  # pytype: skip-file
    21  
    22  import logging
    23  import unittest
    24  
    25  # Protect against environments where azure library is not available.
    26  # pylint: disable=wrong-import-order, wrong-import-position
    27  try:
    28    from apache_beam.io.azure import blobstorageio
    29  except ImportError:
    30    blobstorageio = None  # type: ignore[assignment]
    31  # pylint: enable=wrong-import-order, wrong-import-position
    32  
    33  
    34  @unittest.skipIf(blobstorageio is None, 'Azure dependencies are not installed')
    35  class TestAZFSPathParser(unittest.TestCase):
    36  
    37    BAD_AZFS_PATHS = [
    38        'azfs://'
    39        'azfs://storage-account/'
    40        'azfs://storage-account/**'
    41        'azfs://storage-account/**/*'
    42        'azfs://container'
    43        'azfs:///name'
    44        'azfs:///'
    45        'azfs:/blah/container/name'
    46        'azfs://ab/container/name'
    47        'azfs://accountwithmorethan24chars/container/name'
    48        'azfs://***/container/name'
    49        'azfs://storageaccount/my--container/name'
    50        'azfs://storageaccount/CONTAINER/name'
    51        'azfs://storageaccount/ct/name'
    52    ]
    53  
    54    def test_azfs_path(self):
    55      self.assertEqual(
    56          blobstorageio.parse_azfs_path(
    57              'azfs://storageaccount/container/name', get_account=True),
    58          ('storageaccount', 'container', 'name'))
    59      self.assertEqual(
    60          blobstorageio.parse_azfs_path(
    61              'azfs://storageaccount/container/name/sub', get_account=True),
    62          ('storageaccount', 'container', 'name/sub'))
    63  
    64    def test_bad_azfs_path(self):
    65      for path in self.BAD_AZFS_PATHS:
    66        self.assertRaises(ValueError, blobstorageio.parse_azfs_path, path)
    67      self.assertRaises(
    68          ValueError,
    69          blobstorageio.parse_azfs_path,
    70          'azfs://storageaccount/container/')
    71  
    72    def test_azfs_path_blob_optional(self):
    73      self.assertEqual(
    74          blobstorageio.parse_azfs_path(
    75              'azfs://storageaccount/container/name',
    76              blob_optional=True,
    77              get_account=True), ('storageaccount', 'container', 'name'))
    78      self.assertEqual(
    79          blobstorageio.parse_azfs_path(
    80              'azfs://storageaccount/container/',
    81              blob_optional=True,
    82              get_account=True), ('storageaccount', 'container', ''))
    83  
    84    def test_bad_azfs_path_blob_optional(self):
    85      for path in self.BAD_AZFS_PATHS:
    86        self.assertRaises(ValueError, blobstorageio.parse_azfs_path, path, True)
    87  
    88  
    89  if __name__ == '__main__':
    90    logging.getLogger().setLevel(logging.INFO)
    91    unittest.main()