Skip to content

Commit

Permalink
Fix bug gcloud-datastore will raise exception while attemping to save…
Browse files Browse the repository at this point in the history
… property

values set as empty list.
  • Loading branch information
lucemia committed Dec 31, 2014
1 parent 8cb41f6 commit 2c3defa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ def save_entity(self, dataset_id, key_pb, properties,
will be replaced by those passed in 'properties'; properties
not passed in 'properties' no longer be set for the entity.
.. note::
When saving an entity to the backend, property values set as
empty lists or None cannot be saved and will be ignored.
:type dataset_id: string
:param dataset_id: The dataset in which to save the entity.
Expand Down Expand Up @@ -460,6 +464,9 @@ def save_entity(self, dataset_id, key_pb, properties,
insert.key.CopyFrom(key_pb)

for name, value in properties.items():
if isinstance(value, list) and len(value) == 0:
continue

prop = insert.property.add()
# Set the name of the property.
prop.name = name
Expand Down
36 changes: 36 additions & 0 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,42 @@ def test_allocate_ids_non_empty(self):
request.ParseFromString(cw['body'])
self.assertEqual(list(request.key), before_key_pbs)

def test_save_entity_w_empty_list(self):
from gcloud.datastore.connection import datastore_pb
from gcloud.datastore.key import Key

DATASET_ID = 'DATASET'
key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
rsp_pb = datastore_pb.CommitResponse()
conn = self._makeOne()
URI = '/'.join([
conn.API_BASE_URL,
'datastore',
conn.API_VERSION,
'datasets',
DATASET_ID,
'commit',
])
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
result = conn.save_entity(DATASET_ID, key_pb,
{'foo': u'Foo', 'bar': []})
self.assertEqual(result, True)
cw = http._called_with
self._verifyProtobufCall(cw, URI, conn)
rq_class = datastore_pb.CommitRequest
request = rq_class()
request.ParseFromString(cw['body'])
self.assertEqual(request.transaction, '')
mutation = request.mutation
self.assertEqual(len(mutation.insert_auto_id), 0)
upserts = list(mutation.upsert)
self.assertEqual(len(upserts), 1)
upsert = upserts[0]
self.assertEqual(upsert.key, key_pb)
props = list(upsert.property)
self.assertEqual(len(props), 1)
self.assertNotEqual(props[0].name, 'bar')

def test_save_entity_wo_transaction_w_upsert(self):
from gcloud.datastore.connection import datastore_pb
from gcloud.datastore.key import Key
Expand Down

0 comments on commit 2c3defa

Please sign in to comment.