@@ -29,24 +29,28 @@ class Transaction(Batch):
2929
3030 .. testsetup:: txn-put-multi, txn-api
3131
32- from google.cloud import datastore
33- from tests.system.test_system import Config # system tests
32+ import os
33+ import uuid
3434
35- client = datastore.Client()
36- key1 = client.key('_Doctest')
37- entity1 = datastore.Entity(key=key1)
38- entity1['foo'] = 1337
35+ from google.cloud import datastore
36+ from tests.system.test_system import Config # system tests
3937
40- key2 = client.key('_Doctest', 'abcd1234')
41- entity2 = datastore.Entity(key=key2)
42- entity2['foo'] = 42
38+ unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
39+ client = datastore.Client(namespace='ns{}'.format(unique))
40+ key1 = client.key('_Doctest')
41+ entity1 = datastore.Entity(key=key1)
42+ entity1['foo'] = 1337
4343
44- Config.TO_DELETE.extend([entity1, entity2])
44+ key2 = client.key('_Doctest', 'abcd1234')
45+ entity2 = datastore.Entity(key=key2)
46+ entity2['foo'] = 42
47+
48+ Config.TO_DELETE.extend([entity1, entity2])
4549
4650 .. doctest:: txn-put-multi
4751
48- >>> with client.transaction():
49- ... client.put_multi([entity1, entity2])
52+ >>> with client.transaction():
53+ ... client.put_multi([entity1, entity2])
5054
5155 Because it derives from :class:`~google.cloud.datastore.batch.Batch`,
5256 :class:`Transaction` also provides :meth:`put` and :meth:`delete` methods:
@@ -62,51 +66,59 @@ class Transaction(Batch):
6266
6367 .. testsetup:: txn-error
6468
65- from google.cloud import datastore
69+ import os
70+ import uuid
71+
72+ from google.cloud import datastore
6673
67- client = datastore.Client()
74+ unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
75+ client = datastore.Client(namespace='ns{}'.format(unique))
6876
69- def do_some_work():
70- return
77+ def do_some_work():
78+ return
7179
72- class SomeException(Exception):
73- pass
80+ class SomeException(Exception):
81+ pass
7482
7583 .. doctest:: txn-error
7684
77- >>> with client.transaction():
78- ... do_some_work()
79- ... raise SomeException # rolls back
80- Traceback (most recent call last):
81- ...
82- SomeException
85+ >>> with client.transaction():
86+ ... do_some_work()
87+ ... raise SomeException # rolls back
88+ Traceback (most recent call last):
89+ ...
90+ SomeException
8391
8492 If the transaction block exits without an exception, it will commit
8593 by default.
8694
8795 .. warning::
8896
89- Inside a transaction, automatically assigned IDs for
90- entities will not be available at save time! That means, if you
91- try:
97+ Inside a transaction, automatically assigned IDs for
98+ entities will not be available at save time! That means, if you
99+ try:
100+
101+ .. testsetup:: txn-entity-key, txn-entity-key-after, txn-manual
92102
93- .. testsetup:: txn-entity-key, txn-entity-key-after, txn-manual
103+ import os
104+ import uuid
94105
95- from google.cloud import datastore
96- from tests.system.test_system import Config # system tests
106+ from google.cloud import datastore
107+ from tests.system.test_system import Config # system tests
97108
98- client = datastore.Client()
109+ unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
110+ client = datastore.Client(namespace='ns{}'.format(unique))
99111
100- def Entity(*args, **kwargs):
101- entity = datastore.Entity(*args, **kwargs)
102- Config.TO_DELETE.append(entity)
103- return entity
112+ def Entity(*args, **kwargs):
113+ entity = datastore.Entity(*args, **kwargs)
114+ Config.TO_DELETE.append(entity)
115+ return entity
104116
105- .. doctest:: txn-entity-key
117+ .. doctest:: txn-entity-key
106118
107- >>> with client.transaction():
108- ... entity = Entity(key=client.key('Thing'))
109- ... client.put(entity)
119+ >>> with client.transaction():
120+ ... entity = Entity(key=client.key('Thing'))
121+ ... client.put(entity)
110122
111123 ``entity`` won't have a complete key until the transaction is
112124 committed.
0 commit comments