From e605c8d62f2707f00368790e25589b48dec6e8bf Mon Sep 17 00:00:00 2001 From: Mike Koss Date: Fri, 11 Nov 2016 12:13:23 -0800 Subject: [PATCH] Firestore Client derived from google.cloud.client.Client (#104) --- firestore/google/cloud/firestore/client.py | 30 ++++++++++++++++-- firestore/tox.ini | 1 + firestore/unit_tests/test_client.py | 37 ++++++++++++++++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/firestore/google/cloud/firestore/client.py b/firestore/google/cloud/firestore/client.py index baf286d981e9..7e73e47af0ee 100644 --- a/firestore/google/cloud/firestore/client.py +++ b/firestore/google/cloud/firestore/client.py @@ -14,13 +14,37 @@ """Firestore database SDK Client.""" +from google.cloud.client import Client as _BaseClient +from google.cloud.client import _ClientProjectMixin +from google.cloud.credentials import get_credentials -class Client(object): + +ALL_SCOPES = ('https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/datastore') + + +class Client(_BaseClient, _ClientProjectMixin): """Firestore Client. :type project: str :param project: (optional) Project containing the Firestore database. + + :type credentials: + :class:`OAuth2Credentials ` or + :data:`NoneType ` + :param credentials: (Optional) The OAuth2 Credentials to use for this + client. If not provided, defaults to the Google + Application Default Credentials. """ - def __init__(self, project=None): - raise NotImplementedError() + def __init__(self, project=None, credentials=None): + if credentials is None: + credentials = get_credentials() + + try: + credentials = credentials.create_scoped(ALL_SCOPES) + except AttributeError: + pass + + _ClientProjectMixin.__init__(self, project=project) + _BaseClient.__init__(self, credentials=credentials) diff --git a/firestore/tox.ini b/firestore/tox.ini index b4b4353354f7..5bd3062f6181 100644 --- a/firestore/tox.ini +++ b/firestore/tox.ini @@ -8,6 +8,7 @@ deps = {toxinidir}/../_vendor/grpc-google-firestore-v1alpha1-0.10.0.tar.gz {toxinidir}/../_vendor/gax-google-firestore-v1alpha1-0.10.0.tar.gz pytest + mock covercmd = py.test --quiet \ --cov=google.cloud.firestore \ diff --git a/firestore/unit_tests/test_client.py b/firestore/unit_tests/test_client.py index c05e6e767519..cd452eda5766 100644 --- a/firestore/unit_tests/test_client.py +++ b/firestore/unit_tests/test_client.py @@ -26,5 +26,38 @@ def _make_one(self, *args, **kwargs): return self._get_target_class()(*args, **kwargs) def test_constructor(self): - with self.assertRaises(NotImplementedError): - self._make_one('my-project-id') + client = self._make_one('my-project-id') + self.assertEqual(client.project, 'my-project-id') + + def test_empty_constructor(self): + import mock + + to_mock = 'google.cloud.client._determine_default_project' + project = 'inferred-project' + with mock.patch(to_mock, return_value=project) as determine_proj: + client = self._make_one() + determine_proj.assert_called_once_with(None) + + self.assertEqual(client.project, project) + + def test_credentialed_constructor(self): + from google.cloud.firestore.client import ALL_SCOPES + + creds = MockCredentials() + client = self._make_one('my-project-id', credentials=creds) + self.assertIs(client.connection.credentials, creds) + self.assertEqual(creds.scopes, ALL_SCOPES) + + def test_credentialed_no_scopes_constructor(self): + creds = object() + client = self._make_one('my-project-id', credentials=creds) + self.assertIs(client.connection.credentials, creds) + + +class MockCredentials(object): + def __init__(self): + self.scopes = None + + def create_scoped(self, scopes): + self.scopes = scopes + return self