Skip to content

Commit

Permalink
feat: find emulator project id from environment variable (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche authored Feb 16, 2024
1 parent 6cb3147 commit afd16e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion google/cloud/firestore_v1/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ def __init__(
if credentials is None:
credentials = AnonymousCredentials()
if project is None:
project = _DEFAULT_EMULATOR_PROJECT
# extract project from env var, or use system default
project = (
os.getenv("GOOGLE_CLOUD_PROJECT")
or os.getenv("GCLOUD_PROJECT")
or _DEFAULT_EMULATOR_PROJECT
)

super(BaseClient, self).__init__(
project=project,
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import mock
import pytest

from google.cloud.firestore_v1.base_client import DEFAULT_DATABASE
from google.cloud.firestore_v1.base_client import (
DEFAULT_DATABASE,
_DEFAULT_EMULATOR_PROJECT,
)

PROJECT = "my-prahjekt"

Expand Down Expand Up @@ -100,6 +103,32 @@ def test_client_constructor_explicit(database, expected):
assert client._client_options is client_options


@pytest.mark.parametrize(
"extra_env,project_expected",
[
({}, _DEFAULT_EMULATOR_PROJECT),
({"GCLOUD_PROJECT": "gcloud"}, "gcloud"),
({"GOOGLE_CLOUD_PROJECT": "google"}, "google"),
({"GCLOUD_PROJECT": "gcloud", "GOOGLE_CLOUD_PROJECT": "google"}, "google"),
],
)
def test_client_constructor_emulator(extra_env, project_expected):
"""
Ensure client can be configured with FIRESOTRE_EMULATOR_HOST environment variable
If project is not set, should be detected from GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT
"""
expected_host = "localhost:8080"
environment = {"FIRESTORE_EMULATOR_HOST": expected_host}
if extra_env:
environment.update(extra_env)

with mock.patch("os.environ", environment):
client = _make_client()
assert client._emulator_host == expected_host
assert client.project == project_expected


@pytest.mark.parametrize("database", [None, DEFAULT_DATABASE, "somedb"])
def test_client__firestore_api_property(database):
credentials = _make_credentials()
Expand Down

0 comments on commit afd16e1

Please sign in to comment.