I'm sure this has been asked before, but the only place I can find is
this Stack Overflow question, and I'm pretty sure that is out of date. I know I can get it to work much more simply by just setting FIRESTORE_EMULATOR_HOST=localhost:8080 and then initializing with a credentials file, like:
import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate('firebase-adminsdk.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
(I'm happy to add a new answer there when I understand the issue better).
What I don't like about the above is that I need an admin credentials file to make it work. I wouldn't want to check such a thing into GitHub, for example, so anyone who checks out my code and wants to run tests needs to get the credentials file some other way, and I have to set up my CI environment to have the credentials somehow. These aren't unsolvable problems, but I don't think it should be necessary, because I've used the Firestore emulator from Node.js before, and it didn't need secret credentials. A user can just check out the code, install the dependencies, and run the tests.
I think what I should be able to do is just set FIRESTORE_EMULATOR_HOST=localhost:8080 and then initialize Firebase in a fairly normal way, i.e. something like
import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
from firebase_admin import firestore
# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(
cred,
{
'projectId': 'my_project_id',
},
)
db = firestore.client()
Or maybe even leave out the initialize_app call if I don't need any Firebase functionality other than Firestore, i.e.,
from firebase_admin import firestore
db = firestore.client() # Or should it be firestore.Client() ?
When I try any of the above, though, I get errors saying it can't determine the default credentials (in the first case), or there is no default Firebase instance (in the second).
Is this posslble? It seems like it should be.