Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #135: Document / enforce gcloud.datastore.key.Key.parent() contract #186

Merged
merged 5 commits into from
Sep 30, 2014
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

:returns: a new `Key` instance
"""
clone = copy.deepcopy(self)
clone._dataset = self._dataset # Make a shallow copy of the Dataset.
Expand Down Expand Up @@ -259,8 +262,15 @@ 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
raise NotImplementedError
"""Getter: return a new key for the next highest elemen in path.

This comment was marked as spam.


: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.
"""
if len(self._path) <= 1:
return None
return self.path(self.path()[:-1])

def __repr__(self): #pragma NO COVER
return '<Key%s>' % self.path()
14 changes: 11 additions & 3 deletions gcloud/datastore/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}])