async def test_dispatch_providers(route, event_loop): dispatcher = LoaferDispatcher([route]) dispatcher._dispatch_tasks = CoroutineMock() dispatcher.stop_providers = Mock() await dispatcher.dispatch_providers(event_loop, forever=False) assert dispatcher._dispatch_tasks.called dispatcher._dispatch_tasks.assert_called_once_with(event_loop)
async def test_dispatch_providers(route, event_loop): dispatcher = LoaferDispatcher([route]) dispatcher._dispatch_tasks = CoroutineMock() dispatcher.stop_providers = Mock() await dispatcher.dispatch_providers(event_loop, forever=False) assert dispatcher._dispatch_tasks.called dispatcher._dispatch_tasks.assert_called_once_with(event_loop)
async def test_message_processing(route): dispatcher = LoaferDispatcher([route]) dispatcher.dispatch_message = CoroutineMock() await dispatcher._process_message('message', route) assert dispatcher.dispatch_message.called assert dispatcher.dispatch_message.called_called_once_with('message', route) assert route.provider.confirm_message.called assert route.provider.confirm_message.called_once_with('message')
async def test_message_processing(route): dispatcher = LoaferDispatcher([route]) dispatcher.dispatch_message = CoroutineMock() await dispatcher._process_message('message', route) assert dispatcher.dispatch_message.called dispatcher.dispatch_message.assert_called_once_with('message', route) assert route.provider.confirm_message.called route.provider.confirm_message.assert_called_once_with('message')
async def test_process_route(route): dispatcher = LoaferDispatcher([route]) dispatcher.dispatch_message = CoroutineMock() await dispatcher.process_route(route) assert route.provider.fetch_messages.called assert dispatcher.dispatch_message.called assert dispatcher.dispatch_message.called_called_once_with('message', route) assert route.provider.confirm_message.called assert route.provider.confirm_message.called_once_with('message')
async def test_message_processing(route): dispatcher = LoaferDispatcher([route]) dispatcher.dispatch_message = AsyncMock() await dispatcher._process_message("message", route) assert dispatcher.dispatch_message.called dispatcher.dispatch_message.assert_called_once_with("message", route) assert route.provider.confirm_message.called assert route.provider.message_not_processed.called is False route.provider.confirm_message.assert_called_once_with("message")
async def test_dispatch_without_tasks(route, event_loop): route.provider.fetch_messages = CoroutineMock(return_value=[]) dispatcher = LoaferDispatcher([route]) dispatched = await dispatcher._dispatch_tasks(event_loop) assert len(dispatched) == 0 assert route.provider.fetch_messages.called
async def test_dispatch_tasks(route): route.provider.fetch_messages = CoroutineMock(return_value=['message']) dispatcher = LoaferDispatcher([route]) await dispatcher._dispatch_tasks() assert route.provider.fetch_messages.called assert route.provider.confirm_message.called
async def test_dispatch_tasks_disabled(route, event_loop): route.enabled = False dispatcher = LoaferDispatcher([route]) dispatched = await dispatcher._dispatch_tasks(event_loop) assert len(dispatched) == 0 assert route.provider.fetch_messages.called is False
async def test_dispatch_invalid_message(route, message): route.deliver = CoroutineMock() dispatcher = LoaferDispatcher([route]) confirmation = await dispatcher.dispatch_message(message, route) assert confirmation is False assert route.deliver.called is False
async def test_dispatch_without_tasks(route, event_loop): route.provider.fetch_messages = CoroutineMock(return_value=[]) dispatcher = LoaferDispatcher([route]) await dispatcher._dispatch_tasks() assert route.provider.fetch_messages.called assert route.provider.confirm_message.called is False
async def test_dispatch_tasks(route): route.provider.fetch_messages = AsyncMock(return_value=["message"]) dispatcher = LoaferDispatcher([route]) await dispatcher._dispatch_tasks() assert route.provider.fetch_messages.called assert route.provider.confirm_message.called assert route.provider.message_not_processed.called is False
async def test_dispatch_message_task_cancel(route): route.deliver = CoroutineMock(side_effect=asyncio.CancelledError) dispatcher = LoaferDispatcher([route]) message = 'message' confirmation = await dispatcher.dispatch_message(message, route) assert confirmation is False assert route.deliver.called route.deliver.assert_called_once_with(message)
async def test_dispatch_message_task_delete_message(route): route.deliver = CoroutineMock(side_effect=DeleteMessage) dispatcher = LoaferDispatcher([route]) message = 'rejected-message' confirmation = await dispatcher.dispatch_message(message, route) assert confirmation is True assert route.deliver.called route.deliver.assert_called_once_with(message)
async def test_dispatch_message(route): route.deliver = CoroutineMock(return_value='confirmation') dispatcher = LoaferDispatcher([route]) message = 'foobar' confirmation = await dispatcher.dispatch_message(message, route) assert confirmation == 'confirmation' assert route.deliver.called route.deliver.assert_called_once_with(message)
async def test_dispatch_message(route): route.deliver = AsyncMock(return_value="confirmation") dispatcher = LoaferDispatcher([route]) message = "foobar" confirmation = await dispatcher.dispatch_message(message, route) assert confirmation == "confirmation" assert route.deliver.called route.deliver.assert_called_once_with(message)
async def test_dispatch_message_task_error(route): exc = Exception() route.deliver = CoroutineMock(side_effect=exc) route.error_handler = CoroutineMock(return_value='confirmation') dispatcher = LoaferDispatcher([route]) message = 'message' confirmation = await dispatcher.dispatch_message(message, route) assert confirmation == 'confirmation' assert route.deliver.called is True route.deliver.assert_called_once_with(message) assert route.error_handler.called is True
def test_dispatcher_stop(route): route.stop = Mock() dispatcher = LoaferDispatcher([route]) dispatcher.stop() assert route.stop.called
async def test_dispatch_providers_with_error(route, event_loop): route.provider.fetch_messages.side_effect = ValueError dispatcher = LoaferDispatcher([route]) with pytest.raises(ValueError): await dispatcher.dispatch_providers(forever=False)
def test_dispatcher_stop(route): route.stop = Mock() dispatcher = LoaferDispatcher([route]) dispatcher.stop() assert route.stop.called