From f885008bdcc2a5fc6387262a1c3d955364a0a029 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 22 Feb 2018 15:15:07 -0500 Subject: [PATCH 1/2] Add unit test which actually exercises #3152. --- datastore/tests/unit/test_helpers.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/datastore/tests/unit/test_helpers.py b/datastore/tests/unit/test_helpers.py index 3624665a2a05..91a693455b7d 100644 --- a/datastore/tests/unit/test_helpers.py +++ b/datastore/tests/unit/test_helpers.py @@ -191,6 +191,28 @@ def test_nested_entity_no_key(self): self.assertEqual(len(inside_entity), 1) self.assertEqual(inside_entity[INSIDE_NAME], INSIDE_VALUE) + def test_index_mismatch_ignores_empty_list(self): + from google.cloud.datastore_v1.proto import entity_pb2 + + _PROJECT = 'PROJECT' + _KIND = 'KIND' + _ID = 1234 + + array_val_pb = entity_pb2.Value( + array_value=entity_pb2.ArrayValue(values=[])) + + entity_pb = entity_pb2.Entity( + properties={ + 'baz': array_val_pb, + }, + ) + entity_pb.key.partition_id.project_id = _PROJECT + entity_pb.key.path.add(kind=_KIND, id=_ID) + + entity = self._call_fut(entity_pb) + entity_dict = dict(entity) + self.assertEqual(entity_dict['baz'], []) + class Test_entity_to_protobuf(unittest.TestCase): From 3585264ff3a23ecd6005bdca860872972a9ca74a Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 22 Feb 2018 15:21:59 -0500 Subject: [PATCH 2/2] Don't check 'exclude_from_indexes' for empty lists. Closes #3152. --- datastore/google/cloud/datastore/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastore/google/cloud/datastore/helpers.py b/datastore/google/cloud/datastore/helpers.py index f3838668fe3d..964eb2a37204 100644 --- a/datastore/google/cloud/datastore/helpers.py +++ b/datastore/google/cloud/datastore/helpers.py @@ -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: