From 375382154060f2f127edff64fe63b4cc07115468 Mon Sep 17 00:00:00 2001 From: lucemia Date: Mon, 27 Oct 2014 12:17:03 +0800 Subject: [PATCH 1/3] #295 let entity save accept null value for attribute --- gcloud/datastore/helpers.py | 3 +++ gcloud/datastore/test_helpers.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index c8927bf2beea..f45c80af7ff0 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -222,6 +222,9 @@ def _set_protobuf_value(value_pb, val): :class:`gcloud.datastore.entity.Entity`, :param val: The value to be assigned. """ + if val is None: + return + attr, val = _get_protobuf_attribute_and_value(val) if attr == 'key_value': value_pb.key_value.CopyFrom(val) diff --git a/gcloud/datastore/test_helpers.py b/gcloud/datastore/test_helpers.py index c2c8aae45933..39ce8a55c4a8 100644 --- a/gcloud/datastore/test_helpers.py +++ b/gcloud/datastore/test_helpers.py @@ -361,6 +361,20 @@ def test_key(self): value = pb.key_value self.assertEqual(value, key.to_protobuf()) + def test_none(self): + pb = self._makePB() + self._callFUT(pb, None) + self.assertEqual(pb.HasField('boolean_value'), False) + self.assertEqual(pb.HasField('integer_value'), False) + self.assertEqual(pb.HasField('double_value'), False) + self.assertEqual(pb.HasField('timestamp_microseconds_value'), False) + self.assertEqual(pb.HasField('key_value'), False) + self.assertEqual(pb.HasField('blob_key_value'), False) + self.assertEqual(pb.HasField('string_value'), False) + self.assertEqual(pb.HasField('blob_value'), False) + self.assertEqual(pb.HasField('entity_value'), False) + self.assertEqual(len(pb.list_value), 0) + def test_bool(self): pb = self._makePB() self._callFUT(pb, False) From 014b6584641e601fd71c8a81e78d04aed233bcda Mon Sep 17 00:00:00 2001 From: lucemia Date: Mon, 27 Oct 2014 12:44:25 +0800 Subject: [PATCH 2/3] fix an issue that protoc buff will raise exception if a _pb object create but never assign value --- gcloud/datastore/helpers.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index f45c80af7ff0..800766a62a65 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -223,6 +223,15 @@ def _set_protobuf_value(value_pb, val): :param val: The value to be assigned. """ if val is None: + value_pb.ClearField('boolean_value') + value_pb.ClearField('integer_value') + value_pb.ClearField('double_value') + value_pb.ClearField('timestamp_microseconds_value') + value_pb.ClearField('key_value') + value_pb.ClearField('blob_key_value') + value_pb.ClearField('string_value') + value_pb.ClearField('blob_value') + value_pb.ClearField('entity_value') return attr, val = _get_protobuf_attribute_and_value(val) From a208132dd67a41a6f9fd67effdb3659f3ddb82ec Mon Sep 17 00:00:00 2001 From: lucemia Date: Tue, 28 Oct 2014 22:37:20 +0800 Subject: [PATCH 3/3] fix the helper function and test case while checking null value --- gcloud/datastore/helpers.py | 10 +--------- gcloud/datastore/test_helpers.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index 800766a62a65..91ea9af3fba0 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -223,15 +223,7 @@ def _set_protobuf_value(value_pb, val): :param val: The value to be assigned. """ if val is None: - value_pb.ClearField('boolean_value') - value_pb.ClearField('integer_value') - value_pb.ClearField('double_value') - value_pb.ClearField('timestamp_microseconds_value') - value_pb.ClearField('key_value') - value_pb.ClearField('blob_key_value') - value_pb.ClearField('string_value') - value_pb.ClearField('blob_value') - value_pb.ClearField('entity_value') + value_pb.Clear() return attr, val = _get_protobuf_attribute_and_value(val) diff --git a/gcloud/datastore/test_helpers.py b/gcloud/datastore/test_helpers.py index 39ce8a55c4a8..43879fc79aa9 100644 --- a/gcloud/datastore/test_helpers.py +++ b/gcloud/datastore/test_helpers.py @@ -362,18 +362,23 @@ def test_key(self): self.assertEqual(value, key.to_protobuf()) def test_none(self): + from gcloud.datastore.entity import Entity + + entity = Entity() pb = self._makePB() + + self._callFUT(pb, False) + self._callFUT(pb, 3.1415926) + self._callFUT(pb, 42) + self._callFUT(pb, (1 << 63) - 1) + self._callFUT(pb, 'str') + self._callFUT(pb, b'str') + self._callFUT(pb, u'str') + self._callFUT(pb, entity) + self._callFUT(pb, [u'a', 0, 3.14]) + self._callFUT(pb, None) - self.assertEqual(pb.HasField('boolean_value'), False) - self.assertEqual(pb.HasField('integer_value'), False) - self.assertEqual(pb.HasField('double_value'), False) - self.assertEqual(pb.HasField('timestamp_microseconds_value'), False) - self.assertEqual(pb.HasField('key_value'), False) - self.assertEqual(pb.HasField('blob_key_value'), False) - self.assertEqual(pb.HasField('string_value'), False) - self.assertEqual(pb.HasField('blob_value'), False) - self.assertEqual(pb.HasField('entity_value'), False) - self.assertEqual(len(pb.list_value), 0) + self.assertEqual(len(pb.ListFields()), 0) def test_bool(self): pb = self._makePB()