Skip to content

Commit

Permalink
Make Datastore doctests use a namespace. (googleapis#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and landrito committed Aug 22, 2017
1 parent 3a34f30 commit 5f16c1f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 80 deletions.
66 changes: 37 additions & 29 deletions datastore/google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions datastore/google/cloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
90 changes: 51 additions & 39 deletions datastore/google/cloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down

0 comments on commit 5f16c1f

Please sign in to comment.