Skip to content

Commit

Permalink
Fix #135: enforce contract for Key.parent().
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Sep 24, 2014
1 parent fe1450b commit c8f6aa4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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'}])

0 comments on commit c8f6aa4

Please sign in to comment.