Best Python code snippet using localstack_python
test_download_layers.py
Source:test_download_layers.py  
1import os2from unittest import TestCase3from unittest.mock import Mock, call, patch4from botocore.exceptions import NoCredentialsError, ClientError5from pathlib import Path6from parameterized import parameterized7from samcli.local.layers.layer_downloader import LayerDownloader8from samcli.commands.local.cli_common.user_exceptions import CredentialsRequired, ResourceNotFound9class TestDownloadLayers(TestCase):10    @patch("samcli.local.layers.layer_downloader.LayerDownloader._create_cache")11    def test_initialization(self, create_cache_patch):12        create_cache_patch.return_value = None13        download_layers = LayerDownloader("/some/path", ".", Mock())14        self.assertEqual(download_layers.layer_cache, "/some/path")15        create_cache_patch.assert_called_with("/some/path")16    @patch("samcli.local.layers.layer_downloader.LayerDownloader.download")17    def test_download_all_without_force(self, download_patch):18        download_patch.side_effect = ["/home/layer1", "/home/layer2"]19        download_layers = LayerDownloader("/home", ".", Mock())20        acutal_results = download_layers.download_all(["layer1", "layer2"])21        self.assertEqual(acutal_results, ["/home/layer1", "/home/layer2"])22        download_patch.assert_has_calls([call("layer1", False), call("layer2", False)])23    @patch("samcli.local.layers.layer_downloader.LayerDownloader.download")24    def test_download_all_with_force(self, download_patch):25        download_patch.side_effect = ["/home/layer1", "/home/layer2"]26        download_layers = LayerDownloader("/home", ".", Mock())27        acutal_results = download_layers.download_all(["layer1", "layer2"], force=True)28        self.assertEqual(acutal_results, ["/home/layer1", "/home/layer2"])29        download_patch.assert_has_calls([call("layer1", True), call("layer2", True)])30    @patch("samcli.local.layers.layer_downloader.LayerDownloader._create_cache")31    @patch("samcli.local.layers.layer_downloader.LayerDownloader._is_layer_cached")32    def test_download_layer_that_is_cached(self, is_layer_cached_patch, create_cache_patch):33        is_layer_cached_patch.return_value = True34        download_layers = LayerDownloader("/home", ".", Mock())35        layer_mock = Mock()36        layer_mock.is_defined_within_template = False37        layer_mock.name = "layer1"38        actual = download_layers.download(layer_mock)39        self.assertEqual(actual.codeuri, str(Path("/home/layer1").resolve()))40        create_cache_patch.assert_called_once_with("/home")41    @patch("samcli.local.layers.layer_downloader.resolve_code_path")42    @patch("samcli.local.layers.layer_downloader.LayerDownloader._create_cache")43    def test_download_layer_that_was_template_defined(self, create_cache_patch, resolve_code_path_patch):44        """45        when the template is not lcoated in working directory, layer's codeuri needs to be adjusted46        """47        stack_path_mock = Mock()48        stack_template_location = "./some/path/template.yaml"49        download_layers = LayerDownloader(50            "/home", ".", [Mock(stack_path=stack_path_mock, location=stack_template_location)]51        )52        layer_mock = Mock()53        layer_mock.is_template_defined = True54        layer_mock.name = "layer1"55        layer_mock.codeuri = "codeuri"56        layer_mock.stack_path = stack_path_mock57        resolve_code_path_return_mock = Mock()58        resolve_code_path_patch.return_value = resolve_code_path_return_mock59        actual = download_layers.download(layer_mock)60        self.assertEqual(actual.codeuri, resolve_code_path_return_mock)61        create_cache_patch.assert_not_called()62        resolve_code_path_patch.assert_called_once_with(".", "codeuri")63    @patch("samcli.local.layers.layer_downloader.unzip_from_uri")64    @patch("samcli.local.layers.layer_downloader.LayerDownloader._fetch_layer_uri")65    @patch("samcli.local.layers.layer_downloader.LayerDownloader._create_cache")66    @patch("samcli.local.layers.layer_downloader.LayerDownloader._is_layer_cached")67    def test_download_layer(68        self, is_layer_cached_patch, create_cache_patch, fetch_layer_uri_patch, unzip_from_uri_patch69    ):70        is_layer_cached_patch.return_value = False71        download_layers = LayerDownloader("/home", ".", Mock())72        layer_mock = Mock()73        layer_mock.is_defined_within_template = False74        layer_mock.name = "layer1"75        layer_mock.arn = "arn:layer:layer1:1"76        layer_mock.layer_arn = "arn:layer:layer1"77        fetch_layer_uri_patch.return_value = "layer/uri"78        actual = download_layers.download(layer_mock)79        self.assertEqual(actual.codeuri, str(Path("/home/layer1").resolve()))80        create_cache_patch.assert_called_once_with("/home")81        fetch_layer_uri_patch.assert_called_once_with(layer_mock)82        unzip_from_uri_patch.assert_called_once_with(83            "layer/uri",84            str(Path("/home/layer1.zip").resolve()),85            unzip_output_dir=str(Path("/home/layer1").resolve()),86            progressbar_label="Downloading arn:layer:layer1",87        )88    def test_layer_is_cached(self):89        download_layers = LayerDownloader("/", ".", Mock())90        layer_path = Mock()91        layer_path.exists.return_value = True92        self.assertTrue(download_layers._is_layer_cached(layer_path))93    def test_layer_is_not_cached(self):94        download_layers = LayerDownloader("/", ".", Mock())95        layer_path = Mock()96        layer_path.exists.return_value = False97        self.assertFalse(download_layers._is_layer_cached(layer_path))98    @patch("samcli.local.layers.layer_downloader.Path")99    def test_create_cache(self, path_patch):100        cache_path_mock = Mock()101        path_patch.return_value = cache_path_mock102        self.assertIsNone(LayerDownloader._create_cache("./home"))103        path_patch.assert_called_once_with("./home")104        cache_path_mock.mkdir.assert_called_once_with(parents=True, exist_ok=True, mode=0o700)105class TestLayerDownloader_fetch_layer_uri(TestCase):106    def test_fetch_layer_uri_is_successful(self):107        lambda_client_mock = Mock()108        lambda_client_mock.get_layer_version.return_value = {"Content": {"Location": "some/uri"}}109        download_layers = LayerDownloader("/", ".", Mock(), lambda_client_mock)110        layer = Mock()111        layer.layer_arn = "arn"112        layer.version = 1113        actual_uri = download_layers._fetch_layer_uri(layer=layer)114        self.assertEqual(actual_uri, "some/uri")115    def test_fetch_layer_uri_fails_with_no_creds(self):116        lambda_client_mock = Mock()117        lambda_client_mock.get_layer_version.side_effect = NoCredentialsError()118        download_layers = LayerDownloader("/", ".", Mock(), lambda_client_mock)119        layer = Mock()120        layer.layer_arn = "arn"121        layer.version = 1122        with self.assertRaises(CredentialsRequired):123            download_layers._fetch_layer_uri(layer=layer)124    def test_fetch_layer_uri_fails_with_AccessDeniedException(self):125        lambda_client_mock = Mock()126        lambda_client_mock.get_layer_version.side_effect = ClientError(127            error_response={"Error": {"Code": "AccessDeniedException"}}, operation_name="lambda"128        )129        download_layers = LayerDownloader("/", ".", Mock(), lambda_client_mock)130        layer = Mock()131        layer.layer_arn = "arn"132        layer.version = 1133        with self.assertRaises(CredentialsRequired):134            download_layers._fetch_layer_uri(layer=layer)135    def test_fetch_layer_uri_fails_with_ResourceNotFoundException(self):136        lambda_client_mock = Mock()137        lambda_client_mock.get_layer_version.side_effect = ClientError(138            error_response={"Error": {"Code": "ResourceNotFoundException"}}, operation_name="lambda"139        )140        download_layers = LayerDownloader("/", ".", Mock(), lambda_client_mock)141        layer = Mock()142        layer.layer_arn = "arn"143        layer.version = 1144        with self.assertRaises(ResourceNotFound):145            download_layers._fetch_layer_uri(layer=layer)146    def test_fetch_layer_uri_re_raises_client_error(self):147        lambda_client_mock = Mock()148        lambda_client_mock.get_layer_version.side_effect = ClientError(149            error_response={"Error": {"Code": "Unknown"}}, operation_name="lambda"150        )151        download_layers = LayerDownloader("/", ".", Mock(), lambda_client_mock)152        layer = Mock()153        layer.layer_arn = "arn"154        layer.version = 1155        with self.assertRaises(ClientError):...test_mp3_format.py
Source:test_mp3_format.py  
...21        self.assertEqual(result, MpegVersion.INVALID)22    # Layer23    def test_get_layer_version_l1(self):24        data = bytes.fromhex('80A6FF24')25        result = Mp3Format.get_layer_version(data, 0)26        self.assertEqual(result, MpegLayer.L1)27    def test_get_layer_version_l2(self):28        data = bytes.fromhex('80A4FF24')29        result = Mp3Format.get_layer_version(data, 0)30        self.assertEqual(result, MpegLayer.L2)31    def test_get_layer_version_l3(self):32        data = bytes.fromhex('80A2FF24')33        result = Mp3Format.get_layer_version(data, 0)34        self.assertEqual(result, MpegLayer.L3)35    def test_get_layer_version_invalid(self):36        data = bytes.fromhex('80A0FF24')37        result = Mp3Format.get_layer_version(data, 0)38        self.assertEqual(result, MpegLayer.INVALID)39    # Bitrate40    def test_get_bitrate_v2_l3_160k(self):41        data = bytes.fromhex('8012E542')42        result = Mp3Format.get_bitrate(data, 0)43        self.assertEqual(result, 160000)44    45    def test_get_bitrate_v1_l3_320k(self):46        data = bytes.fromhex('801AE542')47        result = Mp3Format.get_bitrate(data, 0)48        self.assertEqual(result, 320000)49    # Sample rate50    def test_get_sample_rate_v1_48k(self):51        data = bytes.fromhex('22185449')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
