Repository: boto/boto3
File: tests/unit/s3/test_transfer.py (Python)

Code before:
        self.transfer = S3Transfer(manager=self.manager)
        self.callback = mock.Mock()

    def assert_callback_wrapped_in_subscriber(self, call_args):
        subscribers = call_args[0][4]
        # Make sure only one subscriber was passed in.
        assert len(subscribers) == 1
        subscriber = subscribers[0]
        # Make sure that the subscriber is of the correct type
        assert isinstance(subscriber, ProgressCallbackInvoker)
        # Make sure that the on_progress method() calls out to the wrapped
        # callback by actually invoking it.
        subscriber.on_progress(bytes_transferred=1)
        self.callback.assert_called_with(1)

    def test_upload_file(self):
        extra_args = {'ACL': 'public-read'}
        self.transfer.upload_file(
            'smallfile', 'bucket', 'key', extra_args=extra_args
        )
        self.manager.upload.assert_called_with(
            'smallfile', 'bucket', 'key', extra_args, None
        )

    def test_upload_file_via_path(self):
        from pathlib import Path

        extra_args = {'ACL': 'public-read'}
        self.transfer.upload_file(
            Path('smallfile'), 'bucket', 'key', extra_args=extra_args
        )
        self.manager.upload.assert_called_with(
            'smallfile', 'bucket', 'key', extra_args, None
        )

    def test_download_file(self):
        extra_args = {
            'SSECustomerKey': 'foo',
            'SSECustomerAlgorithm': 'AES256',
        }
        self.transfer.download_file(
            'bucket', 'key', '/tmp/smallfile', extra_args=extra_args
        )
        self.manager.download.assert_called_with(
            'bucket', 'key', '/tmp/smallfile', extra_args, None
        )

    def test_download_file_via_path(self):
        import os
        from pathlib import Path


Review comment (suggestion):
Can this import move to the top of the file? Or is there a reason why it needs to be imported locally just in this test? 

Same question for the two imports in `test_download_file_via_path`.

Code after:
    def setUp(self):
        self.client = mock.Mock()
        self.manager = mock.Mock(TransferManager(self.client))
        self.transfer = S3Transfer(manager=self.manager)
        self.callback = mock.Mock()
        # Use NamedTempFile as source of a path string that is valid and
        # realistic for the system the tests are run on. The file gets deleted
        # immediately and will not actually exist while the tests are run.
        with NamedTemporaryFile("w") as tmp_file:
            self.file_path_str = tmp_file.name

    def assert_callback_wrapped_in_subscriber(self, call_args):
        subscribers = call_args[0][4]
        # Make sure only one subscriber was passed in.
        assert len(subscribers) == 1
        subscriber = subscribers[0]
        # Make sure that the subscriber is of the correct type
        assert isinstance(subscriber, ProgressCallbackInvoker)
        # Make sure that the on_progress method() calls out to the wrapped
        # callback by actually invoking it.
        subscriber.on_progress(bytes_transferred=1)
        self.callback.assert_called_with(1)

    def test_upload_file(self):
        extra_args = {'ACL': 'public-read'}
        self.transfer.upload_file(
            'smallfile', 'bucket', 'key', extra_args=extra_args
        )
        self.manager.upload.assert_called_with(
            'smallfile', 'bucket', 'key', extra_args, None
        )

    def test_upload_file_via_path(self):
        extra_args = {'ACL': 'public-read'}
        self.transfer.upload_file(
            pathlib.Path(self.file_path_str),
            'bucket',
            'key',
            extra_args=extra_args,
        )
        self.manager.upload.assert_called_with(
            self.file_path_str, 'bucket', 'key', extra_args, None
        )

    def test_upload_file_via_purepath(self):
        extra_args = {'ACL': 'public-read'}
        self.transfer.upload_file(
            pathlib.PurePath(self.file_path_str),
            'bucket',
            'key',
            extra_args=extra_args,