Skip to content

Commit

Permalink
Merge pull request #185 from tseaver/134-key-from_path-contract
Browse files Browse the repository at this point in the history
Fix #134:  document / enforce Key.from_path contract
  • Loading branch information
silvolu committed Sep 30, 2014
2 parents 77e5f0b + 19c6382 commit 1bb281c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 3 additions & 0 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def from_path(cls, *args, **kwargs):
:rtype: :class:`gcloud.datastore.key.Key`
:returns: a new `Key` instance
"""
if len(args) % 2:
raise ValueError('Must pass an even number of args.')

path = []
items = iter(args)

Expand Down
11 changes: 5 additions & 6 deletions gcloud/datastore/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ def test_from_path_empty(self):
self.assertEqual(key.path(), [{'kind': ''}])

def test_from_path_single_element(self):
# See https://github.com/GoogleCloudPlatform/gcloud-python/issues/134
key = self._getTargetClass().from_path('abc')
self.assertEqual(key.dataset(), None)
self.assertEqual(key.namespace(), None)
self.assertEqual(key.kind(), '') # XXX s.b. 'abc'?
self.assertEqual(key.path(), [{'kind': ''}]) # XXX s.b. 'abc'?
self.assertRaises(ValueError, self._getTargetClass().from_path, 'abc')

def test_from_path_three_elements(self):
self.assertRaises(ValueError, self._getTargetClass().from_path,
'abc', 'def', 'ghi')

def test_from_path_two_elements_second_string(self):
key = self._getTargetClass().from_path('abc', 'def')
Expand Down

0 comments on commit 1bb281c

Please sign in to comment.