Skip to content

Commit

Permalink
Fixed import error for create_client
Browse files Browse the repository at this point in the history
  • Loading branch information
Shun Fan committed Jun 30, 2015
1 parent cacb8ce commit 32cb985
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
4 changes: 3 additions & 1 deletion storage/storage_transfer/create_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.
#
import logging
import os

from apiclient import discovery
import httplib2
Expand All @@ -27,7 +28,8 @@ def create_transfer_client(http=None):
uses a discovery doc but eventually should be discoverable.
"""
logging.getLogger().setLevel(logging.DEBUG)
with open('storagetransfer_v1.json', 'r') as discovery_doc_file:
fname = os.path.join(os.path.dirname(__file__), 'storagetransfer_v1.json')
with open(fname, 'r') as discovery_doc_file:
discover_doc = discovery_doc_file.read()
credentials = GoogleCredentials.get_application_default()
if credentials.create_scoped_required():
Expand Down
27 changes: 14 additions & 13 deletions storage/storage_transfer/test_create_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import unittest

from create_client import create_transfer_client
import create_client
import httplib2
from mock import Mock
from mock import patch
Expand All @@ -24,38 +25,38 @@ class CheckCreateClientTestCase(unittest.TestCase):
"""A test case for client creation."""

def setUp(self):
patcher1 = patch('create_client.GoogleCredentials')
patcher2 = patch('create_client.discovery.build_from_document')
patcher3 = patch('create_client.httplib2')
patcher1 = patch(
'storage.storage_transfer.create_client.GoogleCredentials')
patcher2 = patch(
'storage.storage_transfer.create_client.discovery'
'.build_from_document')
self.mock_google_credentials = patcher1.start()
self.mock_discovery = patcher2.start()
self.mock_httplib2 = patcher3.start()
self.addCleanup(patcher1.stop)
self.addCleanup(patcher2.stop)
self.addCleanup(patcher3.stop)

self.mock_credentials = Mock(
spec=GoogleCredentials)
self.mock_google_credentials.get_application_default.return_value = \
self.mock_credentials
with open('storagetransfer_v1.json', 'r') as discovery_doc_file:
fname = os.path.join(
os.path.dirname(__file__), 'storagetransfer_v1.json')
with open(fname, 'r') as discovery_doc_file:
self.discover_doc = discovery_doc_file.read()
self.http = httplib2.Http()

def test_create_client_scoped_required_true(self):
self.mock_credentials.create_scoped_required.return_value = True
create_transfer_client(self.http)
create_client.create_transfer_client(self.http)
self.mock_discovery.assert_called_with(
self.discover_doc, http=self.http)

def test_create_client_scoped_required_false(self):
self.mock_credentials.create_scoped_required.return_value = False
create_transfer_client(self.http)
create_client.create_transfer_client(self.http)
self.mock_discovery.assert_called_with(
self.discover_doc, http=self.http)

def test_create_client_without_http(self):
self.mock_httplib2.Http.return_value = self.http
create_transfer_client()
self.mock_discovery.assert_called_with(
self.discover_doc, http=self.http)
create_client.create_transfer_client()
assert self.mock_discovery.called

0 comments on commit 32cb985

Please sign in to comment.