From efa90d87e4ba891c61bb4657232ed54f500121c8 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Thu, 16 Mar 2017 09:54:46 -0700 Subject: [PATCH 1/3] Non-functional datastore fix for #3152. --- datastore/google/cloud/datastore/helpers.py | 2 +- datastore/tests/unit/test_helpers.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/datastore/google/cloud/datastore/helpers.py b/datastore/google/cloud/datastore/helpers.py index ee4537317030..91b08452812f 100644 --- a/datastore/google/cloud/datastore/helpers.py +++ b/datastore/google/cloud/datastore/helpers.py @@ -133,7 +133,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: diff --git a/datastore/tests/unit/test_helpers.py b/datastore/tests/unit/test_helpers.py index 18ff98e64781..8529f214640c 100644 --- a/datastore/tests/unit/test_helpers.py +++ b/datastore/tests/unit/test_helpers.py @@ -135,6 +135,27 @@ 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.proto.datastore.v1 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_pb = array_val_pb.array_value.values + + # unindexed_value_pb1 = array_pb.add() + # unindexed_value_pb1.integer_value = 10 + + entity = self._call_fut(entity_pb) + entity_dict = dict(entity) + self.assertIsInstance(entity_dict['baz'], list) + def test_entity_no_key(self): from google.cloud.proto.datastore.v1 import entity_pb2 From 18478f83306554adf963cb227594e6a7899ee5c0 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Thu, 16 Mar 2017 09:54:46 -0700 Subject: [PATCH 2/3] Non-functional datastore fix for #3152. --- datastore/google/cloud/datastore/helpers.py | 2 +- datastore/tests/unit/test_helpers.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/datastore/google/cloud/datastore/helpers.py b/datastore/google/cloud/datastore/helpers.py index ee4537317030..91b08452812f 100644 --- a/datastore/google/cloud/datastore/helpers.py +++ b/datastore/google/cloud/datastore/helpers.py @@ -133,7 +133,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: diff --git a/datastore/tests/unit/test_helpers.py b/datastore/tests/unit/test_helpers.py index 18ff98e64781..8529f214640c 100644 --- a/datastore/tests/unit/test_helpers.py +++ b/datastore/tests/unit/test_helpers.py @@ -135,6 +135,27 @@ 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.proto.datastore.v1 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_pb = array_val_pb.array_value.values + + # unindexed_value_pb1 = array_pb.add() + # unindexed_value_pb1.integer_value = 10 + + entity = self._call_fut(entity_pb) + entity_dict = dict(entity) + self.assertIsInstance(entity_dict['baz'], list) + def test_entity_no_key(self): from google.cloud.proto.datastore.v1 import entity_pb2 From ef4ed3ab8902015054ec9bd3729affbf3501968d Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Tue, 8 Aug 2017 14:29:37 -0700 Subject: [PATCH 3/3] Fix the unit test. --- datastore/tests/unit/test_helpers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datastore/tests/unit/test_helpers.py b/datastore/tests/unit/test_helpers.py index 8529f214640c..95340c525d48 100644 --- a/datastore/tests/unit/test_helpers.py +++ b/datastore/tests/unit/test_helpers.py @@ -149,12 +149,12 @@ def test_index_mismatch_ignores_empty_list(self): array_val_pb = _new_value_pb(entity_pb, 'baz') array_pb = array_val_pb.array_value.values - # unindexed_value_pb1 = array_pb.add() - # unindexed_value_pb1.integer_value = 10 + unindexed_value_pb1 = array_pb.add() + unindexed_value_pb1.integer_value = 10 entity = self._call_fut(entity_pb) entity_dict = dict(entity) - self.assertIsInstance(entity_dict['baz'], list) + self.assertEqual(entity_dict['baz'], [10]) def test_entity_no_key(self): from google.cloud.proto.datastore.v1 import entity_pb2