Skip to content

Commit

Permalink
Firestore Client derived from google.cloud.client.Client (googleapis#104
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mckoss authored Nov 11, 2016
1 parent e315f2a commit e605c8d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
30 changes: 27 additions & 3 deletions firestore/google/cloud/firestore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <oauth2client.client.OAuth2Credentials>` or
:data:`NoneType <types.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)
1 change: 1 addition & 0 deletions firestore/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
37 changes: 35 additions & 2 deletions firestore/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e605c8d

Please sign in to comment.