Skip to content

Commit

Permalink
chore: Add tests for Reference.listen() (#851)
Browse files Browse the repository at this point in the history
* chore: Add unit tests for `Reference.listen()`

* Integration test for rtdb listeners

* fix lint
  • Loading branch information
jonathanedey authored Jan 22, 2025
1 parent e5618c0 commit e6c95e7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
32 changes: 32 additions & 0 deletions integration/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import collections
import json
import os
import time

import pytest

Expand Down Expand Up @@ -245,6 +246,37 @@ def test_delete(self, testref):
ref.delete()
assert ref.get() is None

class TestListenOperations:
"""Test cases for listening to changes to node values."""

def test_listen(self, testref):
self.events = []
def callback(event):
self.events.append(event)

python = testref.parent
registration = python.listen(callback)
try:
ref = python.child('users').push()
assert ref.path == '/_adminsdk/python/users/' + ref.key
assert ref.get() == ''

self.wait_for(self.events, count=2)
assert len(self.events) == 2

assert self.events[1].event_type == 'put'
assert self.events[1].path == '/users/' + ref.key
assert self.events[1].data == ''
finally:
registration.close()

@classmethod
def wait_for(cls, events, count=1, timeout_seconds=5):
must_end = time.time() + timeout_seconds
while time.time() < must_end:
if len(events) >= count:
return
raise pytest.fail('Timed out while waiting for events')

class TestAdvancedQueries:
"""Test cases for advanced interactions via the db.Query interface."""
Expand Down
43 changes: 43 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,49 @@ def callback(_):
finally:
testutils.cleanup_apps()

@pytest.mark.parametrize(
'url,emulator_host,expected_base_url,expected_namespace',
[
# Production URLs with no override:
('https://test.firebaseio.com', None, 'https://test.firebaseio.com/.json', None),
('https://test.firebaseio.com/', None, 'https://test.firebaseio.com/.json', None),
# Production URLs with emulator_host override:
('https://test.firebaseio.com', 'localhost:9000', 'http://localhost:9000/.json',
'test'),
('https://test.firebaseio.com/', 'localhost:9000', 'http://localhost:9000/.json',
'test'),
# Emulator URL with no override.
('http://localhost:8000/?ns=test', None, 'http://localhost:8000/.json', 'test'),
# emulator_host is ignored when the original URL is already emulator.
('http://localhost:8000/?ns=test', 'localhost:9999', 'http://localhost:8000/.json',
'test'),
]
)
def test_listen_sse_client(self, url, emulator_host, expected_base_url, expected_namespace,
mocker):
if emulator_host:
os.environ[_EMULATOR_HOST_ENV_VAR] = emulator_host

try:
firebase_admin.initialize_app(testutils.MockCredential(), {'databaseURL' : url})
ref = db.reference()
mock_sse_client = mocker.patch('firebase_admin._sseclient.SSEClient')
mock_callback = mocker.Mock()
ref.listen(mock_callback)
args, kwargs = mock_sse_client.call_args
assert args[0] == expected_base_url
if expected_namespace:
assert kwargs.get('params') == {'ns': expected_namespace}
else:
assert kwargs.get('params') == {}
finally:
if _EMULATOR_HOST_ENV_VAR in os.environ:
del os.environ[_EMULATOR_HOST_ENV_VAR]
testutils.cleanup_apps()

def test_listener_session(self):
firebase_admin.initialize_app(testutils.MockCredential(), {
'databaseURL' : 'https://test.firebaseio.com',
Expand Down

0 comments on commit e6c95e7

Please sign in to comment.