Skip to content

Commit

Permalink
Merge pull request #1053 from dhermes/fix-1048
Browse files Browse the repository at this point in the history
Updating datastore demo for client pattern.
  • Loading branch information
dhermes committed Aug 12, 2015
2 parents d3956db + c2791ab commit 5a4e766
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
12 changes: 3 additions & 9 deletions gcloud/datastore/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@
# limitations under the License.

import os
from gcloud import datastore
from gcloud.environment_vars import TESTS_DATASET

__all__ = ['DATASET_ID']

__all__ = ['initialize', 'DATASET_ID']


DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')


def initialize():
datastore.set_default_dataset_id(DATASET_ID)
DATASET_ID = os.getenv(TESTS_DATASET)
44 changes: 21 additions & 23 deletions gcloud/datastore/demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Let's start by importing the demo module and initializing our connection.
# Let's start by importing the demo module and initializing our client.
from gcloud import datastore
from gcloud.datastore import demo

demo.initialize()

# Let's import the package containing our helper classes:
client = datastore.Client(dataset_id=demo.DATASET_ID)

# Let's create a new entity of type "Thing" and name it 'Toy':
key = datastore.Key('Thing')
key = client.key('Thing')
toy = datastore.Entity(key)
toy.update({'name': 'Toy'})

# Now let's save it to our datastore:
datastore.put(toy)
client.put(toy)

# If we look it up by its key, we should find it...
print(datastore.get(toy.key))
print(client.get(toy.key))

# And we should be able to delete it...
datastore.delete(toy.key)
client.delete(toy.key)

# Since we deleted it, if we do another lookup it shouldn't be there again:
print(datastore.get(toy.key))
print(client.get(toy.key))

# Now let's try a more advanced query.
# First, let's create some entities.
Expand All @@ -52,14 +50,14 @@
(6789, 'Computer', 13)]
sample_keys = []
for id, name, age in SAMPLE_DATA:
key = datastore.Key('Thing', id)
key = client.key('Thing', id)
sample_keys.append(key)
entity = datastore.Entity(key)
entity['name'] = name
entity['age'] = age
datastore.put(entity)
client.put(entity)
# We'll start by look at all Thing entities:
query = datastore.Query(kind='Thing')
query = client.query(kind='Thing')

# Let's look at the first two.
print(list(query.fetch(limit=2)))
Expand All @@ -74,48 +72,48 @@
print(list(query.fetch()))

# Now delete them.
datastore.delete_multi(sample_keys)
client.delete_multi(sample_keys)

# You can also work inside a transaction.
# (Check the official docs for explanations of what's happening here.)
with datastore.Transaction() as xact:
with client.transaction() as xact:
print('Creating and saving an entity...')
key = datastore.Key('Thing', 'foo')
key = client.key('Thing', 'foo')
thing = datastore.Entity(key)
thing['age'] = 10
xact.put(thing)

print('Creating and saving another entity...')
key2 = datastore.Key('Thing', 'bar')
key2 = client.key('Thing', 'bar')
thing2 = datastore.Entity(key2)
thing2['age'] = 15
xact.put(thing2)

print('Committing the transaction...')

# Now that the transaction is commited, let's delete the entities.
datastore.delete_multi([key, key2])
client.delete_multi([key, key2])

# To rollback a transaction, just call .rollback()
with datastore.Transaction() as xact:
key = datastore.Key('Thing', 'another')
with client.transaction() as xact:
key = client.key('Thing', 'another')
thing = datastore.Entity(key)
xact.put(thing)
xact.rollback()

# Let's check if the entity was actually created:
created = datastore.get(key)
created = client.get(key)
print('yes' if created else 'no')

# Remember, a key won't be complete until the transaction is commited.
# That is, while inside the transaction block, thing.key will be incomplete.
with datastore.Transaction() as xact:
key = datastore.Key('Thing') # partial
with client.transaction() as xact:
key = client.key('Thing') # partial
thing = datastore.Entity(key)
xact.put(thing)
print(thing.key) # This will still be partial

print(thing.key) # This will be complete

# Now let's delete the entity.
datastore.delete(thing.key)
client.delete(thing.key)
3 changes: 2 additions & 1 deletion gcloud/storage/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.

import os
from gcloud.environment_vars import TESTS_PROJECT

__all__ = ['PROJECT_ID']

PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
PROJECT_ID = os.getenv(TESTS_PROJECT)

0 comments on commit 5a4e766

Please sign in to comment.