Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #50 - Missing time import. #53

Merged
merged 1 commit into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))