I tried to find out how to use firestore local emulator for python and for testing purpose. But I can not find out how-to document.
Could somebody help me?
I tried to find out how to use firestore local emulator for python and for testing purpose. But I can not find out how-to document.
Could somebody help me?
Welcome to SO :)
The primary purpose of the Cloud Firestore Emulator (at the moment) seems to be to test security rules, as documented here. This section states: "The only SDK that currently supports the emulator is the Node.js SDK."
Confusingly there are also these Ruby docs for the Google Cloud Client Libraries. The same thing does not seem to be available in Python yet.
Here are instructions for running the emulator as part of the Google Cloud SDK.
Consider using Cloud Firestore in Datastore mode, which has better tooling (it's probably just had more time to mature). You can find instructions for running its emulator on the Running the Datastore mode Emulator page.
Use the Choosing between Native Mode and Datastore Mode page to decide which direction you want to take. If you feel you need the additional 'Native mode' features, it'll probably be easiest to connect straight to a real Firestore instance in the cloud.
Using the firebase_admin
python module, follow the standard setup documented in the Cloud Firestore Docs
This will involve calling initialize_app
with a credentials
context and then creating a traditional Firestore Client with firestore.client()
For example:
from firebase_admin import credentials, firestore, initialize_app
firebase_credentials_file_path = ...
cred = credentials.Certificate(firebase_credentials_file_path)
initialize_app(cred)
db = firestore.client()
Next, you will need to install and run the Firestore Emulator, which will host the a local Firestore instance over localhost:8080
.
npx firebase setup:emulators:firestore
npx firebase --token $FIREBASE_TOKEN emulators:start --only firestore --project $PROJECT_KEY
Finally, inject a redirect in the already instantiated firestore.client
instance to interact with the local emulator host/port using an insecure GRPC channel:
import grpc
from google.cloud.firestore_v1.gapic import firestore_client
from google.cloud.firestore_v1.gapic.transports import firestore_grpc_transport
channel = grpc.insecure_channel("localhost:8080")
transport = firestore_grpc_transport.FirestoreGrpcTransport(channel=channel)
db._firestore_api_internal = firestore_client.FirestoreClient(transport=transport)
Now, your db
object will interact with the local emulator without any problems.
Acknowledgements to John Carter for figuring this out on the gcloud internal api
grpc
just doesn't work in multiprocessing environments. See github.com/googleapis/google-cloud-python/issues/… –
Allochthonous grpc
mutliprocessing limitations, look into extending the multiprocessing.managers.BaseManager
as a facade to a singleton grpc
instance that is process safe. –
Allochthonous firestore_client
and firestore_grpc_transport
are now in google.cloud.firestore_v1.client
(using firebase-admin==5.2.0
and google-cloud-firestore==2.6.0
). –
Ossicle As of now, one can connect any SDK of firebase_admin
to firebase emulator by simply setting these two environment variables. I have tested this personally on Python SDK and it works like a charm.
export FIRESTORE_EMULATOR_HOST="localhost:8080"
export GCLOUD_PROJECT="any-valid-name"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_my_service_account.json'
–
Heteropterous Welcome to SO :)
The primary purpose of the Cloud Firestore Emulator (at the moment) seems to be to test security rules, as documented here. This section states: "The only SDK that currently supports the emulator is the Node.js SDK."
Confusingly there are also these Ruby docs for the Google Cloud Client Libraries. The same thing does not seem to be available in Python yet.
Here are instructions for running the emulator as part of the Google Cloud SDK.
Consider using Cloud Firestore in Datastore mode, which has better tooling (it's probably just had more time to mature). You can find instructions for running its emulator on the Running the Datastore mode Emulator page.
Use the Choosing between Native Mode and Datastore Mode page to decide which direction you want to take. If you feel you need the additional 'Native mode' features, it'll probably be easiest to connect straight to a real Firestore instance in the cloud.
I was running this in a pytest fixture, however you should be able to use it anywhere:
from firebase_admin import credentials, delete_app, firestore, initialize_app
from google.auth.credentials import AnonymousCredentials
class _OfflineGCPCredentials(credentials.Base):
def get_credential(self):
return AnonymousCredentials()
app = initialize_app(credential=_OfflineGCPCredentials(), options={'projectId': 'demo-projectname'})
# ... run your code that includes
client = firestore.client()
delete_app(app)
© 2022 - 2024 — McMap. All rights reserved.