Skip to content

Refine Firebase mock fallback to selectively handle initialization ValueError#133

Merged
DaTiC0 merged 3 commits into
mainfrom
fix-firebase-init-mock-16749792168740504321
Jun 5, 2026
Merged

Refine Firebase mock fallback to selectively handle initialization ValueError#133
DaTiC0 merged 3 commits into
mainfrom
fix-firebase-init-mock-16749792168740504321

Conversation

@DaTiC0

@DaTiC0 DaTiC0 commented Jun 5, 2026

Copy link
Copy Markdown
Owner
  • Refactored reference() in firebase_utils.py to catch ValueError instead of a broad Exception.
  • Added tests in tests/test_firebase_utils.py to assert that ValueError properly triggers the mock fallback, while other exceptions correctly bubble up.

PR created automatically by Jules for task 16749792168740504321 started by @DaTiC0

Summary by Sourcery

Limit Firebase reference fallback to uninitialized-app ValueError and verify behavior with targeted tests.

Bug Fixes:

  • Restrict Firebase reference error handling to ValueError so non-initialization errors surface correctly.

Tests:

  • Add unit tests ensuring ValueError from Firebase triggers mock fallback while other exception types propagate.

The fallback mock database functionality in `firebase_utils.py` (which previously resided in `action_devices.py`) was erroneously catching all exceptions. This masked actual runtime issues such as network connectivity or permission errors by incorrectly returning mock data in production. This commit refactors the `try...except` block to precisely catch `ValueError`, which is uniquely raised by the `firebase-admin` SDK when an app is imported but not initialized (e.g. missing credentials during local development). It also adds comprehensive unit tests to ensure this fallback works correctly without suppressing other errors.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Narrows the Firebase reference() error handling to only treat ValueError as an initialization issue that triggers a MockRef fallback, and adds tests to verify that ValueError leads to the fallback while other exceptions propagate.

Flow diagram for refined Firebase reference error handling

flowchart TD
    A[call reference user_id] --> B{Firebase db.reference call}
    B --> C{ValueError raised?}
    C -->|yes| D[log warning Firebase not initialized]
    D --> E[return MockRef]
    C -->|no| F{Other exception raised?}
    F -->|yes| G[exception bubbles up]
    F -->|no| H[return real db.reference]
Loading

File-Level Changes

Change Details Files
Restrict firebase_utils.reference() fallback to only ValueError when Firebase is uninitialized.
  • Change the broad exception handler around db.reference() to catch only ValueError raised during Firebase reference creation.
  • Preserve the existing behavior of logging a warning and returning MockRef() when a ValueError occurs.
  • Ensure that other exception types now bubble up instead of being silently converted to a MockRef fallback.
firebase_utils.py
Add unit tests to cover ValueError-based fallback behavior and bubbling of other exceptions.
  • Introduce a test case that mocks firebase_admin.db.reference to raise ValueError, asserts FIREBASE_AVAILABLE is True, and verifies reference() returns MockRef and logs a warning.
  • Introduce a test case that mocks firebase_admin.db.reference to raise a non-ValueError (PermissionError) and asserts that reference() propagates the exception without falling back.
  • Use sys.modules manipulation to reload firebase_utils with mocked firebase_admin for controlled test scenarios.
tests/test_firebase_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The tests repeatedly manipulate sys.modules to force a re-import of firebase_utils; consider extracting this into a small helper or using importlib.reload to reduce duplication and make the module reloading behavior less brittle.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The tests repeatedly manipulate `sys.modules` to force a re-import of `firebase_utils`; consider extracting this into a small helper or using `importlib.reload` to reduce duplication and make the module reloading behavior less brittle.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the exception handling in firebase_utils.py to catch ValueError instead of a generic Exception when initializing the Firebase reference, and introduces a new test suite to verify this behavior. The reviewer recommends avoiding sys.modules manipulation in the tests to prevent potential side effects and test isolation issues, suggesting a cleaner mocking approach using unittest.mock.patch instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1 to +45
import unittest
from unittest.mock import patch, MagicMock

class TestFirebaseUtils(unittest.TestCase):
def test_reference_value_error_fallback(self):
# We must clear the module to reload it with our mocked FIREBASE_AVAILABLE state
import sys
if 'firebase_utils' in sys.modules:
del sys.modules['firebase_utils']

