Skip to content

Commit

Permalink
feat: add is_app_initialized function (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
victorperezpiqueras authored and vperez committed Mar 27, 2024
1 parent 32b900b commit b150b03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions firebase_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME):
credential = credentials.ApplicationDefault()
app = App(name, credential, options)
with _apps_lock:
if app.name not in _apps:
if not is_app_initialized(app.name):
_apps[app.name] = app
return app

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


def is_app_initialized(name=_DEFAULT_APP_NAME):
"""Returns True if the App instance for the specified name is initialized.
Args:
name: Name of the App instance to retrieve (optional).
Returns:
bool: True if the App instance is initialized, False otherwise.
"""
with _apps_lock:
return name in _apps


def delete_app(app):
"""Gracefully deletes an App instance.
Expand Down Expand Up @@ -129,9 +140,8 @@ def get_app(name=_DEFAULT_APP_NAME):
if not isinstance(name, str):
raise ValueError('Illegal app name argument type: "{}". App name '
'must be a string.'.format(type(name)))
with _apps_lock:
if name in _apps:
return _apps[name]
if is_app_initialized(name):
return _apps[name]

if name == _DEFAULT_APP_NAME:
raise ValueError(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ def test_app_init_with_default_config(self, env_test_case):
app = firebase_admin.initialize_app(CREDENTIAL, options=env_test_case.init_options)
assert app.options._options == env_test_case.want_options

def test_default_app_is_initialized(self, app_credential):
firebase_admin.initialize_app(app_credential)
assert firebase_admin.is_app_initialized()

def test_no_app_is_initialized(self):
assert firebase_admin.is_app_initialized() is False

def test_correct_app_is_initialized(self):
app_name = 'myApp'
firebase_admin.initialize_app(CREDENTIAL, name=app_name)
assert firebase_admin.is_app_initialized(app_name)

def test_correct_app_is_not_initialized(self):
app_name = 'myApp'
other_app_name = 'otherApp'
firebase_admin.initialize_app(CREDENTIAL, name=app_name)
assert firebase_admin.is_app_initialized(other_app_name) is False

def test_project_id_from_options(self, app_credential):
app = firebase_admin.initialize_app(
app_credential, options={'projectId': 'test-project'}, name='myApp')
Expand Down

0 comments on commit b150b03

Please sign in to comment.