Skip to content

Commit 710af73

Browse files
committed
Move '_require_connection' def into related '_implicit_environ' module.
1 parent 3abc552 commit 710af73

14 files changed

+205
-192
lines changed

gcloud/datastore/_implicit_environ.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from gcloud._helpers import _compute_engine_id
2525
from gcloud._helpers import _lazy_property_deco
2626
from gcloud.datastore.connection import Connection
27+
from gcloud.datastore.connection import _CONNECTIONS
2728

2829

2930
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
@@ -104,6 +105,28 @@ def get_default_dataset_id():
104105
return _DEFAULTS.dataset_id
105106

106107

108+
def _require_connection(connection=None):
109+
"""Infer a connection from the environment, if not passed explicitly.
110+
111+
:type connection: :class:`gcloud.datastore.connection.Connection`
112+
:param connection: Optional.
113+
114+
:rtype: :class:`gcloud.datastore.connection.Connection`
115+
:returns: A connection based on the current environment.
116+
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
117+
cannot be inferred from the environment.
118+
"""
119+
if connection is None:
120+
top = _CONNECTIONS.top
121+
if top is not None:
122+
connection = top.connection
123+
else:
124+
connection = get_default_connection()
125+
if connection is None:
126+
raise EnvironmentError('Connection could not be inferred.')
127+
return connection
128+
129+
107130
def set_default_connection(connection=None):
108131
"""Set default connection either explicitly or implicitly as fall-back.
109132

gcloud/datastore/_testing.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,36 @@ def _setup_defaults(test_case, *args, **kwargs):
3131

3232
def _tear_down_defaults(test_case):
3333
_implicit_environ._DEFAULTS = test_case._replaced_defaults
34+
35+
36+
class _NoCommitBatch(object):
37+
38+
def __init__(self, dataset_id, connection):
39+
from gcloud.datastore.batch import Batch
40+
self._batch = Batch(dataset_id, connection)
41+
42+
def __enter__(self):
43+
from gcloud.datastore.connection import _CONNECTIONS
44+
_CONNECTIONS.push(self._batch)
45+
return self._batch
46+
47+
def __exit__(self, *args):
48+
from gcloud.datastore.connection import _CONNECTIONS
49+
_CONNECTIONS.pop()
50+
51+
52+
class _NoCommitTransaction(object):
53+
54+
def __init__(self, dataset_id, connection, transaction_id='TRANSACTION'):
55+
from gcloud.datastore.transaction import Transaction
56+
xact = self._transaction = Transaction(dataset_id, connection)
57+
xact._id = transaction_id
58+
59+
def __enter__(self):
60+
from gcloud.datastore.connection import _CONNECTIONS
61+
_CONNECTIONS.push(self._transaction)
62+
return self._transaction
63+
64+
def __exit__(self, *args):
65+
from gcloud.datastore.connection import _CONNECTIONS
66+
_CONNECTIONS.pop()

gcloud/datastore/api.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,6 @@ def _require_dataset_id(dataset_id=None, first_key=None):
6666
return dataset_id
6767

6868

69-
def _require_connection(connection=None):
70-
"""Infer a connection from the environment, if not passed explicitly.
71-
72-
:type connection: :class:`gcloud.datastore.connection.Connection`
73-
:param connection: Optional.
74-
75-
:rtype: :class:`gcloud.datastore.connection.Connection`
76-
:returns: A connection based on the current environment.
77-
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
78-
cannot be inferred from the environment.
79-
"""
80-
if connection is None:
81-
top = _CONNECTIONS.top
82-
if top is not None:
83-
connection = top.connection
84-
else:
85-
connection = _implicit_environ.get_default_connection()
86-
if connection is None:
87-
raise EnvironmentError('Connection could not be inferred.')
88-
return connection
89-
90-
9169
def _extended_lookup(connection, dataset_id, key_pbs,
9270
missing=None, deferred=None,
9371
eventual=False, transaction_id=None):
@@ -201,7 +179,7 @@ def get(keys, missing=None, deferred=None, connection=None, dataset_id=None):
201179
if not keys:
202180
return []
203181

204-
connection = _require_connection(connection)
182+
connection = _implicit_environ._require_connection(connection)
205183
dataset_id = _require_dataset_id(dataset_id, keys[0])
206184

207185
if list(set([key.dataset_id for key in keys])) != [dataset_id]:
@@ -260,7 +238,7 @@ def put(entities, connection=None, dataset_id=None):
260238
if not entities:
261239
return
262240

263-
connection = _require_connection(connection)
241+
connection = _implicit_environ._require_connection(connection)
264242
dataset_id = _require_dataset_id(dataset_id, entities[0].key)
265243

266244
current = _CONNECTIONS.top
@@ -295,7 +273,7 @@ def delete(keys, connection=None, dataset_id=None):
295273
if not keys:
296274
return
297275

298-
connection = _require_connection(connection)
276+
connection = _implicit_environ._require_connection(connection)
299277
dataset_id = _require_dataset_id(dataset_id, keys[0])
300278

301279
# We allow partial keys to attempt a delete, the backend will fail.
@@ -325,7 +303,7 @@ def allocate_ids(incomplete_key, num_ids, connection=None):
325303
:returns: The (complete) keys allocated with ``incomplete_key`` as root.
326304
:raises: :class:`ValueError` if ``incomplete_key`` is not a partial key.
327305
"""
328-
connection = _require_connection(connection)
306+
connection = _implicit_environ._require_connection(connection)
329307

