Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'Client.entity' helper #239

Merged
merged 2 commits into from Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions google/cloud/datastore/client.py
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
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