当前位置: 首页>>代码示例>>Python>>正文


Python asynctest.mock方法代码示例

本文整理汇总了Python中asynctest.mock方法的典型用法代码示例。如果您正苦于以下问题:Python asynctest.mock方法的具体用法?Python asynctest.mock怎么用?Python asynctest.mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在asynctest的用法示例。


在下文中一共展示了asynctest.mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUp

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def setUp(self):
        super().setUp()

        self.mock_gui = asynctest.mock.MagicMock()
        argspecs = ({'names': ('A',), 'type': int, 'description': 'First number'},
                    {'names': ('B',), 'type': int, 'description': 'Second number'})

        def run_add(self_, A, B, _gui=self.mock_gui):
            print('add called with %r, %r' % (A, B))
            _gui.display('Result: %d' % (int(A) + int(B),))

        self.cmdmgr.register(make_cmdcls(name='add', run=run_add, argspecs=argspecs, provides=('gui',)))

        async def run_sub(self_, A, B, _gui=self.mock_gui):
            print('sub called with %r, %r' % (A, B))
            _gui.display('Result: %d' % (int(A) - int(B),))
            await asyncio.sleep(0)

        self.cmdmgr.register(make_cmdcls(name='sub', run=run_sub, argspecs=argspecs, provides=('tui',))) 
开发者ID:rndusr,项目名称:stig,代码行数:21,代码来源:cmdmanager_test.py

