Skip to content

Commit

Permalink
Merge pull request #186 from tseaver/135-key-parent-contract
Browse files Browse the repository at this point in the history
Fix #135:  Document / enforce gcloud.datastore.key.Key.parent() contract
  • Loading branch information
silvolu committed Sep 30, 2014
2 parents 1bb281c + 8ce18c9 commit 9af7d1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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`
:returns: a new `Key` instance
"""
clone = copy.deepcopy(self)
clone._dataset = self._dataset # Make a shallow copy of the Dataset.
Expand Down Expand Up @@ -265,8 +268,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 element 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.
"""
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 @@ -346,6 +346,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 9af7d1b

Please sign in to comment.