From 70f93900574a4dced15550f138fa9e7384d6b8af Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 14 Dec 2025 19:05:21 +0000 Subject: [PATCH] more unit tests --- tests/async/test_pubsub_manager.py | 117 ++++++++++++++++++++++++++++ tests/common/test_pubsub_manager.py | 27 +++++++ 2 files changed, 144 insertions(+) diff --git a/tests/async/test_pubsub_manager.py b/tests/async/test_pubsub_manager.py index 0da08d2..abf41a2 100644 --- a/tests/async/test_pubsub_manager.py +++ b/tests/async/test_pubsub_manager.py @@ -374,6 +374,20 @@ class TestAsyncPubSubManager: ) async def test_handle_emit(self): + with mock.patch.object( + async_manager.AsyncManager, 'emit' + ) as super_emit: + await self.pm._handle_emit({'event': 'foo', 'data': ['bar']}) + super_emit.assert_awaited_once_with( + 'foo', + 'bar', + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + + async def test_handle_legacy_emit(self): with mock.patch.object( async_manager.AsyncManager, 'emit' ) as super_emit: @@ -388,6 +402,37 @@ class TestAsyncPubSubManager: ) async def test_handle_emit_binary(self): + with mock.patch.object( + async_manager.AsyncManager, 'emit' + ) as super_emit: + await self.pm._handle_emit({ + 'event': 'foo', + 'binary': True, + 'data': [[{'_placeholder': True, 'num': 0}], 'YmFy'], + }) + super_emit.assert_awaited_once_with( + 'foo', + b'bar', + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + await self.pm._handle_emit({ + 'event': 'foo', + 'binary': True, + 'data': [[{'foo': {'_placeholder': True, 'num': 0}}], 'YmFy'], + }) + super_emit.assert_awaited_with( + 'foo', + {'foo': b'bar'}, + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + + async def test_handle_legacy_emit_binary(self): with mock.patch.object( async_manager.AsyncManager, 'emit' ) as super_emit: @@ -418,6 +463,78 @@ class TestAsyncPubSubManager: callback=None, ) + async def test_handle_emit_list(self): + with mock.patch.object( + async_manager.AsyncManager, 'emit' + ) as super_emit: + await self.pm._handle_emit({'event': 'foo', 'data': [[1, 'two']]}) + super_emit.assert_awaited_once_with( + 'foo', + [1, 'two'], + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + await self.pm._handle_emit({ + 'event': 'foo', + 'binary': True, + 'data': [ + [[1, {'_placeholder': True, 'num': 0}, 'three']], 'dHdv' + ] + }) + super_emit.assert_awaited_with( + 'foo', + [1, b'two', 'three'], + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + + async def test_handle_emit_no_arguments(self): + with mock.patch.object( + async_manager.AsyncManager, 'emit' + ) as super_emit: + await self.pm._handle_emit({'event': 'foo', 'data': []}) + super_emit.assert_awaited_once_with( + 'foo', + (), + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + + async def test_handle_emit_multiple_arguments(self): + with mock.patch.object( + async_manager.AsyncManager, 'emit' + ) as super_emit: + await self.pm._handle_emit({'event': 'foo', 'data': [1, 'two']}) + super_emit.assert_awaited_once_with( + 'foo', + (1, 'two'), + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + await self.pm._handle_emit({ + 'event': 'foo', + 'binary': True, + 'data': [ + [1, {'_placeholder': True, 'num': 0}, 'three'], 'dHdv' + ] + }) + super_emit.assert_awaited_with( + 'foo', + (1, b'two', 'three'), + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + async def test_handle_emit_with_namespace(self): with mock.patch.object( async_manager.AsyncManager, 'emit' diff --git a/tests/common/test_pubsub_manager.py b/tests/common/test_pubsub_manager.py index 227a2ee..91b7785 100644 --- a/tests/common/test_pubsub_manager.py +++ b/tests/common/test_pubsub_manager.py @@ -466,6 +466,33 @@ class TestPubSubManager: callback=None, ) + def test_handle_emit_list(self): + with mock.patch.object(manager.Manager, 'emit') as super_emit: + self.pm._handle_emit({'event': 'foo', 'data': [[1, 'two']]}) + super_emit.assert_called_once_with( + 'foo', + [1, 'two'], + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + self.pm._handle_emit({ + 'event': 'foo', + 'binary': True, + 'data': [ + [[1, {'_placeholder': True, 'num': 0}, 'three']], 'dHdv' + ], + }) + super_emit.assert_called_with( + 'foo', + [1, b'two', 'three'], + namespace=None, + room=None, + skip_sid=None, + callback=None, + ) + def test_handle_emit_no_arguments(self): with mock.patch.object(manager.Manager, 'emit') as super_emit: self.pm._handle_emit({'event': 'foo', 'data': []})