Skip to content

Commit

Permalink
Merge pull request googleapis#53 from jgeewax/datastore-helper-missin…
Browse files Browse the repository at this point in the history
…g-import

Fix googleapis#50 - Missing time import.
  • Loading branch information
jgeewax committed Feb 25, 2014
2 parents 26d2125 + f6979f5 commit 2c26d9b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Helper methods for dealing with Cloud Datastore's Protobuf API."""
from datetime import datetime
import time

import pytz

Expand Down
20 changes: 12 additions & 8 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ def from_protobuf(cls, pb, dataset=None):
def to_protobuf(self):
key = datastore_pb.Key()

# Apparently 's~' is a prefix for High-Replication and is necessary here.
# Another valid preflix is 'e~' indicating EU datacenters.
dataset_id = self.dataset().id()
if dataset_id:
if dataset_id[:2] not in ['s~', 'e~']:
dataset_id = 's~' + dataset_id

key.partition_id.dataset_id = dataset_id
# Technically a dataset is required to do anything with the key,
# but we shouldn't throw a cryptic error if one isn't provided
# in the initializer.
if self.dataset():
# Apparently 's~' is a prefix for High-Replication and is necessary here.
# Another valid preflix is 'e~' indicating EU datacenters.
dataset_id = self.dataset().id()
if dataset_id:
if dataset_id[:2] not in ['s~', 'e~']:
dataset_id = 's~' + dataset_id

key.partition_id.dataset_id = dataset_id

if self._namespace:
key.partition_id.namespace = self._namespace
Expand Down
48 changes: 48 additions & 0 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from datetime import datetime
import time

import unittest2

from gcloud.datastore import helpers
from gcloud.datastore.key import Key


class TestHelpers(unittest2.TestCase):

def test_get_protobuf_attribute(self):
mapping = (
(str(), 'string_value'),
(unicode(), 'string_value'),
(int(), 'integer_value'),
(long(), 'integer_value'),
(float(), 'double_value'),
(bool(), 'boolean_value'),
(datetime.now(), 'timestamp_microseconds_value'),
(Key(), 'key_value'),
)

for test_value, expected_name in mapping:
actual_name, _ = helpers.get_protobuf_attribute_and_value(test_value)
self.assertEqual(expected_name, actual_name,
'Test value "%s" expected %s, got %s' % (
test_value, expected_name, actual_name))

def test_get_protobuf_value(self):
now = datetime.now()

mapping = (
(str('string'), 'string'),
(unicode('string'), 'string'),
(int(), int()),
(long(), int()),
(float(), float()),
(bool(), bool()),
(now, time.mktime(now.timetuple())),
(Key(), Key().to_protobuf()),
)

for test_value, expected_value in mapping:
_, actual_value = helpers.get_protobuf_attribute_and_value(test_value)
self.assertEqual(expected_value, actual_value,
'Test value "%s" expected %s, got %s.' % (
test_value, expected_value, actual_value))

0 comments on commit 2c26d9b

Please sign in to comment.