コード例 #1
0
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()
コード例 #2
0
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')
コード例 #3
0
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')
コード例 #4
0
    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):')
コード例 #5
0
    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 ')
コード例 #6
0
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 ')
コード例 #7
0
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):')
コード例 #8
0
    def setUp(self):
        self.func_called = False
        self.last_args = None
        self.last_kwargs = None

        self.worker = WrappedFunction._WORKERS

        self.err_func: MagicMock = MagicMock()
        self.err_listener = HABApp.core.EventBusListener(
            'HABApp.Errors', WrappedFunction(self.err_func, name='ErrMock'))
        HABApp.core.EventBus.add_listener(self.err_listener)

        class CExecutor:
            def submit(self, callback, *args, **kwargs):
                callback(*args, **kwargs)

        WrappedFunction._WORKERS = CExecutor()
コード例 #9
0
 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'})
コード例 #10
0
 def test_sync_run(self):
     f = WrappedFunction(self.func_call)
     f.run()
     self.assertTrue(self.func_called)
コード例 #11
0
 def listen_events(self, name: str, cb):
     listener = EventBusListener(
         name, WrappedFunction(cb, name=f'TestFunc for name'))
     self.listener.append(listener)
     EventBus.add_listener(listener)
コード例 #12
0
async def test_async_run():
    coro = AsyncMock()
    f = WrappedFunction(coro, name='coro_mock')
    f.run()
    await asyncio.sleep(0.05)
    coro.assert_awaited_once()