Skip to content

Commit 2c26d9b

Browse files
committed
Merge pull request #53 from jgeewax/datastore-helper-missing-import
Fix #50 - Missing time import.
2 parents 26d2125 + f6979f5 commit 2c26d9b

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

gcloud/datastore/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Helper methods for dealing with Cloud Datastore's Protobuf API."""
22
from datetime import datetime
3+
import time
34

45
import pytz
56

gcloud/datastore/key.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ def from_protobuf(cls, pb, dataset=None):
4949
def to_protobuf(self):
5050
key = datastore_pb.Key()
5151

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

6165
if self._namespace:
6266
key.partition_id.namespace = self._namespace

gcloud/datastore/test_helpers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from datetime import datetime
2+
import time
3+
4+
import unittest2
5+
6+
from gcloud.datastore import helpers
7+
from gcloud.datastore.key import Key
8+
9+
10+
class TestHelpers(unittest2.TestCase):
11+
12+
def test_get_protobuf_attribute(self):
13+
mapping = (
14+
(str(), 'string_value'),
15+
(unicode(), 'string_value'),
16+
(int(), 'integer_value'),
17+
(long(), 'integer_value'),
18+
(float(), 'double_value'),
19+
(bool(), 'boolean_value'),
20+
(datetime.now(), 'timestamp_microseconds_value'),
21+
(Key(), 'key_value'),
22+
)
23+
24+
for test_value, expected_name in mapping:
25+
actual_name, _ = helpers.get_protobuf_attribute_and_value(test_value)
26+
self.assertEqual(expected_name, actual_name,
27+
'Test value "%s" expected %s, got %s' % (
28+
test_value, expected_name, actual_name))
29+
30+
def test_get_protobuf_value(self):
31+
now = datetime.now()
32+
33+
mapping = (
34+
(str('string'), 'string'),
35+
(unicode('string'), 'string'),
36+
(int(), int()),
37+
(long(), int()),
38+
(float(), float()),
39+
(bool(), bool()),
40+
(now, time.mktime(now.timetuple())),
41+
(Key(), Key().to_protobuf()),
42+
)
43+
44+
for test_value, expected_value in mapping:
45+
_, actual_value = helpers.get_protobuf_attribute_and_value(test_value)
46+
self.assertEqual(expected_value, actual_value,
47+
'Test value "%s" expected %s, got %s.' % (
48+
test_value, expected_value, actual_value))

0 commit comments

Comments
 (0)