Skip to content

Commit ca0e3f9

Browse files
author
vperez
committed
feat: add is_app_initialized function (#701)
1 parent 32b900b commit ca0e3f9

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

firebase_admin/__init__.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME):
6565
credential = credentials.ApplicationDefault()
6666
app = App(name, credential, options)
6767
with _apps_lock:
68-
if app.name not in _apps:
68+
if not is_app_initialized(app.name):
6969
_apps[app.name] = app
7070
return app
7171

@@ -85,6 +85,17 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME):
8585
'you call initialize_app().').format(name))
8686

8787

88+
def is_app_initialized(name=_DEFAULT_APP_NAME):
89+
"""Returns True if the App instance for the specified name is initialized.
90+
Args:
91+
name: Name of the App instance to retrieve (optional).
92+
Returns:
93+
bool: True if the App instance is initialized, False otherwise.
94+
"""
95+
with _apps_lock:
96+
return name in _apps
97+
98+
8899
def delete_app(app):
89100
"""Gracefully deletes an App instance.
90101
@@ -129,9 +140,8 @@ def get_app(name=_DEFAULT_APP_NAME):
129140
if not isinstance(name, str):
130141
raise ValueError('Illegal app name argument type: "{}". App name '
131142
'must be a string.'.format(type(name)))
132-
with _apps_lock:
133-
if name in _apps:
134-
return _apps[name]
143+
if is_app_initialized(name):
144+
return _apps[name]
135145

136146
if name == _DEFAULT_APP_NAME:
137147
raise ValueError(

tests/test_app.py

+18
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ def test_app_init_with_default_config(self, env_test_case):
285285
app = firebase_admin.initialize_app(CREDENTIAL, options=env_test_case.init_options)
286286
assert app.options._options == env_test_case.want_options
287287

288+
def test_default_app_is_initialized(self, app_credential):
289+
firebase_admin.initialize_app(app_credential)
290+
assert firebase_admin.is_app_initialized()
291+
292+
def test_no_app_is_initialized(self):
293+
assert firebase_admin.is_app_initialized() is False
294+
295+
def test_correct_app_is_initialized(self):
296+
app_name = 'myApp'
297+
firebase_admin.initialize_app(CREDENTIAL, name=app_name)
298+
assert firebase_admin.is_app_initialized(app_name)
299+
300+
def test_correct_app_is_not_initialized(self):
301+
app_name = 'myApp'
302+
other_app_name = 'otherApp'
303+
firebase_admin.initialize_app(CREDENTIAL, name=app_name)
304+
assert firebase_admin.is_app_initialized(other_app_name) is False
305+
288306
def test_project_id_from_options(self, app_credential):
289307
app = firebase_admin.initialize_app(
290308
app_credential, options={'projectId': 'test-project'}, name='myApp')

0 commit comments

Comments
 (0)