Skip to content

Commit ff818ba

Browse files
authored
Datastore: id from googleapis#3832 pull request with unit test (googleapis#4640)
1 parent 4e50bba commit ff818ba

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

datastore/google/cloud/datastore/entity.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ def kind(self):
183183
if self.key:
184184
return self.key.kind
185185

186+
@property
187+
def id(self):
188+
"""Get the ID of the current entity.
189+
190+
.. note::
191+
192+
This relies entirely on the :class:`google.cloud.datastore.key.Key`
193+
set on the entity. That means that we're not storing the ID
194+
of the entity at all, just the properties and a pointer to a
195+
Key which knows its ID.
196+
"""
197+
if self.key is None:
198+
return None
199+
else:
200+
return self.key.id
201+
186202
def __repr__(self):
187203
if self.key:
188204
return '<Entity%s %s>' % (self.key._flat_path,

datastore/tests/unit/test_entity.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ def test__eq__same_value_different_meanings(self):
192192

193193
self.assertFalse(entity1 == entity2)
194194

195+
def test_id(self):
196+
from google.cloud.datastore.key import Key
197+
198+
key = Key(_KIND, _ID, project=_PROJECT)
199+
entity = self._make_one(key=key)
200+
self.assertEqual(entity.id, _ID)
201+
202+
def test_id_none(self):
203+
from google.cloud.datastore.key import Key
204+
205+
entity = self._make_one(key=None)
206+
self.assertEqual(entity.id, None)
207+
195208
def test___repr___no_key_empty(self):
196209
entity = self._make_one()
197210
self.assertEqual(repr(entity), '<Entity {}>')

0 commit comments

Comments
 (0)