diff --git a/gcloud/datastore/entity.py b/gcloud/datastore/entity.py index 219909f8b166..59db2e364120 100644 --- a/gcloud/datastore/entity.py +++ b/gcloud/datastore/entity.py @@ -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 @@ -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 '' % (self._key.path(), super(Entity, self).__repr__()) diff --git a/gcloud/datastore/test_entity.py b/gcloud/datastore/test_entity.py index cd051d47f00b..a3171c2bca36 100644 --- a/gcloud/datastore/test_entity.py +++ b/gcloud/datastore/test_entity.py @@ -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) @@ -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) @@ -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) @@ -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), '') + + 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), "") + class _Key(object): + _MARKER = object() _key = 'KEY' _partial = False _path = None @@ -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