|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import mock |
15 | 16 | import unittest |
16 | 17 |
|
17 | | -import mock |
| 18 | + |
| 19 | +class TestTransactionOptions(unittest.TestCase): |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def _get_target_class(): |
| 23 | + from google.cloud.datastore_v1.types import TransactionOptions |
| 24 | + return TransactionOptions |
| 25 | + |
| 26 | + def _make_one(self, **kw): |
| 27 | + return self._get_target_class()(**kw) |
| 28 | + |
| 29 | + def test_read_only(self): |
| 30 | + options = self._make_one(read_only=self._get_target_class().ReadOnly()) |
| 31 | + self.assertTrue(options.HasField('read_only')) |
| 32 | + |
| 33 | + def test_read_write(self): |
| 34 | + options = self._make_one(read_write= |
| 35 | + self._get_target_class().ReadWrite()) |
| 36 | + self.assertTrue(options.HasField('read_write')) |
| 37 | + |
| 38 | + def test_previous_transaction(self): |
| 39 | + previous_transaction = b'234' |
| 40 | + options = self._make_one( |
| 41 | + read_write=self._get_target_class().ReadWrite( |
| 42 | + previous_transaction=previous_transaction)) |
| 43 | + self.assertEqual(options.read_write.previous_transaction, |
| 44 | + previous_transaction) |
18 | 45 |
|
19 | 46 |
|
20 | 47 | class TestTransaction(unittest.TestCase): |
21 | 48 |
|
22 | 49 | @staticmethod |
23 | 50 | def _get_target_class(): |
24 | 51 | from google.cloud.datastore.transaction import Transaction |
25 | | - |
26 | 52 | return Transaction |
27 | 53 |
|
| 54 | + def _get_options_class(self, **kw): |
| 55 | + from google.cloud.datastore_v1.types import TransactionOptions |
| 56 | + return TransactionOptions |
| 57 | + |
28 | 58 | def _make_one(self, client, **kw): |
29 | 59 | return self._get_target_class()(client, **kw) |
30 | 60 |
|
| 61 | + def _make_options(self, **kw): |
| 62 | + return self._get_options_class()(**kw) |
| 63 | + |
31 | 64 | def test_ctor_defaults(self): |
32 | 65 | project = 'PROJECT' |
33 | 66 | client = _Client(project) |
@@ -212,6 +245,95 @@ class Foo(Exception): |
212 | 245 | self.assertIsNone(xact.id) |
213 | 246 | self.assertEqual(ds_api.begin_transaction.call_count, 1) |
214 | 247 |
|
| 248 | + def test_default(self): |
| 249 | + project = 'PROJECT' |
| 250 | + id_ = 850302 |
| 251 | + ds_api = _make_datastore_api(xact=id_) |
| 252 | + client = _Client(project, datastore_api=ds_api) |
| 253 | + xact = self._make_one(client) |
| 254 | + self.assertTrue(isinstance(xact.options, self._get_options_class())) |
| 255 | + |
| 256 | + def test_read_only(self): |
| 257 | + project = 'PROJECT' |
| 258 | + id_ = 850302 |
| 259 | + ds_api = _make_datastore_api(xact=id_) |
| 260 | + client = _Client(project, datastore_api=ds_api) |
| 261 | + options = self._make_options(read_only=self._make_options().ReadOnly()) |
| 262 | + xact = self._make_one(client, options=options) |
| 263 | + xact.begin() |
| 264 | + begin = ds_api.begin_transaction |
| 265 | + begin.assert_called_once() |
| 266 | + self.assertEqual(begin.call_count, 1) |
| 267 | + |
| 268 | + def test_read_write(self): |
| 269 | + project = 'PROJECT' |
| 270 | + id_ = 850304 |
| 271 | + ds_api = _make_datastore_api(xact_id=id_) |
| 272 | + client = _Client(project, datastore_api=ds_api) |
| 273 | + options = self._make_options(read_write= |
| 274 | + self._make_options().ReadWrite()) |
| 275 | + xact = self._make_one(client, options=options) |
| 276 | + xact.begin() |
| 277 | + begin = ds_api.begin_transaction |
| 278 | + begin.assert_called_once() |
| 279 | + self.assertEqual(begin.call_count, 1) |
| 280 | + |
| 281 | + def test_previous_tid(self): |
| 282 | + project = 'PROJECT' |
| 283 | + id_ = 943243 |
| 284 | + ds_api = _make_datastore_api(xact_id=id_) |
| 285 | + client = _Client(project, datastore_api=ds_api) |
| 286 | + options = self._make_options(read_write= |
| 287 | + self._make_options().ReadWrite( |
| 288 | + previous_transaction=b'321')) |
| 289 | + xact = self._make_one(client, options=options) |
| 290 | + xact.begin() |
| 291 | + begin = ds_api.begin_transaction |
| 292 | + begin.assert_called_once() |
| 293 | + self.assertEqual(begin.call_count, 1) |
| 294 | + |
| 295 | + def test_put_read_only(self): |
| 296 | + project = 'PROJECT' |
| 297 | + id_ = 943243 |
| 298 | + ds_api = _make_datastore_api(xact_id=id_) |
| 299 | + client = _Client(project, datastore_api=ds_api) |
| 300 | + options = self._make_options(read_only=self._make_options().ReadOnly()) |
| 301 | + entity = _Entity() |
| 302 | + xact = self._make_one(client, options=options) |
| 303 | + xact.begin() |
| 304 | + with self.assertRaises(RuntimeError): |
| 305 | + xact.put(entity) |
| 306 | + |
| 307 | + def test_put_read_write(self): |
| 308 | + project = 'PROJECT' |
| 309 | + id_ = 943243 |
| 310 | + ds_api = _make_datastore_api(xact_id=id_) |
| 311 | + client = _Client(project, datastore_api=ds_api) |
| 312 | + options = self._make_options(read_write= |
| 313 | + self._make_options().ReadWrite( |
| 314 | + previous_transaction=b'321')) |
| 315 | + entity = _Entity() |
| 316 | + xact = self._make_one(client, options=options) |
| 317 | + xact.begin() |
| 318 | + xact.put(entity) |
| 319 | + mutated_entity = _mutated_pb(self, xact.mutations, 'insert') |
| 320 | + self.assertEqual(mutated_entity.key, entity.key.to_protobuf()) |
| 321 | + |
| 322 | + |
| 323 | +def _assert_num_mutations(test_case, mutation_pb_list, num_mutations): |
| 324 | + test_case.assertEqual(len(mutation_pb_list), num_mutations) |
| 325 | + |
| 326 | +def _mutated_pb(test_case, mutation_pb_list, mutation_type): |
| 327 | + # Make sure there is only one mutation. |
| 328 | + _assert_num_mutations(test_case, mutation_pb_list, 1) |
| 329 | + |
| 330 | + # We grab the only mutation. |
| 331 | + mutated_pb = mutation_pb_list[0] |
| 332 | + # Then check if it is the correct type. |
| 333 | + test_case.assertEqual(mutated_pb.WhichOneof('operation'), |
| 334 | + mutation_type) |
| 335 | + |
| 336 | + return getattr(mutated_pb, mutation_type) |
215 | 337 |
|
216 | 338 | def _make_key(kind, id_, project): |
217 | 339 | from google.cloud.datastore_v1.proto import entity_pb2 |
@@ -281,7 +403,6 @@ def _make_commit_response(*keys): |
281 | 403 | def _make_datastore_api(*keys, **kwargs): |
282 | 404 | commit_method = mock.Mock( |
283 | 405 | return_value=_make_commit_response(*keys), spec=[]) |
284 | | - |
285 | 406 | xact_id = kwargs.pop('xact_id', 123) |
286 | 407 | txn_pb = mock.Mock( |
287 | 408 | transaction=xact_id, spec=['transaction']) |
|
0 commit comments