async def test_post_process(self): async def process(args): args.messages[0] = 'Kappa' mock_process = CoroutineMock(spec=process, side_effect=process) self.mock_list.postProcess.return_value = [mock_process] self.mock_split.return_value = [] self.assertEqual( await library.create_messages(self.command, self.args), ['Kappa']) mock_process.assert_called_once_with(TypeMatch(CustomProcessArgs))
async def test_reload_config(self, mock_bot, mock_reload): module = Mock() originalConfig = mock_bot.config mock_read = CoroutineMock() mock_bot._config.BotConfig.return_value.read_config = mock_read sys.modules = {'bot._config': module} self.assertIs(await reload.reload_config(self.send), True) self.assertEqual(self.send.call_count, 2) self.assertEqual(mock_reload.call_count, 1) mock_bot._config.BotConfig.assert_called_once_with() mock_read.assert_called_once_with() self.assertNotEqual(mock_bot.config, originalConfig)
class TestBackgroundRunTask(asynctest.TestCase): def setUp(self): self.taskMethod = CoroutineMock(spec=some_task_to_run) self.now = datetime(2000, 1, 1, 0, 0, 0) patcher = patch('bot.utils.logException') self.addCleanup(patcher.stop) self.mock_logException = patcher.start() async def test(self): await bot.coroutine.background._run_task(self.taskMethod, self.now) self.taskMethod.assert_called_once_with(self.now) self.assertFalse(self.mock_logException.called) async def test_exception(self): self.taskMethod.side_effect = Exception await bot.coroutine.background._run_task(self.taskMethod, self.now) self.taskMethod.assert_called_once_with(self.now) self.mock_logException.assert_called_once_with()
class TestLibraryManageBot(asynctest.TestCase): def setUp(self): self.data = Mock(spec=CacheStore) self.database = Mock(spec=DatabaseMain) self.permissions = Mock(spec=ChatPermissionSet) self.send = Mock(spec=send) self.method = CoroutineMock(spec=method, return_value=True) patcher = patch('lib.items.manage') self.addCleanup(patcher.stop) self.mock_manage = patcher.start() self.mock_manage.methods.return_value = { 'method': self.method, 'none': None, } async def test(self): message = Message('!managebot method') self.assertIs( await managebot.manage_bot(self.data, self.permissions, self.send, 'managebot', message), True) self.assertFalse(self.send.called) self.method.assert_called_once_with(TypeMatch(ManageBotArgs)) async def test_not_existing(self): message = Message('!managebot not_existing') self.assertIs( await managebot.manage_bot(self.data, self.permissions, self.send, 'managebot', message), False) self.assertFalse(self.send.called) self.assertFalse(self.method.called) async def test_none(self): message = Message('!managebot none') self.assertIs( await managebot.manage_bot(self.data, self.permissions, self.send, 'managebot', message), False) self.assertFalse(self.send.called) self.assertFalse(self.method.called)
async def test_reload_config_multiple(self, mock_bot, mock_reload): async def wait(*args): await asyncio.sleep(0.2) async def call_0(): return await reload.reload_config(self.send) async def call_1(): await asyncio.sleep(0.1) return await reload.reload_config(self.send) module = Mock() originalConfig = mock_bot.config mock_read = CoroutineMock() mock_bot._config.BotConfig.return_value.read_config = mock_read sys.modules = {'bot._config': module} mock_read.side_effect = wait self.assertEqual(await asyncio.gather(call_0(), call_1()), [True, True]) self.assertEqual(self.send.call_count, 2) self.assertEqual(mock_reload.call_count, 1) mock_bot._config.BotConfig.assert_called_once_with() mock_read.assert_called_once_with() self.assertNotEqual(mock_bot.config, originalConfig)