Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite6c95e7

Browse files
authored
chore: Add tests forReference.listen() (#851)
* chore: Add unit tests for `Reference.listen()`* Integration test for rtdb listeners* fix lint
1 parente5618c0 commite6c95e7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

‎integration/test_db.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
importcollections
1717
importjson
1818
importos
19+
importtime
1920

2021
importpytest
2122

@@ -245,6 +246,37 @@ def test_delete(self, testref):
245246
ref.delete()
246247
assertref.get()isNone
247248

249+
classTestListenOperations:
250+
"""Test cases for listening to changes to node values."""
251+
252+
deftest_listen(self,testref):
253+
self.events= []
254+
defcallback(event):
255+
self.events.append(event)
256+
257+
python=testref.parent
258+
registration=python.listen(callback)
259+
try:
260+
ref=python.child('users').push()
261+
assertref.path=='/_adminsdk/python/users/'+ref.key
262+
assertref.get()==''
263+
264+
self.wait_for(self.events,count=2)
265+
assertlen(self.events)==2
266+
267+
assertself.events[1].event_type=='put'
268+
assertself.events[1].path=='/users/'+ref.key
269+
assertself.events[1].data==''
270+
finally:
271+
registration.close()
272+
273+
@classmethod
274+
defwait_for(cls,events,count=1,timeout_seconds=5):
275+
must_end=time.time()+timeout_seconds
276+
whiletime.time()<must_end:
277+
iflen(events)>=count:
278+
return
279+
raisepytest.fail('Timed out while waiting for events')
248280

249281
classTestAdvancedQueries:
250282
"""Test cases for advanced interactions via the db.Query interface."""

‎tests/test_db.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,49 @@ def callback(_):
535535
finally:
536536
testutils.cleanup_apps()
537537

538+
@pytest.mark.parametrize(
539+
'url,emulator_host,expected_base_url,expected_namespace',
540+
[
541+
# Production URLs with no override:
542+
('https://test.firebaseio.com',None,'https://test.firebaseio.com/.json',None),
543+
('https://test.firebaseio.com/',None,'https://test.firebaseio.com/.json',None),
544+
545+
# Production URLs with emulator_host override:
546+
('https://test.firebaseio.com','localhost:9000','http://localhost:9000/.json',
547+
'test'),
548+
('https://test.firebaseio.com/','localhost:9000','http://localhost:9000/.json',
549+
'test'),
550+
551+
# Emulator URL with no override.
552+
('http://localhost:8000/?ns=test',None,'http://localhost:8000/.json','test'),
553+
554+
# emulator_host is ignored when the original URL is already emulator.
555+
('http://localhost:8000/?ns=test','localhost:9999','http://localhost:8000/.json',
556+
'test'),
557+
]
558+
)
559+
deftest_listen_sse_client(self,url,emulator_host,expected_base_url,expected_namespace,
560+
mocker):
561+
ifemulator_host:
562+
os.environ[_EMULATOR_HOST_ENV_VAR]=emulator_host
563+
564+
try:
565+
firebase_admin.initialize_app(testutils.MockCredential(), {'databaseURL' :url})
566+
ref=db.reference()
567+
mock_sse_client=mocker.patch('firebase_admin._sseclient.SSEClient')
568+
mock_callback=mocker.Mock()
569+
ref.listen(mock_callback)
570+
args,kwargs=mock_sse_client.call_args
571+
assertargs[0]==expected_base_url
572+
ifexpected_namespace:
573+
assertkwargs.get('params')== {'ns':expected_namespace}
574+
else:
575+
assertkwargs.get('params')== {}
576+
finally:
577+
if_EMULATOR_HOST_ENV_VARinos.environ:
578+
delos.environ[_EMULATOR_HOST_ENV_VAR]
579+
testutils.cleanup_apps()
580+
538581
deftest_listener_session(self):
539582
firebase_admin.initialize_app(testutils.MockCredential(), {
540583
'databaseURL' :'https://test.firebaseio.com',

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp