Skip to content

Commit

Permalink
Add tests for logic in Entity.__repr__.
Browse files Browse the repository at this point in the history
Fixes #174.
  • Loading branch information
tseaver committed Oct 17, 2014
1 parent 7c55a04 commit 64c711e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 1 addition & 2 deletions gcloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class NoKey(RuntimeError):
pass
"""Exception raised by Entity methods which require a key."""


class Entity(dict): # pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -238,7 +238,6 @@ def delete(self):
dataset_id=dataset.id(), key_pb=key.to_protobuf())

def __repr__(self):
# An entity should have a key all the time (even if it's partial).
if self._key:
return '<Entity%s %s>' % (self._key.path(),
super(Entity, self).__repr__())
Expand Down
22 changes: 18 additions & 4 deletions gcloud/datastore/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def test_from_protobuf(self):
def test_reload_no_key(self):
from gcloud.datastore.entity import NoKey

dataset = _Dataset()
entity = self._makeOne(None, None)
entity['foo'] = 'Foo'
self.assertRaises(NoKey, entity.reload)
Expand Down Expand Up @@ -116,7 +115,6 @@ def test_reload_hit(self):
def test_save_no_key(self):
from gcloud.datastore.entity import NoKey

dataset = _Dataset()
entity = self._makeOne(None, None)
entity['foo'] = 'Foo'
self.assertRaises(NoKey, entity.save)
Expand Down Expand Up @@ -186,7 +184,6 @@ def test_save_w_returned_key(self):
def test_delete_no_key(self):
from gcloud.datastore.entity import NoKey

dataset = _Dataset()
entity = self._makeOne(None, None)
entity['foo'] = 'Foo'
self.assertRaises(NoKey, entity.delete)
Expand All @@ -201,8 +198,23 @@ def test_delete(self):
self.assertTrue(entity.delete() is None)
self.assertEqual(connection._deleted, (_DATASET_ID, 'KEY'))

def test___repr___no_key_empty(self):
entity = self._makeOne(None, None)
self.assertEqual(repr(entity), '<Entity {}>')

def test___repr___w_key_non_empty(self):
connection = _Connection()
dataset = _Dataset(connection)
key = _Key(dataset)
key.path('/bar/baz')
entity = self._makeOne()
entity.key(key)
entity['foo'] = 'Foo'
self.assertEqual(repr(entity), "<Entity/bar/baz {'foo': 'Foo'}>")


class _Key(object):
_MARKER = object()
_key = 'KEY'
_partial = False
_path = None
Expand All @@ -219,7 +231,9 @@ def to_protobuf(self):
def is_partial(self):
return self._partial

def path(self, path):
def path(self, path=_MARKER):
if path is self._MARKER:
return self._path
self._path = path


Expand Down

0 comments on commit 64c711e

Please sign in to comment.