async def test_async_run(): coro = CoroutineMock() WrappedFunction._EVENT_LOOP = asyncio.get_event_loop() f = WrappedFunction(coro, name='coro_mock') f.run() await asyncio.sleep(0.05) coro.assert_awaited_once()
async def test_async_args(): coro = AsyncMock() f = WrappedFunction(coro, name='coro_mock') f.run('arg1', 'arg2', kw1='kw1') await asyncio.sleep(0.05) coro.assert_awaited_once_with('arg1', 'arg2', kw1='kw1')
async def test_async_args(): coro = CoroutineMock() WrappedFunction._EVENT_LOOP = asyncio.get_event_loop() f = WrappedFunction(coro, name='coro_mock') f.run('arg1', 'arg2', kw1='kw1') await asyncio.sleep(0.05) coro.assert_awaited_once_with('arg1', 'arg2', kw1='kw1')
def test_exception1(self): def tmp(): 1 / 0 WrappedFunction._EVENT_LOOP = asyncio.get_event_loop() f = WrappedFunction(tmp) self.assertFalse(self.err_func.called) f.run() self.assertTrue(self.err_func.called) err = self.err_func.call_args[0][0] assert isinstance(err, HABApp.core.events.habapp_events.HABAppError) assert err.func_name == 'tmp' assert isinstance(err.exception, ZeroDivisionError) assert err.traceback.startswith('Traceback (most recent call last):')
def test_exception1(self): def tmp(): 1 / 0 f = WrappedFunction(tmp) self.assertFalse(self.err_func.called) f.run() self.assertTrue(self.err_func.called) err = self.err_func.call_args[0][0] assert isinstance(err, HABApp.core.events.habapp_events.HABAppException) assert err.func_name == 'tmp' assert isinstance(err.exception, ZeroDivisionError) assert err.traceback.startswith('File ')
async def test_async_error_wrapper(): async def tmp(): 1 / 0 f = WrappedFunction(tmp) err_func = AsyncMock() err_listener = HABApp.core.EventBusListener( TOPIC_ERRORS, WrappedFunction(err_func, name='ErrMock')) HABApp.core.EventBus.add_listener(err_listener) f.run() await asyncio.sleep(0.05) assert err_func.called err = err_func.call_args[0][0] assert isinstance(err, HABApp.core.events.habapp_events.HABAppException) assert err.func_name == 'tmp' assert isinstance(err.exception, ZeroDivisionError) assert err.traceback.startswith('File ')
async def test_async_error_wrapper(): async def tmp(): 1 / 0 f = WrappedFunction(tmp) WrappedFunction._EVENT_LOOP = asyncio.get_event_loop() err_func = CoroutineMock() err_listener = HABApp.core.EventBusListener( 'HABApp.Errors', WrappedFunction(err_func, name='ErrMock')) HABApp.core.EventBus.add_listener(err_listener) f.run() await asyncio.sleep(0.05) assert err_func.called err = err_func.call_args[0][0] assert isinstance(err, HABApp.core.events.habapp_events.HABAppError) assert err.func_name == 'tmp' assert isinstance(err.exception, ZeroDivisionError) assert err.traceback.startswith('Traceback (most recent call last):')
def test_sync_args(self): f = WrappedFunction(self.func_call) f.run('sarg1', 'sarg2', skw1='skw1') self.assertTrue(self.func_called) self.assertEqual(self.last_args, ('sarg1', 'sarg2')) self.assertEqual(self.last_kwargs, {'skw1': 'skw1'})
def test_sync_run(self): f = WrappedFunction(self.func_call) f.run() self.assertTrue(self.func_called)
async def test_async_run(): coro = AsyncMock() f = WrappedFunction(coro, name='coro_mock') f.run() await asyncio.sleep(0.05) coro.assert_awaited_once()