Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY definition of _Monkey class used for testing. #250

Merged
merged 1 commit into from
Oct 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gcloud/_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Shared testing utilities."""


class _Monkey(object):
# context-manager for replacing module names in the scope of a test.

def __init__(self, module, **kw):
self.module = module
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
for key, value in kw.items():
setattr(module, key, value)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
for key, value in self.to_restore.items():
setattr(self.module, key, value)
4 changes: 2 additions & 2 deletions gcloud/datastore/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_it(self):
from gcloud.datastore import SCOPE
from gcloud.datastore.connection import Connection
from gcloud.test_credentials import _Client
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey

CLIENT_EMAIL = 'phred@example.com'
PRIVATE_KEY = 'SEEkR1t'
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_it(self):
from gcloud.datastore.connection import Connection
from gcloud.datastore.dataset import Dataset
from gcloud.test_credentials import _Client
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey

CLIENT_EMAIL = 'phred@example.com'
PRIVATE_KEY = 'SEEkR1t'
Expand Down
18 changes: 1 addition & 17 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def test_transaction_setter(self):
self.assertTrue(conn.transaction() is xact)

def test_mutation_wo_transaction(self):
from gcloud._testing import _Monkey
from gcloud.datastore.connection import datastore_pb

class Mutation(object):
Expand Down Expand Up @@ -866,20 +867,3 @@ def __init__(self, headers, content):
def request(self, **kw):
self._called_with = kw
return self._headers, self._content


class _Monkey(object):

# context-manager for replacing module names in the scope of a test.
def __init__(self, module, **kw):
self.module = module
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
for key, value in kw.items():
setattr(module, key, value)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
for key, value in self.to_restore.items():
setattr(self.module, key, value)
4 changes: 2 additions & 2 deletions gcloud/storage/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_it(self):
from gcloud.storage import SCOPE
from gcloud.storage.connection import Connection
from gcloud.test_credentials import _Client
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
PROJECT = 'project'
CLIENT_EMAIL = 'phred@example.com'
PRIVATE_KEY = 'SEEkR1t'
Expand Down Expand Up @@ -43,7 +43,7 @@ def _callFUT(self, *args, **kw):
def test_it(self):
from tempfile import NamedTemporaryFile
from gcloud import storage
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey

bucket = object()

Expand Down
10 changes: 5 additions & 5 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class _Key(object):
self.assertEqual(kw['path'], COPY_PATH)

def test_upload_file_default_key(self):
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import bucket as MUT
BASENAME = 'file.ext'
FILENAME = '/path/to/%s' % BASENAME
Expand All @@ -312,7 +312,7 @@ def set_contents_from_filename(self, filename):
self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME)])

def test_upload_file_explicit_key(self):
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import bucket as MUT
FILENAME = '/path/to/file'
KEY = 'key'
Expand All @@ -332,7 +332,7 @@ def set_contents_from_filename(self, filename):
self.assertEqual(_uploaded, [(bucket, KEY, FILENAME)])

def test_upload_file_object_no_key(self):
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import bucket as MUT
FILENAME = 'file.txt'
FILEOBJECT = MockFile(FILENAME)
Expand All @@ -352,7 +352,7 @@ def set_contents_from_file(self, fh):
self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT)])

def test_upload_file_object_explicit_key(self):
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import bucket as MUT
FILENAME = 'file.txt'
FILEOBJECT = MockFile(FILENAME)
Expand Down Expand Up @@ -839,7 +839,7 @@ def test_make_public_w_future(self):

def test_make_public_recursive(self):
from gcloud.storage.acl import ACL
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import iterator
from gcloud.storage import bucket as MUT
_saved = []
Expand Down
6 changes: 3 additions & 3 deletions gcloud/storage/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_delete(self):

def test_get_contents_to_file(self):
from StringIO import StringIO
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import key as MUT
_CHUNKS = ['abc', 'def']
KEY = 'key'
Expand All @@ -135,7 +135,7 @@ def test_get_contents_to_file(self):

def test_get_contents_to_filename(self):
from tempfile import NamedTemporaryFile
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import key as MUT
_CHUNKS = ['abc', 'def']
KEY = 'key'
Expand All @@ -151,7 +151,7 @@ def test_get_contents_to_filename(self):
self.assertEqual(wrote, ''.join(_CHUNKS))

def test_get_contents_as_string(self):
from gcloud.test_credentials import _Monkey
from gcloud._testing import _Monkey
from gcloud.storage import key as MUT
_CHUNKS = ['abc', 'def']
KEY = 'key'
Expand Down
19 changes: 2 additions & 17 deletions gcloud/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestCredentials(unittest2.TestCase):
def test_get_for_service_account_wo_scope(self):
from tempfile import NamedTemporaryFile
from gcloud import credentials
from gcloud._testing import _Monkey
CLIENT_EMAIL = 'phred@example.com'
PRIVATE_KEY = 'SEEkR1t'
client = _Client()
Expand All @@ -25,6 +26,7 @@ def test_get_for_service_account_wo_scope(self):
def test_get_for_service_account_w_scope(self):
from tempfile import NamedTemporaryFile
from gcloud import credentials
from gcloud._testing import _Monkey
CLIENT_EMAIL = 'phred@example.com'
PRIVATE_KEY = 'SEEkR1t'
SCOPE = 'SCOPE'
Expand All @@ -51,20 +53,3 @@ def __init__(self):
def SignedJwtAssertionCredentials(self, **kw):
self._called_with = kw
return self._signed


class _Monkey(object):
# context-manager for replacing module names in the scope of a test.

def __init__(self, module, **kw):
self.module = module
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
for key, value in kw.items():
setattr(module, key, value)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
for key, value in self.to_restore.items():
setattr(self.module, key, value)