# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License'). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the 'license' file accompanying this file. This file is
# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from tests import unittest
import mock

from botocore.exceptions import ClientError

from boto3.s3 import inject


class TestInjectTransferMethods(unittest.TestCase):
    def test_inject_upload_download_file_to_client(self):
        class_attributes = {}
        inject.inject_s3_transfer_methods(class_attributes=class_attributes)
        self.assertIn('upload_file', class_attributes)
        self.assertIn('download_file', class_attributes)

    def test_upload_file_proxies_to_transfer_object(self):
        with mock.patch('boto3.s3.inject.S3Transfer') as transfer:
            inject.upload_file(mock.sentinel.CLIENT,
                               Filename='filename',
                               Bucket='bucket', Key='key')
            transfer.return_value.upload_file.assert_called_with(
                filename='filename', bucket='bucket', key='key',
                extra_args=None, callback=None)

    def test_download_file_proxies_to_transfer_object(self):
        with mock.patch('boto3.s3.inject.S3Transfer') as transfer:
            inject.download_file(
                mock.sentinel.CLIENT,
                Bucket='bucket', Key='key',
                Filename='filename')
            transfer.return_value.download_file.assert_called_with(
                bucket='bucket', key='key', filename='filename',
                extra_args=None, callback=None)


class TestBucketLoad(unittest.TestCase):
    def setUp(self):
        self.client = mock.Mock()
        self.resource = mock.Mock()
        self.resource.meta.client = self.client

    def test_bucket_load_finds_bucket(self):
        self.resource.name = 'MyBucket'
        self.client.list_buckets.return_value = [
            {'Name': 'NotMyBucket', 'CreationDate': 1},
            {'Name': resource.name, 'CreationDate': 2}
        ]

        inject.bucket_load(self.resource)
        self.assertEqual(
            self.resource.meta.data,
            {'Name': resource.name, 'CreationDate': 2})

    def test_bucket_load_raise_error(self):
        self.resource.name = 'MyBucket'
        self.client.list_buckets.return_value = [
            {'Name': 'NotMyBucket', 'CreationDate': 1},
            {'Name': 'NotMine2', 'CreationDate': 2}
        ]
        with self.assertRaises(ClientError):
            inject.bucket_load(self.resource)

@@ -2239,34 +2239,61 @@
 .return_value = 
-%5B%0A
+%7B%0A            'Buckets': %5B%0A  
             %7B'Na
@@ -2280,32 +2280,34 @@
 %5B%0A              
+  
 %7B'Name': 'NotMyB
@@ -2338,32 +2338,36 @@
 1%7D,%0A            
+    
 %7B'Name': resourc
@@ -2351,32 +2351,37 @@
        %7B'Name': 
+self.
 resource.name, '
@@ -2397,25 +2397,28 @@
 ate': 2%7D
-%0A
+,%0A  
         
 %5D%0A%0A     
@@ -2409,17 +2409,30 @@
         
-%5D
+  %5D,%0A        %7D
 %0A%0A      
@@ -2551,16 +2551,21 @@
 'Name': 
+self.
 resource
@@ -2723,16 +2723,41 @@
 _value =
+ %7B%0A            'Buckets':
  %5B%0A     
@@ -2755,32 +2755,36 @@
 : %5B%0A            
+    
 %7B'Name': 'NotMyB
@@ -2815,32 +2815,36 @@
 1%7D,%0A            
+    
 %7B'Name': 'NotMin
@@ -2866,25 +2866,29 @@
 ate': 2%7D
-%0A
+,%0A   
         
 %5D%0A      
@@ -2879,17 +2879,29 @@
         
-%5D
+ %5D,%0A        %7D
 %0A       

Fix failing test