@@ -24,28 +24,50 @@ def _get_target_class():
24
24
25
25
return Transaction
26
26
27
- def _get_options_class (self , ** kw ):
27
+ def _make_one (self , client , ** kw ):
28
+ return self ._get_target_class ()(client , ** kw )
29
+
30
+ def _make_options (self , read_only = False , previous_transaction = None ):
28
31
from google .cloud .datastore_v1 .types import TransactionOptions
29
32
30
- return TransactionOptions
33
+
10000
span> kw = {}
31
34
32
- def _make_one ( self , client , ** kw ) :
33
- return self . _get_target_class ()( client , ** kw )
35
+ if read_only :
36
+ kw [ "read_only" ] = TransactionOptions . ReadOnly ( )
34
37
35
- def _make_options (self , ** kw ):
36
- return self ._get_options_class ()(** kw )
38
+ return TransactionOptions (** kw )
37
39
38
40
def test_ctor_defaults (self ):
39
41
project = "PROJECT"
40
42
client = _Client (project )
43
+
41
44
xact = self ._make_one (client )
45
+
42
46
self .assertEqual (xact .project , project )
43
47
self .assertIs (xact ._client , client )
44
48
self .assertIsNone (xact .id )
45
49
self .assertEqual (xact ._status , self ._get_target_class ()._INITIAL )
46
50
self .assertEqual (xact ._mutations , [])
47
51
self .assertEqual (len (xact ._partial_key_entities ), 0 )
48
52
53
+ def test_constructor_read_only (self ):
54
+ project = "PROJECT"
55
+ id_ = 850302
56
+ ds_api = _make_datastore_api (xact = id_ )
57
+ client = _Client (project , datastore_api = ds_api )
58
+ options = self ._make_options (read_only = True )
59
+
60
+ xact = self ._make_one (client , read_only = True )
61
+
62
+ self .assertEqual (xact ._options , options )
63
+
64
+ def _make_begin_request (self , project , read_only = False ):
65
+ expected_options = self ._make_options (read_only = read_only )
66
+ return {
67
+ "project_id" : project ,
68
+ "transaction_options" : expected_options ,
69
+ }
70
+
49
71
def test_current (self ):
50
72
from google .cloud .datastore_v1 .types import datastore as datastore_pb2
51
73
@@ -57,24 +79,34 @@ def test_current(self):
57
79
xact2 = self ._make_one (client )
58
80
self .assertIsNone (xact1 .current ())
59
81
self .assertIsNone (xact2 .current ())
82
+
60
83
with xact1 :
61
84
self .assertIs (xact1 .current (), xact1 )
62
85
self .assertIs (xact2 .current (), xact1 )
86
+
63
87
with _NoCommitBatch (client ):
64
88
self .assertIsNone (xact1 .current ())
65
89
self .assertIsNone (xact2 .current ())
90
+
66
91
with xact2 :
67
92
self .assertIs (xact1 .current (), xact2 )
68
93
self .assertIs (xact2 .current (), xact2 )
94
+
69
95
with _NoCommitBatch (client ):
70
96
self .assertIsNone (xact1 .current ())
71
97
self .assertIsNone (xact2 .current ())
98
+
72
99
self .assertIs (xact1 .current (), xact1 )
73
100
self .assertIs (xact2 .current (), xact1 )
101
+
74
102
self .assertIsNone (xact1 .current ())
75
103
self .assertIsNone (xact2 .current ())
76
104
77
- ds_api .rollback .assert_not_called ()
105
+ begin_txn = ds_api .begin_transaction
106
+ self .assertEqual (begin_txn .call_count , 2 )
107
+ expected_request = self ._make_begin_request (project )
108
+ begin_txn .assert_called_with (request = expected_request )
109
+
78
110
commit_method = ds_api .commit
79
111
self .assertEqual (commit_method .call_count , 2 )
80
112
mode = datastore_pb2 .CommitRequest .Mode .TRANSACTIONAL
@@ -87,21 +119,35 @@ def test_current(self):
87
119
}
88
120
)
89
121
90
- begin_txn = ds_api .begin_transaction
91
- self .assertEqual (begin_txn .call_count , 2 )
92
- begin_txn .assert_called_with (request = {"project_id" : project })
122
+ ds_api .rollback .assert_not_called ()
93
123
94
124
def test_begin (self ):
95
125
project = "PROJECT"
96
126
id_ = 889
97
127
ds_api = _make_datastore_api (xact_id = id_ )
98
128
client = _Client (project , datastore_api = ds_api )
99
129
xact = self ._make_one (client )
130
+
100
131
xact .begin ()
132
+
101
133
self .assertEqual (xact .id , id_ )
102
- ds_api .begin_transaction .assert_called_once_with (
103
- request = {"project_id" : project }
104
- )
134
+
135
+ expected_request = self ._make_begin_request (project )
136
+ ds_api .begin_transaction .assert_called_once_with (request = expected_request )
137
+
138
+ def test_begin_w_readonly (self ):
139
+ project = "PROJECT"
140
+ id_ = 889
141
+ ds_api = _make_datastore_api (xact_id = id_ )
142
+ client = _Client (project , datastore_api = ds_api )
143
+ xact = self ._make_one (client , read_only = True )
144
+
145
+ xact .begin ()
146
+
147
+ self .assertEqual (xact .id , id_ )
148
+
149
+ expected_request = self ._make_begin_request (project , read_only = True )
150
+ ds_api .begin_transaction .assert_called_once_with (request = expected_request )
105
151
106
152
def test_begin_w_retry_w_timeout (self ):
107
153
project = "PROJECT"
@@ -116,8 +162,10 @@ def test_begin_w_retry_w_timeout(self):
116
162
xact .begin (retry = retry , timeout = timeout )
117
163
118
164
self .assertEqual (xact .id , id_ )
165
+
166
+ expected_request = self ._make_begin_request (project )
119
167
ds_api .begin_transaction .assert_called_once_with (
120
- request = { "project_id" : project } , retry = retry , timeout = timeout
168
+ request = expected_request , retry = retry , timeout = timeout ,
121
169
)
122
170
123
171
def test_begin_tombstoned (self ):
@@ -126,19 +174,23 @@ def test_begin_tombstoned(self):
126
174
ds_api = _make_datastore_api (xact_id = id_ )
127
175
client = _Client (project , datastore_api = ds_api )
128
176
xact = self ._make_one (client )
177
+
129
178
xact .begin ()
179 +
130
180
self .assertEqual (xact .id , id_ )
131
- ds_api . begin_transaction . assert_called_once_with (
132
- request = { "project_id" : project }
133
- )
181
+
182
+ expected_request = self . _make_begin_request ( project )
183
+ ds_api . begin_transaction . assert_called_once_with ( request = expected_request )
134
184
135
185
xact .rollback ()
186
+
136
187
client ._datastore_api .rollback .assert_called_once_with (
137
188
request = {"project_id" : project , "transaction" : id_ }
138
189
)
139
190
self .assertIsNone (xact .id )
140
191
141
- self .assertRaises (ValueError , xact .begin )
192
+ with self .assertRaises (ValueError ):
193
+ xact .begin ()
142
194
143
195
def test_begin_w_begin_transaction_failure (self ):
144
196
project = "PROJECT"
@@ -152,9 +204,9 @@ def test_begin_w_begin_transaction_failure(self):
152
204
xact .begin ()
153
205
154
206
self .assertIsNone (xact .id )
155
- ds_api . begin_transaction . assert_called_once_with (
156
- request = { "project_id" : project }
157
- )
207
+
208
+ expected_request = self . _make_begin_request ( project )
209
+ ds_api . begin_transaction . assert_called_once_with ( request = expected_request )
158
210
159
211
def test_rollback (self ):
160
212
project = "PROJECT"
@@ -256,11 +308,14 @@ def test_context_manager_no_raise(self):
256
308
ds_api = _make_datastore_api (xact_id = id_ )
257
309
client = _Client (project , datastore_api = ds_api )
258
310
xact = self ._make_one (client )
311
+
259
312
with xact :
260
- self .assertEqual (xact .id , id_ )
261
- ds_api .begin_transaction .assert_called_once_with (
262
- request = {"project_id" : project }
263
- )
313
+ self .assertEqual (xact .id , id_ ) # only set between begin / commit
314
+
315
+ self .assertIsNone (xact .id )
316
+
317
+ expected_request = self ._make_begin_request (project )
318
+ ds_api .begin_transaction .assert_called_once_with (request = expected_request )
264
319
265
320
mode = datastore_pb2 .CommitRequest .Mode .TRANSACTIONAL
266
321
client ._datastore_api .commit .assert_called_once_with (
@@ -272,9 +327,6 @@ def test_context_manager_no_raise(self):
272
327
},
273
328
)
274
329
275
- self .assertIsNone (xact .id )
276
- self .assertEqual (ds_api .begin_transaction .call_count , 1 )
277
-
278
330
def test_context_manager_w_raise (self ):
279
331
class Foo (Exception ):
280
332
pass
@@ -288,29 +340,20 @@ class Foo(Exception):
288
340
try :
289
341
with xact :
290
342
self .assertEqual (xact .id , id_ )
291
- ds_api .begin_transaction .assert_called_once_with (
292
- request = {"project_id" : project }
293
- )
294
343
raise Foo ()
295
344
except Foo :
296
- self .assertIsNone (xact .id )
297
- client ._datastore_api .rollback .assert_called_once_with (
298
- request = {"project_id" : project , "transaction" : id_ }
299
- )
345
+ pass
300
346
301
- client ._datastore_api .commit .assert_not_called ()
302
347
self .assertIsNone (xact .id )
303
- self .assertEqual (ds_api .begin_transaction .call_count , 1 )
304
348
305
- def test_constructor_read_only (self ):
306
- project = "PROJECT"
307
- id_ = 850302
308
- ds_api = _make_datastore_api (xact = id_ )
309
- client = _Client (project , datastore_api = ds_api )
310
- read_only = self ._get_options_class ().ReadOnly ()
311
- options = self ._make_options (read_only = read_only )
312
- xact = self ._make_one (client , read_only = True )
313
- self .assertEqual (xact ._options , options )
349
+ expected_request = self ._make_begin_request (project )
350
+ ds_api .begin_transaction .assert_called_once_with (request = expected_request )
351
+
352
+ client ._datastore_api .commit .assert_not_called ()
353
+
354
+ client ._datastore_api .rollback .assert_called_once_with (
355
+ request = {"project_id" : project , "transaction" : id_ }
356
+ )
314
357
315
358
def test_put_read_only (self ):
316
359
project = "PROJECT"
0 commit comments