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

Firestore: support emulator in client. #8721

Merged
merged 10 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions firestore/google/cloud/firestore_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ def _firestore_api(self):
# Use a custom channel.
# We need this in order to set appropriate keepalive options.

channel = firestore_grpc_transport.FirestoreGrpcTransport.create_channel(
self._target,
credentials=self._credentials,
options={"grpc.keepalive_time_ms": 30000}.items(),
)

if self._emulator_host is not None:
channel = firestore_grpc_transport.firestore_pb2_grpc.grpc.insecure_channel(
self._emulator_host
)
else:
channel = firestore_grpc_transport.FirestoreGrpcTransport.create_channel(
self._target,
credentials=self._credentials,
options={"grpc.keepalive_time_ms": 30000}.items(),
)

self._transport = firestore_grpc_transport.FirestoreGrpcTransport(
address=self._target, channel=channel
Expand Down
16 changes: 13 additions & 3 deletions firestore/tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_constructor(self):
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, DEFAULT_DATABASE)
self.assertIs(client._client_info, _CLIENT_INFO)
self.assertIsNone(client._emulator_host)

def test_constructor_with_emulator_host(self):
from google.cloud.firestore_v1.client import _FIRESTORE_EMULATOR_HOST
Expand Down Expand Up @@ -75,7 +76,7 @@ def test_constructor_explicit(self):
self.assertIs(client._client_info, client_info)

@mock.patch(
"google.cloud.firestore_v1.gapic.firestore_client." "FirestoreClient",
"google.cloud.firestore_v1.gapic.firestore_client.FirestoreClient",
autospec=True,
return_value=mock.sentinel.firestore_api,
)
Expand All @@ -96,11 +97,17 @@ def test__firestore_api_property(self, mock_client):
self.assertEqual(mock_client.call_count, 1)

@mock.patch(
"google.cloud.firestore_v1.gapic.firestore_client." "FirestoreClient",
"google.cloud.firestore_v1.gapic.firestore_client.FirestoreClient",
autospec=True,
return_value=mock.sentinel.firestore_api,
)
def test__firestore_api_property_with_emulator(self, mock_client):
@mock.patch(
"google.cloud.firestore_v1.gapic.transports.firestore_grpc_transport.firestore_pb2_grpc.grpc.insecure_channel",
autospec=True,
)
def test__firestore_api_property_with_emulator(
self, mock_insecure_channel, mock_client
):
emulator_host = "localhost:8081"
with mock.patch("os.getenv") as getenv:
getenv.return_value = emulator_host
Expand All @@ -111,6 +118,9 @@ def test__firestore_api_property_with_emulator(self, mock_client):
self.assertIs(firestore_api, mock_client.return_value)
self.assertIs(firestore_api, client._firestore_api_internal)

self.assertEqual(mock_insecure_channel.call_count, 1)
self.assertEqual(mock_insecure_channel.call_args.args[0], emulator_host)

# Call again to show that it is cached, but call count is still 1.
self.assertIs(client._firestore_api, mock_client.return_value)
self.assertEqual(mock_client.call_count, 1)
Expand Down