Repository: googleapis/google-cloud-python

Issue:
Entities created in datastore snippets should use a namespace
This is useful for debugging build failures. This is [already done][1] for the rest of the entities created in the datastore system test.

[1]: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/e63ddc384e4a54a6a085595d779a399ed449c26c/datastore/tests/system/test_system.py#L52-L64


Discussion:

Resolving patch:
diff --git a/datastore/google/cloud/datastore/client.py b/datastore/google/cloud/datastore/client.py
--- a/datastore/google/cloud/datastore/client.py
+++ b/datastore/google/cloud/datastore/client.py
@@ -504,56 +504,64 @@ def query(self, **kwargs):
 
         .. testsetup:: query
 
-           from google.cloud import datastore
+            import os
+            import uuid
 
-           client = datastore.Client()
-           query = client.query(kind='_Doctest')
+            from google.cloud import datastore
 
-           def do_something(entity):
-               pass
+            unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+            client = datastore.Client(namespace='ns{}'.format(unique))
+            query = client.query(kind='_Doctest')
+
+            def do_something(entity):
+                pass
 
         .. doctest:: query
 
-           >>> query = client.query(kind='MyKind')
-           >>> query.add_filter('property', '=', 'val')
+            >>> query = client.query(kind='MyKind')
+            >>> query.add_filter('property', '=', 'val')
 
         Using the query iterator
 
         .. doctest:: query
 
-           >>> query_iter = query.fetch()
-           >>> for entity in query_iter:
-           ...     do_something(entity)
+            >>> query_iter = query.fetch()
+            >>> for entity in query_iter:
+            ...     do_something(entity)
 
         or manually page through results
 
         .. testsetup:: query-page
 
-           from google.cloud import datastore
-           from tests.system.test_system import Config  # system tests
+            import os
+            import uuid
+
+            from google.cloud import datastore
+            from tests.system.test_system import Config  # system tests
 
-           client = datastore.Client()
+            unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+            client = datastore.Client(namespace='ns{}'.format(unique))
 
-           key = client.key('_Doctest')
-           entity1 = datastore.Entity(key=key)
-           entity1['foo'] = 1337
-           entity2 = datastore.Entity(key=key)
-           entity2['foo'] = 42
-           Config.TO_DELETE.extend([entity1, entity2])
-           client.put_multi([entity1, entity2])
+            key = client.key('_Doctest')
+            entity1 = datastore.Entity(key=key)
+            entity1['foo'] = 1337
+            entity2 = datastore.Entity(key=key)
+            entity2['foo'] = 42
+            Config.TO_DELETE.extend([entity1, entity2])
+            client.put_multi([entity1, entity2])
 
-           query = client.query(kind='_Doctest')
-           cursor = None
+            query = client.query(kind='_Doctest')
+            cursor = None
 
         .. doctest:: query-page
 
-           >>> query_iter = query.fetch(start_cursor=cursor)
-           >>> pages = query_iter.pages
-           >>>
-           >>> first_page = next(pages)
-           >>> first_page_entities = list(first_page)
-           >>> query_iter.next_page_token
-           b'...'
+            >>> query_iter = query.fetch(start_cursor=cursor)
+            >>> pages = query_iter.pages
+            >>>
+            >>> first_page = next(pages)
+            >>> first_page_entities = list(first_page)
+            >>> query_iter.next_page_token
+            b'...'
 
         :type kwargs: dict
         :param kwargs: Parameters for initializing and instance of
diff --git a/datastore/google/cloud/datastore/entity.py b/datastore/google/cloud/datastore/entity.py
--- a/datastore/google/cloud/datastore/entity.py
+++ b/datastore/google/cloud/datastore/entity.py
@@ -42,29 +42,33 @@ class Entity(dict):
 
     .. testsetup:: entity-ctor
 
-       from google.cloud import datastore
-       from tests.system.test_system import Config  # system tests
+        import os
+        import uuid
+
+        from google.cloud import datastore
+        from tests.system.test_system import Config  # system tests
 
-       client = datastore.Client()
-       key = client.key('EntityKind', 1234, namespace='_Doctest')
-       entity = datastore.Entity(key=key)
-       entity['property'] = 'value'
-       Config.TO_DELETE.append(entity)
+        unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+        client = datastore.Client(namespace='ns{}'.format(unique))
+        key = client.key('EntityKind', 1234, namespace='_Doctest')
+        entity = datastore.Entity(key=key)
+        entity['property'] = 'value'
+        Config.TO_DELETE.append(entity)
 
-       client.put(entity)
+        client.put(entity)
 
     .. doctest:: entity-ctor
 
-       >>> client.get(key)
-       <Entity('EntityKind', 1234) {'property': 'value'}>
+        >>> client.get(key)
+        <Entity('EntityKind', 1234) {'property': 'value'}>
 
     You can the set values on the entity just like you would on any
     other dictionary.
 
     .. doctest:: entity-ctor
 
