Skip to content
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

chore: Add tests for Reference.listen() #851

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading