Skip to content

Commit 2c3defa

Browse files
committed
Fix bug gcloud-datastore will raise exception while attemping to save property
values set as empty list.
1 parent 8cb41f6 commit 2c3defa

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

gcloud/datastore/connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ def save_entity(self, dataset_id, key_pb, properties,
433433
will be replaced by those passed in 'properties'; properties
434434
not passed in 'properties' no longer be set for the entity.
435435
436+
.. note::
437+
When saving an entity to the backend, property values set as
438+
empty lists or None cannot be saved and will be ignored.
439+
436440
:type dataset_id: string
437441
:param dataset_id: The dataset in which to save the entity.
438442
@@ -460,6 +464,9 @@ def save_entity(self, dataset_id, key_pb, properties,
460464
insert.key.CopyFrom(key_pb)
461465

462466
for name, value in properties.items():
467+
if isinstance(value, list) and len(value) == 0:
468+
continue
469+
463470
prop = insert.property.add()
464471
# Set the name of the property.
465472
prop.name = name

gcloud/datastore/test_connection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,42 @@ def test_allocate_ids_non_empty(self):
917917
request.ParseFromString(cw['body'])
918918
self.assertEqual(list(request.key), before_key_pbs)
919919

920+
def test_save_entity_w_empty_list(self):
921+
from gcloud.datastore.connection import datastore_pb
922+
from gcloud.datastore.key import Key
923+
924+
DATASET_ID = 'DATASET'
925+
key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
926+
rsp_pb = datastore_pb.CommitResponse()
927+
conn = self._makeOne()
928+
URI = '/'.join([
929+
conn.API_BASE_URL,
930+
'datastore',
931+
conn.API_VERSION,
932+
'datasets',
933+
DATASET_ID,
934+
'commit',
935+
])
936+
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
937+
result = conn.save_entity(DATASET_ID, key_pb,
938+
{'foo': u'Foo', 'bar': []})
939+
self.assertEqual(result, True)
940+
cw = http._called_with
941+
self._verifyProtobufCall(cw, URI, conn)
942+
rq_class = datastore_pb.CommitRequest
943+
request = rq_class()
944+
request.ParseFromString(cw['body'])
945+
self.assertEqual(request.transaction, '')
946+
mutation = request.mutation
947+
self.assertEqual(len(mutation.insert_auto_id), 0)
948+
upserts = list(mutation.upsert)
949+
self.assertEqual(len(upserts), 1)
950+
upsert = upserts[0]
951+
self.assertEqual(upsert.key, key_pb)
952+
props = list(upsert.property)
953+
self.assertEqual(len(props), 1)
954+
self.assertNotEqual(props[0].name, 'bar')
955+
920956
def test_save_entity_wo_transaction_w_upsert(self):
921957
from gcloud.datastore.connection import datastore_pb
922958
from gcloud.datastore.key import Key

0 commit comments

Comments
 (0)