Best Python code snippet using localstack_python
test_get_bucket_location.py
Source:test_get_bucket_location.py  
...31                                    'LocationConstraint':32                                    expected_location_bucket33                                })34        # call function test35        actual_bucket_location = S3Utils.get_bucket_location(36            trace_id, client_s3, bucket_name, aws_account)37        # check result38        self.assertEqual(expected_location_bucket, actual_bucket_location)39    def test_get_bucket_location_success_response_not_exists_location_constraint(self):40        expected_location_bucket = copy.deepcopy(DataTestS3.LOCATION_BUCKET)41        # check exist bucket42        list_buckets = client_s3.list_buckets()43        if len(list_buckets['Buckets']) > 0:44            for bucket in list_buckets['Buckets']:45                client_s3.delete_bucket(Bucket=bucket['Name'])46        # prepare data47        client_s3.create_bucket(Bucket=bucket_name,48                                CreateBucketConfiguration={49                                    'LocationConstraint':50                                    expected_location_bucket51                                })52        # call function test53        with patch.object(client_s3, 'get_bucket_location') as mock_method:54            mock_method.return_value = {}55            actual_bucket_location = S3Utils.get_bucket_location(56                trace_id, client_s3, bucket_name, aws_account)57        # check result58        expected_location_bucket = None59        self.assertEqual(expected_location_bucket, actual_bucket_location)60    def test_get_bucket_location_error_method_not_allowed(self):61        # create mock throw error when called function get_bucket_location62        expected_error_response = copy.deepcopy(DataCommon.ERROR_RESPONSE)63        expected_operation_name = copy.deepcopy(DataCommon.OPERATION_NAME)64        expected_error_response['Error']['Code'] = 'MethodNotAllowed'65        with patch.object(client_s3, 'get_bucket_location') as mock_method:66            mock_method.side_effect = ClientError(67                error_response=expected_error_response,68                operation_name=expected_operation_name)69            with patch.object(PmLogAdapter, 'warning',70                              return_value=None) as mock_method_warning:71                with self.assertRaises(PmError) as exception:72                    # call function test73                    S3Utils.get_bucket_location(trace_id, client_s3,74                                                bucket_name, aws_account)75        # check error76        actual_cause_error = exception.exception.cause_error77        self.assertEqual(expected_error_response['Error'],78                         actual_cause_error.response['Error'])79        self.assertEqual(expected_operation_name,80                         actual_cause_error.operation_name)81        # check write log warning82        mock_method_warning.assert_any_call(83            '[%s] 権éã¨ã©ã¼ã«ããS3ãã±ãããªã¼ã¸ã§ã³æ
å ±ã®åå¾ã«å¤±æãã¾ãããï¼%sï¼', aws_account,84            bucket_name)85    def test_get_bucket_location_error_access_denied(self):86        # create mock throw error when called function get_bucket_location87        expected_error_response = copy.deepcopy(DataCommon.ERROR_RESPONSE)88        expected_operation_name = copy.deepcopy(DataCommon.OPERATION_NAME)89        expected_error_response['Error']['Code'] = 'AccessDenied'90        with patch.object(client_s3, 'get_bucket_location') as mock_method:91            mock_method.side_effect = ClientError(92                error_response=expected_error_response,93                operation_name=expected_operation_name)94            with patch.object(PmLogAdapter, 'warning',95                              return_value=None) as mock_method_warning:96                with self.assertRaises(PmError) as exception:97                    # call function test98                    S3Utils.get_bucket_location(trace_id, client_s3,99                                                bucket_name, aws_account)100        # check error101        actual_cause_error = exception.exception.cause_error102        self.assertEqual(expected_error_response['Error'],103                         actual_cause_error.response['Error'])104        self.assertEqual(expected_operation_name,105                         actual_cause_error.operation_name)106        # check write log warning107        mock_method_warning.assert_any_call(108            '[%s] 権éã¨ã©ã¼ã«ããS3ãã±ãããªã¼ã¸ã§ã³æ
å ±ã®åå¾ã«å¤±æãã¾ãããï¼%sï¼', aws_account,109            bucket_name)110    def test_get_bucket_location_error_other(self):111        # create mock throw error when called function get_bucket_location112        expected_error_response = copy.deepcopy(DataCommon.ERROR_RESPONSE)113        expected_operation_name = copy.deepcopy(DataCommon.OPERATION_NAME)114        with patch.object(client_s3, 'get_bucket_location') as mock_method:115            mock_method.side_effect = ClientError(116                error_response=expected_error_response,117                operation_name=expected_operation_name)118            with patch.object(PmLogAdapter, 'error',119                              return_value=None) as mock_method_error:120                with self.assertRaises(PmError) as exception:121                    # call function test122                    S3Utils.get_bucket_location(trace_id, client_s3,123                                                bucket_name, aws_account)124        # check error125        actual_cause_error = exception.exception.cause_error126        self.assertEqual(expected_error_response['Error'],127                         actual_cause_error.response['Error'])128        self.assertEqual(expected_operation_name,129                         actual_cause_error.operation_name)130        # check write log error131        mock_method_error.assert_any_call('[%s]S3ãã±ãããªã¼ã¸ã§ã³æ
å ±ã®åå¾ã«å¤±æãã¾ãããï¼%sï¼',...s3.py
Source:s3.py  
...102            errorMessage.S3_PUT_OBJECT_ERROR.message,103            errorMessage.S3_PUT_OBJECT_ERROR.error_code104        )105def s3_get_image_url(s3, file_name):106    location = s3.get_bucket_location(107        Bucket=config_parser.get_s3_bucket_name()108    )["LocationConstraint"]109    encoded_file_name = urllib.parse.quote(file_name)110    return "https://" + config_parser.get_s3_bucket_name() + ".s3." + location + ".amazonaws.com/" + encoded_file_name111def s3_get_shape_image_url(s3, file_name):112    location = s3.get_bucket_location(113        Bucket=config_parser.get_s3_bucket_name()114    )["LocationConstraint"]115    encoded_file_name = urllib.parse.quote(file_name)116    return "https://" + config_parser.get_s3_bucket_name() + ".s3." + location + ".amazonaws.com/" \117           + config_parser.get_s3_shape_folder_name() + '/' + encoded_file_name118def s3_get_color_image_url(s3, file_name):119    location = s3.get_bucket_location(120        Bucket=config_parser.get_s3_bucket_name()121    )["LocationConstraint"]122    encoded_file_name = urllib.parse.quote(file_name)123    return "https://" + config_parser.get_s3_bucket_name() + ".s3." + location + ".amazonaws.com/" \124           + config_parser.get_s3_color_folder_name() + '/' + encoded_file_name125def s3_get_ocr_image_url(s3, file_name):126    location = s3.get_bucket_location(127        Bucket=config_parser.get_s3_bucket_name()128    )["LocationConstraint"]129    encoded_file_name = urllib.parse.quote(file_name)130    return "https://" + config_parser.get_s3_bucket_name() + ".s3." + location + ".amazonaws.com/" \131           + config_parser.get_s3_ocr_folder_name() + '/' + encoded_file_name132def s3_get_searched_drug_image_url(s3, file_name):133    location = s3.get_bucket_location(134        Bucket=config_parser.get_s3_bucket_name()135    )["LocationConstraint"]136    encoded_file_name = urllib.parse.quote(file_name)137    return "https://" + config_parser.get_s3_bucket_name() + ".s3." + location + ".amazonaws.com/" \...test_inputs.py
Source:test_inputs.py  
...5    a = LinearInput('foobar', 4, 7)6    a.set_num_buckets(17)7    for v in a.sample_space(55):8        assert 4 <= v <= 79    b, l = a.get_bucket_location(4)10    assert b == 011    assert l == 0.012    b, l = a.get_bucket_location(7 - 1e-12)13    assert b == 17-114    assert l == pytest.approx(1.0)15    assert a.bisect_inputs(5, 6) == pytest.approx(5.5)16def test_logarithmic():17    a = LogarithmicInput('foobar', 0, 2e3)18    a.set_num_buckets(37, scale=1e-3)19    for v in a.sample_space(55):20        assert 0 <= v <= 2e321    b, l = a.get_bucket_location(0)22    assert b == 023    assert l == 0.024    b, l = a.get_bucket_location(2e3 - 1e-12)25    assert b == 37-126    assert l == pytest.approx(1.0)27    assert 5.25 < a.bisect_inputs(5, 6) < 5.4928def test_linear_inverse():29    a = LinearInput('foobar', 11, 57)30    a.set_num_buckets(107)31    trials = [random.uniform(a.minimum, a.maximum) for _ in range(100)]32    trials.append(a.minimum)33    trials.append(a.maximum)34    for v in trials:35        l  = a.get_bucket_value(v)36        vv = a.get_input_value(l)37        assert v == pytest.approx(vv, abs=1e-12)38        assert 0.0 <= l <= a.num_buckets...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!!
