Python asynctest.mock() Examples
The following are 30
code examples of asynctest.mock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
asynctest
, or try the search function
.
Example #1
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
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
Example #2
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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')
Example #3
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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')
Example #4
Source File: cmdmanager_test.py From stig with GNU General Public License v3.0 | 6 votes |
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',)))
Example #5
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
def test_patch_decorates_coroutine(self): with self.subTest("old style coroutine"): @asynctest.mock.patch.multiple("test.test_mock.Test", is_patched=lambda self: True) @asyncio.coroutine def a_coroutine(): import test.test_mock return test.test_mock.Test().is_patched() self.assertTrue(run_coroutine(a_coroutine())) with self.subTest("native coroutine"): @asynctest.mock.patch.multiple("test.test_mock.Test", is_patched=lambda self: True) async def a_native_coroutine(): import test.test_mock return test.test_mock.Test().is_patched() self.assertTrue(run_coroutine(a_native_coroutine()))
Example #6
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
def test_patch_autospec_with_patches_on_top(self): called = False @asynctest.mock.patch("{}.{}".format(self.test_class_path, "is_patched"), return_value=True) @asynctest.mock.patch("{}.{}".format(self.test_class_path, "a_coroutine"), autospec=True) def patched_function(coroutine_mock, is_patched_mock): nonlocal called called = True self.assertIsInstance(Test.is_patched, asynctest.mock.Mock) self.assertTrue(Test.is_patched()) self.assertTrue(asyncio.iscoroutinefunction(coroutine_mock)) self.assertTrue(asyncio.iscoroutinefunction(Test.a_coroutine)) patched_function() self.assertTrue(called)
Example #7
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
def test_patch_autospec_with_patches_under(self): called = False @asynctest.mock.patch("{}.{}".format(self.test_class_path, "a_coroutine"), autospec=True) @asynctest.mock.patch("{}.{}".format(self.test_class_path, "is_patched"), return_value=True) def patched_function(is_patched_mock, coroutine_mock): nonlocal called called = True self.assertIsInstance(Test.is_patched, asynctest.mock.Mock) self.assertTrue(Test.is_patched()) self.assertTrue(asyncio.iscoroutinefunction(coroutine_mock)) self.assertTrue(asyncio.iscoroutinefunction(Test.a_coroutine)) patched_function() self.assertTrue(called)
Example #8
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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')
Example #9
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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()
Example #10
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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')])
Example #11
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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')])
Example #12
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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, [])
Example #13
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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()
Example #14
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
def test_patch_as_decorator_uses_MagicMock(self): called = [] @asynctest.mock.patch('test.test_mock.Test') def test_mock_class(mock): self.assertIsInstance(mock, asynctest.mock.MagicMock) called.append("test_mock_class") @asynctest.mock.patch('test.test_mock.Test.a_function') def test_mock_function(mock): self.assertIsInstance(mock, asynctest.mock.MagicMock) called.append("test_mock_function") test_mock_class() test_mock_function() self.assertIn("test_mock_class", called) self.assertIn("test_mock_function", called)
Example #15
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #16
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
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()
Example #17
Source File: torrent_cmds_test.py From stig with GNU General Public License v3.0 | 6 votes |
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, [])
Example #18
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
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')])
Example #19
Source File: test_mock.py From asynctest with Apache License 2.0 | 6 votes |
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
Example #20
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_assert_awaited_once_with(self): mock = asynctest.mock.CoroutineMock() with self.assertRaises(AssertionError): mock.assert_awaited_once_with('foo') yield from mock('foo') mock.assert_awaited_once_with('foo') yield from mock('foo') with self.assertRaises(AssertionError): mock.assert_awaited_once_with('foo')
Example #21
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_assert_awaited(self): mock = asynctest.mock.CoroutineMock() with self.assertRaises(AssertionError): mock.assert_awaited() yield from mock() mock.assert_awaited()
Example #22
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_patch_with_MagicMock(self): with asynctest.mock.patch.object(Test(), 'a_function') as mock: self.assertIsInstance(mock, asynctest.mock.MagicMock) obj = Test() obj.test = Test() with asynctest.mock.patch.object(obj, 'test') as mock: self.assertIsInstance(mock, asynctest.mock.MagicMock)
Example #23
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_assert_awaited_once(self): mock = asynctest.mock.CoroutineMock() with self.assertRaises(AssertionError): mock.assert_awaited_once() yield from mock() mock.assert_awaited_once() yield from mock() with self.assertRaises(AssertionError): mock.assert_awaited_once()
Example #24
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_assert_not_awaited(self): mock = asynctest.mock.CoroutineMock() mock.assert_not_awaited() yield from mock() with self.assertRaises(AssertionError): mock.assert_not_awaited()
Example #25
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_create_autospec_on_coroutine_and_using_assert_methods(self): mock = asynctest.create_autospec(Test.a_coroutine_with_args) mock.assert_not_awaited() yield from mock("arg0", "arg1", "arg2") mock.assert_awaited() # calls assert not awaited mock.assert_awaited_once() mock.assert_awaited_with("arg0", "arg1", "arg2") mock.assert_awaited_once_with("arg0", "arg1", "arg2") mock.assert_any_await("arg0", "arg1", "arg2") mock.assert_has_awaits([asynctest.call("arg0", "arg1", "arg2")])
Example #26
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_Mock_is_not_CoroutineMock(self): self.assertNotIsInstance(asynctest.mock.Mock(), asynctest.mock.CoroutineMock)
Example #27
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_MagicMock_is_not_CoroutineMock(self): self.assertNotIsInstance(asynctest.mock.MagicMock(), asynctest.mock.CoroutineMock)
Example #28
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_patch_as_context_manager_uses_MagicMock(self): with asynctest.mock.patch('test.test_mock.Test') as mock: self.assertIsInstance(mock, asynctest.mock.MagicMock) with asynctest.mock.patch('test.test_mock.Test.a_function') as mock: self.assertIsInstance(mock, asynctest.mock.MagicMock)
Example #29
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_patch_as_decorator_uses_CoroutineMock_on_coroutine_function(self): called = False @asynctest.mock.patch('test.test_mock.Test.a_coroutine') def test_mock_coroutine(mock): nonlocal called self.assertIsInstance(mock, asynctest.mock.CoroutineMock) called = True test_mock_coroutine() self.assertTrue(called)
Example #30
Source File: test_mock.py From asynctest with Apache License 2.0 | 5 votes |
def test_patch_coroutine_function_with_CoroutineMock(self): default = asynctest.mock.DEFAULT with asynctest.mock.patch.multiple('test.test_mock.Test', a_function=default, a_coroutine=default, an_async_coroutine=default): import test.test_mock obj = test.test_mock.Test() self.assertIsInstance(obj.a_function, asynctest.mock.MagicMock) self.assertIsInstance(obj.a_coroutine, asynctest.mock.CoroutineMock) self.assertIsInstance(obj.an_async_coroutine, asynctest.mock.CoroutineMock)