@@ -22,49 +22,60 @@ def _getTargetClass(self):
2222
2323 return Transaction
2424
25- def _makeOne (self , dataset = None ):
26- return self ._getTargetClass ()(dataset )
25+ def _makeOne (self , dataset_id = None , connection = None ):
26+ return self ._getTargetClass ()(dataset_id = dataset_id ,
27+ connection = connection )
28+
29+ def test_ctor_missing_required (self ):
30+ from gcloud .datastore import _implicit_environ
31+
32+ self .assertEqual (_implicit_environ .DATASET , None )
33+
34+ with self .assertRaises (ValueError ):
35+ self ._makeOne ()
36+ with self .assertRaises (ValueError ):
37+ self ._makeOne (dataset_id = object ())
38+ with self .assertRaises (ValueError ):
39+ self ._makeOne (connection = object ())
2740
2841 def test_ctor (self ):
2942 from gcloud .datastore .datastore_v1_pb2 import Mutation
3043
3144 _DATASET = 'DATASET'
3245 connection = _Connection ()
33- dataset = _Dataset (_DATASET , connection )
34- xact = self ._makeOne (dataset )
46+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
3547 self .assertEqual (xact ._dataset_id , _DATASET )
3648 self .assertEqual (xact ._connection , connection )
3749 self .assertEqual (xact .id , None )
3850 self .assertTrue (isinstance (xact .mutation , Mutation ))
3951 self .assertEqual (len (xact ._auto_id_entities ), 0 )
4052
4153 def test_ctor_with_env (self ):
54+ from gcloud ._testing import _Monkey
4255 from gcloud .datastore import _implicit_environ
4356
4457 DATASET_ID = 'DATASET'
4558 CONNECTION = _Connection ()
46- DATASET = _Dataset (DATASET_ID , CONNECTION )
4759
48- _implicit_environ .DATASET = DATASET
60+ with _Monkey (_implicit_environ , DATASET_ID = DATASET_ID ,
61+ CONNECTION = CONNECTION ):
62+ transaction = self ._makeOne ()
4963
50- transaction = self ._makeOne (dataset = None )
5164 self .assertEqual (transaction ._dataset_id , DATASET_ID )
5265 self .assertEqual (transaction ._connection , CONNECTION )
5366
5467 def test_add_auto_id_entity (self ):
5568 entity = _Entity ()
5669 _DATASET = 'DATASET'
5770 connection = _Connection ()
58- dataset = _Dataset (_DATASET , connection )
59- xact = self ._makeOne (dataset )
71+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
6072 xact .add_auto_id_entity (entity )
6173 self .assertEqual (xact ._auto_id_entities , [entity ])
6274
6375 def test_begin (self ):
6476 _DATASET = 'DATASET'
6577 connection = _Connection (234 )
66- dataset = _Dataset (_DATASET , connection )
67- xact = self ._makeOne (dataset )
78+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
6879 xact .begin ()
6980 self .assertEqual (xact .id , 234 )
7081 self .assertEqual (connection ._begun , _DATASET )
@@ -73,8 +84,7 @@ def test_begin(self):
7384 def test_rollback (self ):
7485 _DATASET = 'DATASET'
7586 connection = _Connection (234 )
76- dataset = _Dataset (_DATASET , connection )
77- xact = self ._makeOne (dataset )
87+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
7888 xact .begin ()
7989 xact .rollback ()
8090 self .assertEqual (xact .id , None )
@@ -84,8 +94,7 @@ def test_rollback(self):
8494 def test_commit_no_auto_ids (self ):
8595 _DATASET = 'DATASET'
8696 connection = _Connection (234 )
87- dataset = _Dataset (_DATASET , connection )
88- xact = self ._makeOne (dataset )
97+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
8998 xact ._mutation = mutation = object ()
9099 xact .begin ()
91100 xact .commit ()
@@ -100,8 +109,7 @@ def test_commit_w_auto_ids(self):
100109 connection = _Connection (234 )
101110 connection ._commit_result = _CommitResult (
102111 _make_key (_KIND , _ID , _DATASET ))
103- dataset = _Dataset (_DATASET , connection )
104- xact = self ._makeOne (dataset )
112+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
105113 entity = _Entity ()
106114 xact .add_auto_id_entity (entity )
107115 xact ._mutation = mutation = object ()
@@ -110,13 +118,12 @@ def test_commit_w_auto_ids(self):
110118 self .assertEqual (connection ._committed , (_DATASET , mutation ))
111119 self .assertTrue (connection ._xact is None )
112120 self .assertEqual (xact .id , None )
113- self .assertEqual (entity ._key . _path , [{'kind' : _KIND , 'id' : _ID }])
121+ self .assertEqual (entity .key . path , [{'kind' : _KIND , 'id' : _ID }])
114122
115123 def test_commit_w_already (self ):
116124 _DATASET = 'DATASET'
117125 connection = _Connection (234 )
118- dataset = _Dataset (_DATASET , connection )
119- xact = self ._makeOne (dataset )
126+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
120127 xact ._mutation = object ()
121128 xact .begin ()
122129 connection .transaction (()) # Simulate previous commit via false-ish.
@@ -128,8 +135,7 @@ def test_commit_w_already(self):
128135 def test_context_manager_no_raise (self ):
129136 _DATASET = 'DATASET'
130137 connection = _Connection (234 )
131- dataset = _Dataset (_DATASET , connection )
132- xact = self ._makeOne (dataset )
138+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
133139 xact ._mutation = mutation = object ()
134140 with xact :
135141 self .assertEqual (xact .id , 234 )
@@ -144,8 +150,7 @@ class Foo(Exception):
144150 pass
145151 _DATASET = 'DATASET'
146152 connection = _Connection (234 )
147- dataset = _Dataset (_DATASET , connection )
148- xact = self ._makeOne (dataset )
153+ xact = self ._makeOne (dataset_id = _DATASET , connection = connection )
149154 xact ._mutation = object ()
150155 try :
151156 with xact :
@@ -173,19 +178,6 @@ def _make_key(kind, id, dataset_id):
173178 return key
174179
175180
176- class _Dataset (object ):
177-
178- def __init__ (self , id , connection = None ):
179- self ._id = id
180- self ._connection = connection
181-
182- def id (self ):
183- return self ._id
184-
185- def connection (self ):
186- return self ._connection
187-
188-
189181class _Connection (object ):
190182 _marker = object ()
191183 _begun = _rolled_back = _committed = _xact = None
@@ -221,9 +213,4 @@ class _Entity(object):
221213
222214 def __init__ (self ):
223215 from gcloud .datastore .key import Key
224- self ._key = Key ('KIND' , dataset_id = 'DATASET' )
225-
226- def key (self , key = None ):
227- if key is not None :
228- self ._key = key
229- return self ._key
216+ self .key = Key ('KIND' , dataset_id = 'DATASET' )
0 commit comments