Skip to content

Commit b194e0e

Browse files
committed
Merge pull request #250 from tseaver/refactor-dry_monkey
DRY definition of _Monkey class used for testing.
2 parents 0e99f7b + 49450e0 commit b194e0e

7 files changed

Lines changed: 33 additions & 46 deletions

File tree

gcloud/_testing.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Shared testing utilities."""
2+
3+
4+
class _Monkey(object):
5+
# context-manager for replacing module names in the scope of a test.
6+
7+
def __init__(self, module, **kw):
8+
self.module = module
9+
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
10+
for key, value in kw.items():
11+
setattr(module, key, value)
12+
13+
def __enter__(self):
14+
return self
15+
16+
def __exit__(self, exc_type, exc_val, exc_tb):
17+
for key, value in self.to_restore.items():
18+
setattr(self.module, key, value)

gcloud/datastore/test___init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_it(self):
1313
from gcloud.datastore import SCOPE
1414
from gcloud.datastore.connection import Connection
1515
from gcloud.test_credentials import _Client
16-
from gcloud.test_credentials import _Monkey
16+
from gcloud._testing import _Monkey
1717

1818
CLIENT_EMAIL = 'phred@example.com'
1919
PRIVATE_KEY = 'SEEkR1t'
@@ -45,7 +45,7 @@ def test_it(self):
4545
from gcloud.datastore.connection import Connection
4646
from gcloud.datastore.dataset import Dataset
4747
from gcloud.test_credentials import _Client
48-
from gcloud.test_credentials import _Monkey
48+
from gcloud._testing import _Monkey
4949

5050
CLIENT_EMAIL = 'phred@example.com'
5151
PRIVATE_KEY = 'SEEkR1t'

gcloud/datastore/test_connection.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def test_transaction_setter(self):
161161
self.assertTrue(conn.transaction() is xact)
162162

163163
def test_mutation_wo_transaction(self):
164+
from gcloud._testing import _Monkey
164165
from gcloud.datastore.connection import datastore_pb
165166

166167
class Mutation(object):
@@ -866,20 +867,3 @@ def __init__(self, headers, content):
866867
def request(self, **kw):
867868
self._called_with = kw
868869
return self._headers, self._content
869-
870-
871-
class _Monkey(object):
872-
873-
# context-manager for replacing module names in the scope of a test.
874-
def __init__(self, module, **kw):
875-
self.module = module
876-
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
877-
for key, value in kw.items():
878-
setattr(module, key, value)
879-
880-
def __enter__(self):
881-
return self
882-
883-
def __exit__(self, exc_type, exc_val, exc_tb):
884-
for key, value in self.to_restore.items():
885-
setattr(self.module, key, value)

gcloud/storage/test___init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_it(self):
1313
from gcloud.storage import SCOPE
1414
from gcloud.storage.connection import Connection
1515
from gcloud.test_credentials import _Client
16-
from gcloud.test_credentials import _Monkey
16+
from gcloud._testing import _Monkey
1717
PROJECT = 'project'
1818
CLIENT_EMAIL = 'phred@example.com'
1919
PRIVATE_KEY = 'SEEkR1t'
@@ -43,7 +43,7 @@ def _callFUT(self, *args, **kw):
4343
def test_it(self):
4444
from tempfile import NamedTemporaryFile
4545
from gcloud import storage
46-
from gcloud.test_credentials import _Monkey
46+
from gcloud._testing import _Monkey
4747

4848
bucket = object()
4949

gcloud/storage/test_bucket.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class _Key(object):
292292
self.assertEqual(kw['path'], COPY_PATH)
293293

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

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

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

354354
def test_upload_file_object_explicit_key(self):
355-
from gcloud.test_credentials import _Monkey
355+
from gcloud._testing import _Monkey
356356
from gcloud.storage import bucket as MUT
357357
FILENAME = 'file.txt'
358358
FILEOBJECT = MockFile(FILENAME)
@@ -839,7 +839,7 @@ def test_make_public_w_future(self):
839839

840840
def test_make_public_recursive(self):
841841
from gcloud.storage.acl import ACL
842-
from gcloud.test_credentials import _Monkey
842+
from gcloud._testing import _Monkey
843843
from gcloud.storage import iterator
844844
from gcloud.storage import bucket as MUT
845845
_saved = []

gcloud/storage/test_key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_delete(self):
121121

122122
def test_get_contents_to_file(self):
123123
from StringIO import StringIO
124-
from gcloud.test_credentials import _Monkey
124+
from gcloud._testing import _Monkey
125125
from gcloud.storage import key as MUT
126126
_CHUNKS = ['abc', 'def']
127127
KEY = 'key'
@@ -135,7 +135,7 @@ def test_get_contents_to_file(self):
135135

136136
def test_get_contents_to_filename(self):
137137
from tempfile import NamedTemporaryFile
138-
from gcloud.test_credentials import _Monkey
138+
from gcloud._testing import _Monkey
139139
from gcloud.storage import key as MUT
140140
_CHUNKS = ['abc', 'def']
141141
KEY = 'key'
@@ -151,7 +151,7 @@ def test_get_contents_to_filename(self):
151151
self.assertEqual(wrote, ''.join(_CHUNKS))
152152

153153
def test_get_contents_as_string(self):
154-
from gcloud.test_credentials import _Monkey
154+
from gcloud._testing import _Monkey
155155
from gcloud.storage import key as MUT
156156
_CHUNKS = ['abc', 'def']
157157
KEY = 'key'

gcloud/test_credentials.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class TestCredentials(unittest2.TestCase):
66
def test_get_for_service_account_wo_scope(self):
77
from tempfile import NamedTemporaryFile
88
from gcloud import credentials
9+
from gcloud._testing import _Monkey
910
CLIENT_EMAIL = 'phred@example.com'
1011
PRIVATE_KEY = 'SEEkR1t'
1112
client = _Client()
@@ -25,6 +26,7 @@ def test_get_for_service_account_wo_scope(self):
2526
def test_get_for_service_account_w_scope(self):
2627
from tempfile import NamedTemporaryFile
2728
from gcloud import credentials
29+
from gcloud._testing import _Monkey
2830
CLIENT_EMAIL = 'phred@example.com'
2931
PRIVATE_KEY = 'SEEkR1t'
3032
SCOPE = 'SCOPE'
@@ -51,20 +53,3 @@ def __init__(self):
5153
def SignedJwtAssertionCredentials(self, **kw):
5254
self._called_with = kw
5355
return self._signed
54-
55-
56-
class _Monkey(object):
57-
# context-manager for replacing module names in the scope of a test.
58-
59-
def __init__(self, module, **kw):
60-
self.module = module
61-
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
62-
for key, value in kw.items():
63-
setattr(module, key, value)
64-
65-
def __enter__(self):
66-
return self
67-
68-
def __exit__(self, exc_type, exc_val, exc_tb):
69-
for key, value in self.to_restore.items():
70-
setattr(self.module, key, value)

0 commit comments

Comments
 (0)