def test_player_count_display_with_players(self): bot = GatherBot('testuser') bot.organiser.queues['testchannel'] = set(['player1', 'player2']) self.assertEqual( '(2/10)', bot.player_count_display('testchannel') )
def test_player_count_display_with_zero(self): bot = GatherBot('testuser') bot.organiser.queues['testchannel'] = set() self.assertEqual( '(0/10)', bot.player_count_display('testchannel') )
def setUp(self): self.send_message = get_mock_coro(True) self.bot = GatherBot('gatherbot') self.bot.register_message_handler(self.send_message) self.bot.register_action('^!help$', commands.bot_help) self.bot.register_action('^!(?:add|join|s)$', commands.add) self.bot.register_action('^!(?:remove|rem|so)$', commands.remove) self.bot.register_action('^!(?:game|status)$', commands.game_status) self.bot.register_action('^!(?:reset)$', commands.reset)
async def test_register_and_send(self): bot = GatherBot('testuser') handler = get_mock_coro(True) bot.register_message_handler(handler) await bot.say('Test channel', 'Test message') self.assertEqual(1, len(bot.message_handlers)) self.assertTrue(handler.called)
def test_register(self): bot = GatherBot('testuser') self.assertEqual({}, bot.actions) regex = r'^test' action = mock.Mock() bot.register_action(regex, action) self.assertEqual( {regex: (re.compile(regex, re.IGNORECASE), action)}, bot.actions )
async def test_on_message_from_other(self): bot = GatherBot('testuser') regex = r'^test' action = get_mock_coro(True) bot.actions = {regex: (re.compile(regex, re.IGNORECASE), action)} mock_message = mock.Mock() mock_message.author = 'anotheruser' mock_message.content = 'test' await bot.on_message(mock_message) self.assertTrue(action.called)
async def test_member_went_afk_in_no_channels(self): bot = GatherBot('testuser') bot.say = get_mock_coro(True) bot.announce_players = get_mock_coro(True) bot.organiser = unittest.mock.Mock() bot.organiser.remove_from_all.return_value = set() player = unittest.mock.Mock() await bot.member_went_afk(player) self.assertTrue(bot.organiser.remove_from_all.called)
async def test_on_message_error(self): bot = GatherBot('testuser') regex = r'^test' action = get_mock_coro_throwing_exception(AssertionError, 'error') bot.actions = {regex: (re.compile(regex, re.IGNORECASE), action)} bot.say = get_mock_coro(True) mock_message = mock.Mock() mock_message.author = 'anotheruser' mock_message.content = 'test' await bot.on_message(mock_message) self.assertTrue(action.called) self.assertTrue(bot.say.called)
async def test_announce_players(self, mock_organiser): bot = GatherBot('testuser') bot.say = get_mock_coro(True) bot.player_count_display = unittest.mock.Mock(return_value='(1/10)') bot.organiser.queues['test channel'] = set(['mac']) await bot.announce_players('test channel') bot.say.assert_called_with( 'test channel', 'Currently signed in players (1/10): mac' )
async def test_say_lines(self, mock_organiser): bot = GatherBot('testuser') bot.say = get_mock_coro(True) await bot.say_lines( 'test channel', [ 'test message 1', 'test message 2', ] ) bot.say.assert_has_calls([ unittest.mock.call('test channel', 'test message 1'), unittest.mock.call('test channel', 'test message 2'), ])
async def test_member_went_offline_in_channels(self): bot = GatherBot('testuser') bot.say = get_mock_coro(True) bot.announce_players = get_mock_coro(True) bot.player_count_display = get_mock_coro(True) bot.organiser = unittest.mock.Mock() bot.organiser.remove_from_all.return_value = set(['testchannel']) player = unittest.mock.Mock() await bot.member_went_offline(player) self.assertTrue(bot.organiser.remove_from_all.called) self.assertTrue(bot.say.called) self.assertTrue(bot.announce_players.called)
class TestGatherBotIntegration(unittest.TestCase): def setUp(self): self.send_message = get_mock_coro(True) self.bot = GatherBot('gatherbot') self.bot.register_message_handler(self.send_message) self.bot.register_action('^!help$', commands.bot_help) self.bot.register_action('^!(?:add|join|s)$', commands.add) self.bot.register_action('^!(?:remove|rem|so)$', commands.remove) self.bot.register_action('^!(?:game|status)$', commands.game_status) self.bot.register_action('^!(?:reset)$', commands.reset) @async_test async def test_10_players_find_a_game(self): server = Server('testserver') channel = Channel(server, 'testchannel') for i in range(10): await self.bot.on_message( Message(Member('player{}'.format(i)), channel, '!s')) self.send_message.assert_has_calls([ call(channel, 'You are now signed in, player0. (1/10)'), call(channel, 'You are now signed in, player1. (2/10)'), call(channel, 'You are now signed in, player2. (3/10)'), call(channel, 'You are now signed in, player3. (4/10)'), call(channel, 'You are now signed in, player4. (5/10)'), call(channel, 'You are now signed in, player5. (6/10)'), call(channel, 'You are now signed in, player6. (7/10)'), call(channel, 'You are now signed in, player7. (8/10)'), call(channel, 'You are now signed in, player8. (9/10)'), call(channel, 'You are now signed in, player9. (10/10)')]) # FIXME: Check that this message better matches a template. # The player order is random, so you can't just compare the message self.assertTrue( self.send_message.mock_calls[-1][1][1].startswith('Game starting!') ) @async_test async def test_multiple_servers(self): server1 = Server('testserver1') server2 = Server('testserver2') channel1 = Channel(server1, 'testchannel') channel2 = Channel(server2, 'testchannel') for i in range(5): await self.bot.on_message( Message(Member('player{}'.format(i)), channel1, '!s')) for i in range(5, 10): await self.bot.on_message( Message(Member('player{}'.format(i)), channel2, '!s')) self.send_message.assert_has_calls([ call(channel1, 'You are now signed in, player0. (1/10)'), call(channel1, 'You are now signed in, player1. (2/10)'), call(channel1, 'You are now signed in, player2. (3/10)'), call(channel1, 'You are now signed in, player3. (4/10)'), call(channel1, 'You are now signed in, player4. (5/10)'), call(channel2, 'You are now signed in, player5. (1/10)'), call(channel2, 'You are now signed in, player6. (2/10)'), call(channel2, 'You are now signed in, player7. (3/10)'), call(channel2, 'You are now signed in, player8. (4/10)'), call(channel2, 'You are now signed in, player9. (5/10)')]) self.assertEqual(10, len(self.send_message.mock_calls), self.send_message.mock_calls)