# Mock firebase_admin and db to simulate an uninitialized state where db.reference raises ValueError
mock_db = MagicMock()
mock_db.reference.side_effect = ValueError("The default Firebase app does not exist.")

with patch.dict(sys.modules, {'firebase_admin': MagicMock(db=mock_db)}):
import firebase_utils

# FIREBASE_AVAILABLE should be True because the import succeeds (mocked)
self.assertTrue(firebase_utils.FIREBASE_AVAILABLE)

with patch('firebase_utils.logger.warning') as mock_logger:
ref = firebase_utils.reference()

# Should fallback to MockRef
self.assertIsInstance(ref, firebase_utils.MockRef)
mock_logger.assert_called_once()
self.assertIn("Firebase not initialized", mock_logger.call_args[0][0])

def test_reference_other_exception_bubbles_up(self):
import sys
if 'firebase_utils' in sys.modules:
del sys.modules['firebase_utils']

mock_db = MagicMock()
# Some other error like network permission denied
mock_db.reference.side_effect = PermissionError("Permission denied.")

with patch.dict(sys.modules, {'firebase_admin': MagicMock(db=mock_db)}):
import firebase_utils

with self.assertRaises(PermissionError):
firebase_utils.reference()

if __name__ == '__main__':
unittest.main()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Manipulating sys.modules by deleting and reloading modules during tests can lead to unexpected side effects, such as breaking other tests or causing issues with modules that have already imported firebase_utils (e.g., action_devices).

Instead of reloading the module, you can use unittest.mock.patch to directly mock firebase_utils.FIREBASE_AVAILABLE and firebase_utils.db (using create=True to handle cases where db is not defined due to a missing firebase_admin installation). This is much cleaner, more idiomatic, and avoids test isolation issues.

import unittest
from unittest.mock import patch, MagicMock
import firebase_utils

class TestFirebaseUtils(unittest.TestCase):
    @patch('firebase_utils.FIREBASE_AVAILABLE', True)
    @patch('firebase_utils.db', create=True)
    def test_reference_value_error_fallback(self, mock_db):
        mock_db.reference.side_effect = ValueError("The default Firebase app does not exist.")

        with patch('firebase_utils.logger.warning') as mock_logger:
            ref = firebase_utils.reference()

            # Should fallback to MockRef
            self.assertIsInstance(ref, firebase_utils.MockRef)
            mock_logger.assert_called_once()
            self.assertIn("Firebase not initialized", mock_logger.call_args[0][0])

    @patch('firebase_utils.FIREBASE_AVAILABLE', True)
    @patch('firebase_utils.db', create=True)
    def test_reference_other_exception_bubbles_up(self, mock_db):
        mock_db.reference.side_effect = PermissionError("Permission denied.")

        with self.assertRaises(PermissionError):
            firebase_utils.reference()

if __name__ == '__main__':
    unittest.main()

google-labs-jules Bot and others added 2 commits June 5, 2026 06:09
The fallback mock database functionality in `firebase_utils.py` (which previously resided in `action_devices.py`) was erroneously catching all exceptions. This masked actual runtime issues such as network connectivity or permission errors by incorrectly returning mock data in production. This commit refactors the `try...except` block to precisely catch `ValueError`, which is uniquely raised by the `firebase-admin` SDK when an app is imported but not initialized (e.g. missing credentials during local development). It also adds comprehensive unit tests to ensure this fallback works correctly without suppressing other errors.

Additionally, this commit updates `tests/test_multi_tenant.py` to fix unit tests that were broken by the refactoring, replacing broad module mocks with precise functional mocks where needed.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
The fallback mock database functionality in `firebase_utils.py` (which previously resided in `action_devices.py`) was erroneously catching all exceptions. This masked actual runtime issues such as network connectivity or permission errors by incorrectly returning mock data in production. This commit refactors the `try...except` block to precisely catch `ValueError`, which is uniquely raised by the `firebase-admin` SDK when an app is imported but not initialized (e.g. missing credentials during local development). It also adds comprehensive unit tests to ensure this fallback works correctly without suppressing other errors.

Additionally, this commit updates `tests/test_multi_tenant.py` to fix unit tests that were broken by the refactoring, replacing broad module mocks with precise functional mocks where needed.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
@DaTiC0 DaTiC0 merged commit 67b4c1c into main Jun 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant