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 1 commit
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
Prev Previous commit
Next Next commit
Fix #135: enforce contract for Key.parent().
  • Loading branch information
tseaver committed Sep 24, 2014
commit c8f6aa47c3663f6bdad19d03472ee2543fb2de8a
4 changes: 3 additions & 1 deletion gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<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'}])