330308
if not incomplete_key.is_partial:
331309
raise ValueError(('Key is not partial.', incomplete_key))

gcloud/datastore/test__implicit_environ.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,58 @@ def test_descriptor_for_connection(self):
299299
'connection' in _implicit_environ._DEFAULTS.__dict__)
300300

301301

302+
class Test__require_connection(unittest2.TestCase):
303+
304+
_MARKER = object()
305+
306+
def _callFUT(self, passed=_MARKER):
307+
from gcloud.datastore._implicit_environ import _require_connection
308+
if passed is self._MARKER:
309+
return _require_connection()
310+
return _require_connection(passed)
311+
312+
def _monkey(self, connection):
313+
from gcloud.datastore._testing import _monkey_defaults
314+
return _monkey_defaults(connection=connection)
315+
316+
def test_implicit_unset(self):
317+
with self._monkey(None):
318+
with self.assertRaises(EnvironmentError):
319+
self._callFUT()
320+
321+
def test_implicit_unset_w_existing_batch(self):
322+
from gcloud.datastore._testing import _NoCommitBatch
323+
ID = 'DATASET'
324+
CONNECTION = object()
325+
with self._monkey(None):
326+
with _NoCommitBatch(dataset_id=ID, connection=CONNECTION):
327+
self.assertEqual(self._callFUT(), CONNECTION)
328+
329+
def test_implicit_unset_w_existing_transaction(self):
330+
from gcloud.datastore._testing import _NoCommitTransaction
331+
ID = 'DATASET'
332+
CONNECTION = object()
333+
with self._monkey(None):
334+
with _NoCommitTransaction(dataset_id=ID, connection=CONNECTION):
335+
self.assertEqual(self._callFUT(), CONNECTION)
336+
337+
def test_implicit_unset_passed_explicitly(self):
338+
CONNECTION = object()
339+
with self._monkey(None):
340+
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
341+
342+
def test_implicit_set(self):
343+
IMPLICIT_CONNECTION = object()
344+
with self._monkey(IMPLICIT_CONNECTION):
345+
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)
346+
347+
def test_implicit_set_passed_explicitly(self):
348+
IMPLICIT_CONNECTION = object()
349+
CONNECTION = object()
350+
with self._monkey(IMPLICIT_CONNECTION):
351+
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
352+
353+
302354
class Test_set_default_connection(unittest2.TestCase):
303355

304356
def setUp(self):

gcloud/datastore/test_api.py

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,31 @@ def test_implicit_unset_w_keys(self):
4141
self.assertEqual(self._callFUT(first_key=_Key(ID)), ID)
4242

