-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.reference #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e26870d
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] 2bb8bef
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] bb58e60
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] 5a76613
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] 997c109
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] e2b17a4
Merge branch 'main' into test-firebase-utils-exception-42054468350276β¦
DaTiC0 5fbd68e
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] a78d992
π§ͺ [testing improvement] Untested Exception Path in firebase_utils.refβ¦
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,109 @@ | ||
| import unittest | ||
| from unittest.mock import patch, MagicMock | ||
| from unittest.mock import MagicMock, patch | ||
| import firebase_utils | ||
|
|
||
|
|
||
| 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 | ||
| def test_reference_root_success(self): | ||
| # Mock db as it might not be imported if firebase_admin is missing | ||
| mock_db = MagicMock() | ||
| with patch('firebase_utils.FIREBASE_AVAILABLE', True), \ | ||
| patch('firebase_utils.db', mock_db, create=True): | ||
| ref = firebase_utils.reference() | ||
| mock_db.reference.assert_called_with('/devices') | ||
| self.assertNotIsInstance(ref, firebase_utils.MockRef) | ||
|
|
||
| def test_reference_user_success(self): | ||
| mock_db = MagicMock() | ||
| mock_db.reference.side_effect = ValueError("The default Firebase app does not exist.") | ||
| with patch('firebase_utils.FIREBASE_AVAILABLE', True), \ | ||
| patch('firebase_utils.db', mock_db, create=True): | ||
| ref = firebase_utils.reference(user_id="user123") | ||
| mock_db.reference.assert_called_with('/users/user123/devices') | ||
| self.assertNotIsInstance(ref, firebase_utils.MockRef) | ||
|
|
||
| with patch.dict(sys.modules, {'firebase_admin': MagicMock(db=mock_db)}): | ||
| import firebase_utils | ||
| def test_reference_exception_path(self): | ||
| # Ensure FIREBASE_AVAILABLE is True for this test | ||
| mock_db = MagicMock() | ||
| mock_db.reference.side_effect = Exception("Firebase initialization error") | ||
| with patch('firebase_utils.FIREBASE_AVAILABLE', True), \ | ||
| patch('firebase_utils.db', mock_db, create=True), \ | ||
| patch('firebase_utils.logger.warning') as mock_warning: | ||
| ref = firebase_utils.reference() | ||
|
|
||
| # FIREBASE_AVAILABLE should be True because the import succeeds (mocked) | ||
| self.assertTrue(firebase_utils.FIREBASE_AVAILABLE) | ||
| # Verify it returns a MockRef instance | ||
| self.assertIsInstance(ref, firebase_utils.MockRef) | ||
|
|
||
| with patch('firebase_utils.logger.warning') as mock_logger: | ||
| ref = firebase_utils.reference() | ||
| # Verify the warning was logged | ||
| mock_warning.assert_called() | ||
| self.assertIn("Firebase not initialized", mock_warning.call_args[0][0]) | ||
|
|
||
| # 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_firebase_not_available(self): | ||
| with patch('firebase_utils.FIREBASE_AVAILABLE', False): | ||
| ref = firebase_utils.reference() | ||
| self.assertIsInstance(ref, firebase_utils.MockRef) | ||
|
|
||
| def test_reference_other_exception_bubbles_up(self): | ||
| import sys | ||
| if 'firebase_utils' in sys.modules: | ||
| del sys.modules['firebase_utils'] | ||
| def test_normalize_user_scope(self): | ||
| self.assertEqual(firebase_utils._normalize_user_scope("user123"), "user123") | ||
| self.assertEqual(firebase_utils._normalize_user_scope(123), "123") | ||
| self.assertEqual(firebase_utils._normalize_user_scope(" user123 "), "user123") | ||
| self.assertIsNone(firebase_utils._normalize_user_scope(None)) | ||
| self.assertIsNone(firebase_utils._normalize_user_scope("")) | ||
| self.assertIsNone(firebase_utils._normalize_user_scope(" ")) | ||
| self.assertIsNone(firebase_utils._normalize_user_scope("user/123")) | ||
| self.assertIsNone(firebase_utils._normalize_user_scope("user\\123")) | ||
| self.assertIsNone(firebase_utils._normalize_user_scope("user..123")) | ||
|
|
||
| def test_get_user_device_states_ref_valid(self): | ||
| mock_db = MagicMock() | ||
| # Some other error like network permission denied | ||
| mock_db.reference.side_effect = PermissionError("Permission denied.") | ||
| mock_ref = MagicMock() | ||
| mock_db.reference.return_value = mock_ref | ||
| mock_device_ref = MagicMock() | ||
| mock_ref.child.return_value = mock_device_ref | ||
| mock_states_ref = MagicMock() | ||
| mock_device_ref.child.return_value = mock_states_ref | ||
|
|
||
| with patch('firebase_utils.FIREBASE_AVAILABLE', True), \ | ||
| patch('firebase_utils.db', mock_db, create=True): | ||
|
|
||
| ref = firebase_utils._get_user_device_states_ref("user123", "device1") | ||
|
|
||
| mock_db.reference.assert_called_with('/users/user123/devices') | ||
| mock_ref.child.assert_called_with('device1') | ||
| mock_device_ref.child.assert_called_with('states') | ||
| self.assertEqual(ref, mock_states_ref) | ||
|
|
||
| def test_get_user_device_states_ref_invalid(self): | ||
| self.assertIsNone(firebase_utils._get_user_device_states_ref(None, "device1")) | ||
| self.assertIsNone(firebase_utils._get_user_device_states_ref("user/123", "device1")) | ||
| self.assertIsNone(firebase_utils._get_user_device_states_ref("user1", None)) | ||
| self.assertIsNone(firebase_utils._get_user_device_states_ref("user1", "device/1")) | ||
|
|
||
| def test_mock_ref_and_child(self): | ||
| ref = firebase_utils.MockRef() | ||
| data = ref.get() | ||
| self.assertEqual(data, firebase_utils.MOCK_DEVICES) | ||
|
|
||
| child = ref.child("test-light-1") | ||
| self.assertIsInstance(child, firebase_utils.MockChild) | ||
| self.assertEqual(child.get(), firebase_utils.MOCK_DEVICES["test-light-1"]) | ||
|
|
||
| grandchild = child.child("name") | ||
| self.assertEqual(grandchild.get(), firebase_utils.MOCK_DEVICES["test-light-1"]["name"]) | ||
|
|
||
| # Test non-existent path | ||
| self.assertIsNone(ref.child("non-existent").get()) | ||
|
|
||
| with patch.dict(sys.modules, {'firebase_admin': MagicMock(db=mock_db)}): | ||
| import firebase_utils | ||
| # Test update | ||
| # Careful as MOCK_DEVICES is shared. | ||
| original_states = firebase_utils.MOCK_DEVICES["test-light-1"]["states"].copy() | ||
| try: | ||
| update_data = {"on": not original_states["on"]} | ||
| child.child("states").update(update_data) | ||
| self.assertEqual(firebase_utils.MOCK_DEVICES["test-light-1"]["states"]["on"], not original_states["on"]) | ||
| finally: | ||
| firebase_utils.MOCK_DEVICES["test-light-1"]["states"] = original_states | ||
|
|
||
| with self.assertRaises(PermissionError): | ||
| firebase_utils.reference() | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add integration tests that exercise
_normalize_user_scopeviareference()to prove the normalized value is actually used in paths.Currently
_normalize_user_scopeis only tested in isolation. Please add an integration-style test forreference(user_id=...)that asserts the Firebase path uses the normalized value, e.g.:reference(user_id=" user123 ")callsmock_db.reference("/users/user123/devices")reference(user_id=123)callsmock_db.reference("/users/123/devices")This guards against regressions where
referencestops using_normalize_user_scopecorrectly.