From fe1450ba9b7196a841fcb236334da016ce3b4796 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 24 Sep 2014 18:15:48 -0400 Subject: [PATCH 1/3] Propose a contract for Key.parent. --- gcloud/datastore/key.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index 3356acf47562..834efe3dd51d 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -33,6 +33,9 @@ def _clone(self): We make a shallow copy of the :class:`gcloud.datastore.dataset.Dataset` because it holds a reference an authenticated connection, which we don't want to lose. + + :rtype: :class:`gcloud.datastore.key.Key` + :returns: a new `Key` instance """ clone = copy.deepcopy(self) clone._dataset = self._dataset # Make a shallow copy of the Dataset. @@ -259,7 +262,12 @@ def id_or_name(self): return self.id() or self.name() def parent(self):#pragma NO COVER - # See https://github.com/GoogleCloudPlatform/gcloud-python/issues/135 + """Getter: return a new key for the next highest elemen in path. + + :rtype: :class:`gcloud.datastore.key.Key` + :returns: a new `Key` instance, whose path consists of all but the last + element of self's path. If self has only one path element, return None. + """ raise NotImplementedError def __repr__(self): #pragma NO COVER From c8f6aa47c3663f6bdad19d03472ee2543fb2de8a Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 24 Sep 2014 18:22:49 -0400 Subject: [PATCH 2/3] Fix #135: enforce contract for Key.parent(). --- gcloud/datastore/key.py | 4 +++- gcloud/datastore/test_key.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index 834efe3dd51d..ca1ad5643a28 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -268,7 +268,9 @@ def parent(self):#pragma NO COVER :returns: a new `Key` instance, whose path consists of all but the last element of self's path. If self has only one path element, return None. """ - raise NotImplementedError + if len(self._path) <= 1: + return None + return self.path(self.path()[:-1]) def __repr__(self): #pragma NO COVER return '' % self.path() diff --git a/gcloud/datastore/test_key.py b/gcloud/datastore/test_key.py index 5da0f8af19cf..d08ba93352a9 100644 --- a/gcloud/datastore/test_key.py +++ b/gcloud/datastore/test_key.py @@ -339,6 +339,14 @@ def test_id_or_name_w_name_only(self): key = self._makeOne(path=_PATH) self.assertEqual(key.id_or_name(), _NAME) - def _ugh(self): - protokey = key.to_protobuf() - self.assertEqual(protokey.partition_id.dataset_id, _DATASET) + def test_parent_default(self): + key = self._makeOne() + self.assertEqual(key.parent(), None) + + def test_parent_explicit_top_level(self): + key = self._getTargetClass().from_path('abc', 'def') + self.assertEqual(key.parent(), None) + + def test_parent_explicit_nested(self): + key = self._getTargetClass().from_path('abc', 'def', 'ghi', 123) + self.assertEqual(key.parent().path(), [{'kind': 'abc', 'name': 'def'}]) From 37dd3280137231bd564d217d234118e046580a1e Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 25 Sep 2014 13:50:16 -0400 Subject: [PATCH 3/3] Incorporate feedback from @dhermes. --- gcloud/datastore/key.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index 52980b69e077..e341cef196ec 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -262,7 +262,7 @@ def id_or_name(self): return self.id() or self.name() def parent(self):#pragma NO COVER - """Getter: return a new key for the next highest elemen in path. + """Getter: return a new key for the next highest element in path. :rtype: :class:`gcloud.datastore.key.Key` :returns: a new `Key` instance, whose path consists of all but the last