4343
def test_implicit_unset_w_existing_batch_wo_keys(self):
44+
from gcloud.datastore._testing import _NoCommitBatch
4445
ID = 'DATASET'
4546
with self._monkey(None):
4647
with _NoCommitBatch(dataset_id=ID, connection=object()):
4748
self.assertEqual(self._callFUT(), ID)
4849

4950
def test_implicit_unset_w_existing_batch_w_keys(self):
5051
from gcloud.datastore.test_batch import _Key
52+
from gcloud.datastore._testing import _NoCommitBatch
5153
ID = 'DATASET'
5254
OTHER = 'OTHER'
5355
with self._monkey(None):
5456
with _NoCommitBatch(dataset_id=ID, connection=object()):
5557
self.assertEqual(self._callFUT(first_key=_Key(OTHER)), ID)
5658

5759
def test_implicit_unset_w_existing_transaction_wo_keys(self):
60+
from gcloud.datastore._testing import _NoCommitTransaction
5861
ID = 'DATASET'
5962
with self._monkey(None):
6063
with _NoCommitTransaction(dataset_id=ID, connection=object()):
6164
self.assertEqual(self._callFUT(), ID)
6265

6366
def test_implicit_unset_w_existing_transaction_w_keys(self):
6467
from gcloud.datastore.test_batch import _Key
68+
from gcloud.datastore._testing import _NoCommitTransaction
6569
ID = 'DATASET'
6670
OTHER = 'OTHER'
6771
with self._monkey(None):
@@ -108,56 +112,6 @@ def test_id_implicit_set_passed_explicitly_w_keys(self):
108112
self.assertEqual(self._callFUT(ID, first_key=_Key(OTHER)), ID)
109113

110114

111-
class Test__require_connection(unittest2.TestCase):
112-
113-
_MARKER = object()
114-
115-
def _callFUT(self, passed=_MARKER):
116-
from gcloud.datastore.api import _require_connection
117-
if passed is self._MARKER:
118-
return _require_connection()
119-
return _require_connection(passed)
120-
121-
def _monkey(self, connection):
122-
from gcloud.datastore._testing import _monkey_defaults
123-
return _monkey_defaults(connection=connection)
124-
125-
def test_implicit_unset(self):
126-
with self._monkey(None):
127-
with self.assertRaises(EnvironmentError):
128-
self._callFUT()
129-
130-
def test_implicit_unset_w_existing_batch(self):
131-
ID = 'DATASET'
132-
CONNECTION = object()
133-
with self._monkey(None):
134-
with _NoCommitBatch(dataset_id=ID, connection=CONNECTION):
135-
self.assertEqual(self._callFUT(), CONNECTION)
136-
137-
def test_implicit_unset_w_existing_transaction(self):
138-
ID = 'DATASET'
139-
CONNECTION = object()
140-
with self._monkey(None):
141-
with _NoCommitTransaction(dataset_id=ID, connection=CONNECTION):
142-
self.assertEqual(self._callFUT(), CONNECTION)
143-
144-
def test_implicit_unset_passed_explicitly(self):
145-
CONNECTION = object()
146-
with self._monkey(None):
147-
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
148-
149-
def test_implicit_set(self):
150-
IMPLICIT_CONNECTION = object()
151-
with self._monkey(IMPLICIT_CONNECTION):
152-
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)
153-
154-
def test_implicit_set_passed_explicitly(self):
155-
IMPLICIT_CONNECTION = object()
156-
CONNECTION = object()
157-
with self._monkey(IMPLICIT_CONNECTION):
158-
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
159-
160-
161115
class Test_get_function(unittest2.TestCase):
162116

163117
def setUp(self):
@@ -499,6 +453,7 @@ def test_implicit_wo_transaction(self):
499453
def test_w_transaction(self):
500454
from gcloud.datastore.key import Key
501455
from gcloud.datastore.test_connection import _Connection
456+
from gcloud.datastore._testing import _NoCommitTransaction
502457

