Skip to content
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
7 changes: 6 additions & 1 deletion gcloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,18 @@ def save(self):
transaction.add_auto_id_entity(self)

if isinstance(key_pb, datastore_pb.Key):
# Update the path (which may have been altered).
# NOTE: The underlying namespace can't have changed in a save().
# The value of the dataset ID may have changed from implicit
# (i.e. None, with the ID implied from the dataset.Dataset
# object associated with the Entity/Key), but if it was
# implicit before the save() we leave it as implicit.
path = []
for element in key_pb.path_element:
key_part = {}
for descriptor, value in element._fields.items():
key_part[descriptor.name] = value
path.append(key_part)
# Update the path (which may have been altered).
self._key = key.path(path)

return self
Expand Down
8 changes: 6 additions & 2 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ def key_from_protobuf(pb):

path.append(element_dict)

dataset_id = pb.partition_id.dataset_id or None
namespace = pb.partition_id.namespace
dataset_id = None
if pb.partition_id.HasField('dataset_id'):
dataset_id = pb.partition_id.dataset_id
namespace = None
if pb.partition_id.HasField('namespace'):
namespace = pb.partition_id.namespace

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


return Key(path, namespace, dataset_id)

Expand Down
6 changes: 4 additions & 2 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def test_wo_dataset(self):
entity_pb = datastore_pb.Entity()
entity_pb.key.partition_id.dataset_id = _DATASET_ID
entity_pb.key.path_element.add(kind=_KIND, id=_ID)
entity_pb.key.partition_id.dataset_id = _DATASET_ID
prop_pb = entity_pb.property.add()
prop_pb.name = 'foo'
prop_pb.value.string_value = 'Foo'
Expand All @@ -32,6 +31,7 @@ def test_wo_dataset(self):
self.assertEqual(entity['foo'], 'Foo')
key = entity.key()
self.assertEqual(key._dataset_id, _DATASET_ID)
self.assertEqual(key.namespace(), None)
self.assertEqual(key.kind(), _KIND)
self.assertEqual(key.id(), _ID)

Expand All @@ -45,7 +45,6 @@ def test_w_dataset(self):
entity_pb = datastore_pb.Entity()
entity_pb.key.partition_id.dataset_id = _DATASET_ID
entity_pb.key.path_element.add(kind=_KIND, id=_ID)
entity_pb.key.partition_id.dataset_id = _DATASET_ID
prop_pb = entity_pb.property.add()
prop_pb.name = 'foo'
prop_pb.value.string_value = 'Foo'
Expand All @@ -56,6 +55,7 @@ def test_w_dataset(self):
self.assertEqual(entity['foo'], 'Foo')
key = entity.key()
self.assertEqual(key._dataset_id, _DATASET_ID)
self.assertEqual(key.namespace(), None)
self.assertEqual(key.kind(), _KIND)
self.assertEqual(key.id(), _ID)

Expand Down Expand Up @@ -88,11 +88,13 @@ def test_w_dataset_id_in_pb(self):
pb = self._makePB(_DATASET)
key = self._callFUT(pb)
self.assertEqual(key._dataset_id, _DATASET)
self.assertEqual(key.namespace(), None)

def test_w_namespace_in_pb(self):
_NAMESPACE = 'NAMESPACE'
pb = self._makePB(namespace=_NAMESPACE)
key = self._callFUT(pb)
self.assertEqual(key._dataset_id, None)
self.assertEqual(key.namespace(), _NAMESPACE)

def test_w_path_in_pb(self):
Expand Down