-       >>> entity['age'] = 20
-       >>> entity['name'] = 'JJ'
+        >>> entity['age'] = 20
+        >>> entity['name'] = 'JJ'
 
     However, not all types are allowed as a value for a Google Cloud Datastore
     entity. The following basic types are supported by the API:
diff --git a/datastore/google/cloud/datastore/transaction.py b/datastore/google/cloud/datastore/transaction.py
--- a/datastore/google/cloud/datastore/transaction.py
+++ b/datastore/google/cloud/datastore/transaction.py
@@ -29,24 +29,28 @@ class Transaction(Batch):
 
     .. testsetup:: txn-put-multi, txn-api
 
-       from google.cloud import datastore
-       from tests.system.test_system import Config  # system tests
+        import os
+        import uuid
 
-       client = datastore.Client()
-       key1 = client.key('_Doctest')
-       entity1 = datastore.Entity(key=key1)
-       entity1['foo'] = 1337
+        from google.cloud import datastore
+        from tests.system.test_system import Config  # system tests
 
-       key2 = client.key('_Doctest', 'abcd1234')
-       entity2 = datastore.Entity(key=key2)
-       entity2['foo'] = 42
+        unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+        client = datastore.Client(namespace='ns{}'.format(unique))
+        key1 = client.key('_Doctest')
+        entity1 = datastore.Entity(key=key1)
+        entity1['foo'] = 1337
 
-       Config.TO_DELETE.extend([entity1, entity2])
+        key2 = client.key('_Doctest', 'abcd1234')
+        entity2 = datastore.Entity(key=key2)
+        entity2['foo'] = 42
+
+        Config.TO_DELETE.extend([entity1, entity2])
 
     .. doctest:: txn-put-multi
 
-       >>> with client.transaction():
-       ...     client.put_multi([entity1, entity2])
+        >>> with client.transaction():
+        ...     client.put_multi([entity1, entity2])
 
     Because it derives from :class:`~google.cloud.datastore.batch.Batch`,
     :class:`Transaction` also provides :meth:`put` and :meth:`delete` methods:
@@ -62,51 +66,59 @@ class Transaction(Batch):
 
     .. testsetup:: txn-error
 
-       from google.cloud import datastore
+        import os
+        import uuid
+
+        from google.cloud import datastore
 
-       client = datastore.Client()
+        unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+        client = datastore.Client(namespace='ns{}'.format(unique))
 
-       def do_some_work():
-           return
+        def do_some_work():
+            return
 
-       class SomeException(Exception):
-           pass
+        class SomeException(Exception):
+            pass
 
     .. doctest:: txn-error
 
-       >>> with client.transaction():
-       ...     do_some_work()
-       ...     raise SomeException  # rolls back
-       Traceback (most recent call last):
-         ...
-       SomeException
+        >>> with client.transaction():
+        ...     do_some_work()
+        ...     raise SomeException  # rolls back
+        Traceback (most recent call last):
+          ...
+        SomeException
 
     If the transaction block exits without an exception, it will commit
     by default.
 
     .. warning::
 
-       Inside a transaction, automatically assigned IDs for
-       entities will not be available at save time!  That means, if you
-       try:
+        Inside a transaction, automatically assigned IDs for
+        entities will not be available at save time!  That means, if you
+        try:
+
+        .. testsetup:: txn-entity-key, txn-entity-key-after, txn-manual
 
-       .. testsetup:: txn-entity-key, txn-entity-key-after, txn-manual
+            import os
+            import uuid
 
-          from google.cloud import datastore
-          from tests.system.test_system import Config  # system tests
+            from google.cloud import datastore
+            from tests.system.test_system import Config  # system tests
 
-          client = datastore.Client()
+            unique = os.getenv('CIRCLE_BUILD_NUM', str(uuid.uuid4())[0:8])
+            client = datastore.Client(namespace='ns{}'.format(unique))
 
-          def Entity(*args, **kwargs):
-              entity = datastore.Entity(*args, **kwargs)
-              Config.TO_DELETE.append(entity)
-              return entity
+            def Entity(*args, **kwargs):
+                entity = datastore.Entity(*args, **kwargs)
+                Config.TO_DELETE.append(entity)
+                return entity
 
-       .. doctest:: txn-entity-key
+        .. doctest:: txn-entity-key
 
-          >>> with client.transaction():
-          ...     entity = Entity(key=client.key('Thing'))
-          ...     client.put(entity)
+            >>> with client.transaction():
+            ...     entity = Entity(key=client.key('Thing'))
+            ...     client.put(entity)
 
        ``entity`` won't have a complete key until the transaction is
        committed.


Test patch: