Skip to content
Closed
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
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def entity_from_protobuf(pb):
# Check if ``value_pb`` was excluded from index. Lists need to be
# special-cased and we require all ``exclude_from_indexes`` values
# in a list agree.
if is_list:
if is_list and len(value) > 0:
exclude_values = set(value_pb.exclude_from_indexes
for value_pb in value_pb.array_value.values)
if len(exclude_values) != 1:
Expand Down
15 changes: 15 additions & 0 deletions datastore/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ def test_query_distinct_on(self):
self.assertEqual(entities[0]['name'], 'Catelyn')
self.assertEqual(entities[1]['name'], 'Arya')

def test_empty_array_value(self):
from google.cloud import datastore
from google.cloud.datastore_v1.proto import datastore_pb2
from google.cloud.datastore_v1.proto import entity_pb2
client = self.CLIENT
key = client.key('Foo', 'Bar')
entity = datastore.Entity(key=key)
with client.batch() as batch:
batch.put(entity)
mutations = batch.mutations
mutation = mutations[0]
value_pb = mutation.upsert.properties.get_or_create('name-name')
value_pb.array_value.CopyFrom(entity_pb2.ArrayValue(values=[]))
client.get(key)


class TestDatastoreTransaction(TestDatastore):

Expand Down
18 changes: 18 additions & 0 deletions datastore/tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ def test_mismatched_value_indexed(self):
with self.assertRaises(ValueError):
self._call_fut(entity_pb)

def test_index_mismatch_ignores_empty_list(self):
from google.cloud.datastore_v1.proto import entity_pb2
from google.cloud.datastore.helpers import _new_value_pb

_PROJECT = 'PROJECT'
_KIND = 'KIND'
_ID = 1234
entity_pb = entity_pb2.Entity()
entity_pb.key.partition_id.project_id = _PROJECT
entity_pb.key.path.add(kind=_KIND, id=_ID)

array_val_pb = _new_value_pb(entity_pb, 'baz')
array_val_pb.array_value.CopyFrom(entity_pb2.ArrayValue(values=[]))

entity = self._call_fut(entity_pb)
entity_dict = dict(entity)
self.assertEqual(entity_dict['baz'], [])

def test_entity_no_key(self):
from google.cloud.datastore_v1.proto import entity_pb2

Expand Down