示例2: test_discovering_focused_file

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_discovering_focused_file(self):
        from stig.commands.cli import RenameCmd
        self.mock_get_relative_path_from_focused.return_value = 'id=1234/mock/path/to/file'
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['file2'], info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_called_once_with(unique=False)
        self.mock_select_torrents.assert_called_once_with('id=1234', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_called_once_with(1234, path='mock/path/to/file', new_name='file2') 
开发者ID:rndusr,项目名称:stig,代码行数:21,代码来源:torrent_cmds_test.py

示例3: test_specifying_torrent

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_specifying_torrent(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['id=1234', 'Renamed Torrent'], info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('id=1234', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_called_once_with(1234, path=None, new_name='Renamed Torrent') 
开发者ID:rndusr,项目名称:stig,代码行数:20,代码来源:torrent_cmds_test.py

示例4: test_specifying_file

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_specifying_file(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['id=1234/mock/path/to/file', 'file2'], info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('id=1234', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_called_once_with(1234, path='mock/path/to/file', new_name='file2') 
开发者ID:rndusr,项目名称:stig,代码行数:20,代码来源:torrent_cmds_test.py

示例5: test_torrent_filter_must_match_one_torrent_when_renaming_torrents

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_torrent_filter_must_match_one_torrent_when_renaming_torrents(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),
                      MockTorrent(id=1235, name='Some Torrent')))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['Some Torrent', 'Renamed Torrent'], info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, False)
        info_cb.assert_not_called()
        err_cb.assert_called_once_with('rename: mock filter matches more than one torrent')

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('Some Torrent', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_not_called() 
开发者ID:rndusr,项目名称:stig,代码行数:21,代码来源:torrent_cmds_test.py

示例6: test_renaming_files_of_multiple_torrents_succeeds

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_renaming_files_of_multiple_torrents_succeeds(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),
                      MockTorrent(id=1235, name='Some Torrent')))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['Some Torrent/mock/path/to/file', 'file2'], info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('Some Torrent', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.assertEqual(self.mock_srvapi.torrent.rename.call_args_list,
                         [call(1234, path='mock/path/to/file', new_name='file2'),
                          call(1235, path='mock/path/to/file', new_name='file2')]) 
开发者ID:rndusr,项目名称:stig,代码行数:23,代码来源:torrent_cmds_test.py

示例7: test_using_unique_option_in_TUI_with_multiple_matches

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_using_unique_option_in_TUI_with_multiple_matches(self):
        from stig.commands.tui import RenameCmd
        self.mock_get_relative_path_from_focused.return_value = 'Some Torrent/focused/file'
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),
                      MockTorrent(id=1235, name='Some Torrent')))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['--unique', 'file2'],
                            info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, False)
        info_cb.assert_not_called()
        err_cb.assert_called_once_with('rename: mock filter matches more than one torrent')

        self.mock_get_relative_path_from_focused.assert_called_once_with(unique=True)
        self.mock_select_torrents.assert_called_once_with('Some Torrent', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.assertEqual(self.mock_srvapi.torrent.rename.call_args_list, []) 
开发者ID:rndusr,项目名称:stig,代码行数:23,代码来源:torrent_cmds_test.py

示例8: test_using_unique_option_in_CLI_with_single_match

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_using_unique_option_in_CLI_with_single_match(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1235, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['--unique', 'id=1235/path/to/file', 'file2'],
                            info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('id=1235', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.assertEqual(self.mock_srvapi.torrent.rename.call_args_list,
                         [call(1235, path='path/to/file', new_name='file2')]) 
开发者ID:rndusr,项目名称:stig,代码行数:22,代码来源:torrent_cmds_test.py

示例9: test_using_unique_option_in_CLI_with_multiple_matches

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_using_unique_option_in_CLI_with_multiple_matches(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),
                      MockTorrent(id=1235, name='Some Torrent')))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['--unique', 'Some Torrent/path/to/file', 'file2'],
                            info_handler=info_cb, error_handler=err_cb)
        await process.wait_async()
        self.assertEqual(process.success, False)
        info_cb.assert_not_called()
        err_cb.assert_called_once_with('rename: mock filter matches more than one torrent')

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('Some Torrent', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.assertEqual(self.mock_srvapi.torrent.rename.call_args_list, []) 
开发者ID:rndusr,项目名称:stig,代码行数:22,代码来源:torrent_cmds_test.py

示例10: test_TUI_completion_candidates__options_are_ignored

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_TUI_completion_candidates__options_are_ignored(
            self, mock_get_focused_item_source, mock_torrent_path):
        from stig.commands.tui import RenameCmd
        mock_torrent_path.return_value = 'mock candidates'
        mock_get_focused_item_source.return_value = None
        for args in (Args(('rename', '-a', 'foo'), curarg_index=2, curarg_curpos=0),
                     Args(('rename', '-a', '-b', 'foo'), curarg_index=3, curarg_curpos=0),
                     Args(('rename', '-a', '-b', '--cee', 'foo'), curarg_index=4, curarg_curpos=0)):
            cands = await RenameCmd.completion_candidates(args)
            from logging import getLogger
            log = getLogger()
            log.debug(mock_torrent_path.call_args_list)
            mock_torrent_path.assert_called_once_with('foo', only='any')
            mock_get_focused_item_source.assert_called_once_with()
            self.assertEqual(cands, 'mock candidates')
            mock_torrent_path.reset_mock()
            mock_get_focused_item_source.reset_mock() 
开发者ID:rndusr,项目名称:stig,代码行数:19,代码来源:torrent_cmds_test.py

示例11: inject_class

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def inject_class(obj):
    # Decorate _Test_* mixin classes so we can retrieve the mock class to test
    # with the last argument of the test function ("klass").
    if isinstance(obj, type):
        for attr_name in dir(obj):
            attr = getattr(obj, attr_name)
            if callable(attr) and attr_name.startswith('test_'):
                setattr(obj, attr_name, inject_class(attr))

        return obj
    else:
        @functools.wraps(obj)
        def wrapper(self):
            return obj(self, getattr(asynctest, self.class_to_test))

        return wrapper 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_mock.py

示例12: test_mock_customize_async_context_manager_with_coroutine

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_mock_customize_async_context_manager_with_coroutine(self, klass):
        enter_called = False
        exit_called = False

        async def enter_coroutine(*args):
            nonlocal enter_called
            enter_called = True

        async def exit_coroutine(*args):
            nonlocal exit_called
            exit_called = True

        instance = self.WithAsyncContextManager()
        mock_instance = asynctest.mock.MagicMock(instance)

        mock_instance.__aenter__ = enter_coroutine
        mock_instance.__aexit__ = exit_coroutine

        async def use_context_manager():
            async with mock_instance:
                pass

        run_coroutine(use_context_manager())
        self.assertTrue(enter_called)
        self.assertTrue(exit_called) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:27,代码来源:test_mock.py

示例13: test_awaited_CoroutineMock_sets_awaited

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_awaited_CoroutineMock_sets_awaited(self):
        mock = asynctest.mock.CoroutineMock()
        yield from mock()
        self.assertTrue(mock.awaited)

        mock.reset_mock()
        self.assertFalse(mock.awaited)

        @asyncio.coroutine
        def side_effect():
            raise RuntimeError()

        mock = asynctest.mock.CoroutineMock(side_effect=side_effect)

        with self.assertRaises(RuntimeError):
            yield from mock() 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_mock.py

示例14: test_awaited_wait_next

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_awaited_wait_next(self):
        mock = asynctest.mock.CoroutineMock()
        yield from mock()
        t = asyncio.ensure_future(mock.awaited.wait_next())
        yield from asyncio.sleep(0.01)
        self.assertFalse(t.done())
        yield from mock()
        yield from t

        mock.reset_mock()
        yield from mock()
        t = asyncio.ensure_future(mock.awaited.wait_next(skip=1))
        yield from asyncio.sleep(0.01)
        yield from mock()
        self.assertFalse(t.done())
        yield from mock()
        yield from t 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:19,代码来源:test_mock.py

示例15: test_await_args_list

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import mock [as 别名]
def test_await_args_list(self):
        with self.subTest('in order'):
            mock = asynctest.mock.CoroutineMock()
            t1 = mock('foo')
            t2 = mock('bar')
            yield from t1
            yield from t2
            self.assertEqual(mock.await_args_list, [asynctest.call('foo'), asynctest.call('bar')])

        with self.subTest('out of order'):
            mock = asynctest.mock.CoroutineMock()
            t1 = mock('foo')
            t2 = mock('bar')
            yield from t2
            yield from t1
            self.assertEqual(mock.await_args_list, [asynctest.call('bar'), asynctest.call('foo')]) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_mock.py


注:本文中的asynctest.mock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。