Skip to content

Commit

Permalink
feat: add 'Client.entity' helper (#239)
Browse files Browse the repository at this point in the history
Proxy for 'google.cloud.datastore.entity.Entity'.
  • Loading branch information
tseaver committed Oct 18, 2021
1 parent 6398bbc commit 49d48f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ def key(self, *path_args, **kwargs):
kwargs["namespace"] = self.namespace
return Key(*path_args, **kwargs)

def entity(self, key=None, exclude_from_indexes=()):
"""Proxy to :class:`google.cloud.datastore.entity.Entity`."""
return Entity(key=key, exclude_from_indexes=exclude_from_indexes)

def batch(self):
"""Proxy to :class:`google.cloud.datastore.batch.Batch`."""
return Batch(self)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,30 @@ def test_key_w_namespace_collision(self):
kind, id_, project=self.PROJECT, namespace=namespace2
)

def test_entity_w_defaults(self):
creds = _make_credentials()
client = self._make_one(credentials=creds)

patch = mock.patch("google.cloud.datastore.client.Entity", spec=["__call__"])
with patch as mock_klass:
entity = client.entity()
self.assertIs(entity, mock_klass.return_value)
mock_klass.assert_called_once_with(key=None, exclude_from_indexes=())

def test_entity_w_explicit(self):
key = mock.Mock(spec=[])
exclude_from_indexes = ["foo", "bar"]
creds = _make_credentials()
client = self._make_one(credentials=creds)

patch = mock.patch("google.cloud.datastore.client.Entity", spec=["__call__"])
with patch as mock_klass:
entity = client.entity(key, exclude_from_indexes)
self.assertIs(entity, mock_klass.return_value)
mock_klass.assert_called_once_with(
key=key, exclude_from_indexes=exclude_from_indexes
)

def test_batch(self):
creds = _make_credentials()
client = self._make_one(credentials=creds)
Expand Down

0 comments on commit 49d48f1

Please sign in to comment.