503458
DATASET_ID = 'DATASET'
504459
KIND = 'Kind'
@@ -661,6 +616,7 @@ def test_existing_batch_w_completed_key(self):
661616
from gcloud.datastore.test_batch import _Connection
662617
from gcloud.datastore.test_batch import _Entity
663618
from gcloud.datastore.test_batch import _Key
619+
from gcloud.datastore._testing import _NoCommitBatch
664620

665621
# Build basic mocks needed to delete.
666622
_DATASET = 'DATASET'
@@ -687,6 +643,7 @@ def test_implicit_connection(self):
687643
from gcloud.datastore.test_batch import _Connection
688644
from gcloud.datastore.test_batch import _Entity
689645
from gcloud.datastore.test_batch import _Key
646+
from gcloud.datastore._testing import _NoCommitBatch
690647

691648
# Build basic mocks needed to delete.
692649
_DATASET = 'DATASET'
@@ -804,6 +761,7 @@ def test_wo_batch_w_key_different_than_default_dataset_id(self):
804761
def test_w_existing_batch(self):
805762
from gcloud.datastore.test_batch import _Connection
806763
from gcloud.datastore.test_batch import _Key
764+
from gcloud.datastore._testing import _NoCommitBatch
807765

808766
# Build basic mocks needed to delete.
809767
_DATASET = 'DATASET'
@@ -825,6 +783,7 @@ def test_w_existing_batch(self):
825783
def test_w_existing_transaction(self):
826784
from gcloud.datastore.test_batch import _Connection
827785
from gcloud.datastore.test_batch import _Key
786+
from gcloud.datastore._testing import _NoCommitTransaction
828787

829788
# Build basic mocks needed to delete.
830789
_DATASET = 'DATASET'
@@ -847,6 +806,7 @@ def test_implicit_connection_and_dataset_id(self):
847806
from gcloud.datastore._testing import _monkey_defaults
848807
from gcloud.datastore.test_batch import _Connection
849808
from gcloud.datastore.test_batch import _Key
809+
from gcloud.datastore._testing import _NoCommitBatch
850810

851811
# Build basic mocks needed to delete.
852812
_DATASET = 'DATASET'
@@ -918,39 +878,6 @@ def test_with_already_completed_key(self):
918878
COMPLETE_KEY, 2)
919879

920880

921-
class _NoCommitBatch(object):
922-
923-
def __init__(self, dataset_id, connection):
924-
from gcloud.datastore.batch import Batch
925-
self._batch = Batch(dataset_id, connection)
926-
927-
def __enter__(self):
928-
from gcloud.datastore.connection import _CONNECTIONS
929-
_CONNECTIONS.push(self._batch)
930-
return self._batch
931-
932-
def __exit__(self, *args):
933-
from gcloud.datastore.connection import _CONNECTIONS
934-
_CONNECTIONS.pop()
935-
936-
937-
class _NoCommitTransaction(object):
938-
939-
def __init__(self, dataset_id, connection, transaction_id='TRANSACTION'):
940-
from gcloud.datastore.transaction import Transaction
941-
xact = self._transaction = Transaction(dataset_id, connection)
942-
xact._id = transaction_id
943-
944-
def __enter__(self):
945-
from gcloud.datastore.connection import _CONNECTIONS
946-
_CONNECTIONS.push(self._transaction)
947-
return self._transaction
948-
949-
def __exit__(self, *args):
950-
from gcloud.datastore.connection import _CONNECTIONS
951-
_CONNECTIONS.pop()
952-
953-
954881
class _HttpMultiple(object):
955882

956883
def __init__(self, *responses):

gcloud/datastore/test_transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_ctor_with_env(self):
7373
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
7474

7575
def test_current(self):
76-
from gcloud.datastore.test_api import _NoCommitBatch
76+
from gcloud.datastore._testing import _NoCommitBatch
7777
_DATASET = 'DATASET'
7878
connection = _Connection()
7979
xact1 = self._makeOne(_DATASET, connection)

0 commit comments

Comments
 (0)