async def test_add_emoji_role_pair(mocker): """Adds a emoji/role pair to the role message creation.""" cog, mock_bot, reaction_for_role_message = init_mocks() cog.delete_setup_messages = mocker.AsyncMock() cog.send_reply.return_value = tosurnament_mock.MessageMock(tosurnament_mock.SETUP_MESSAGE_MOCK.id + 1) mock_channel = tosurnament_mock.ChannelMock(tosurnament_mock.SETUP_CHANNEL_MOCK.id + 1) mock_ctx = tosurnament_mock.CtxMock(mock_bot, channel=mock_channel) mock_preview_message = tosurnament_mock.MessageMock(tosurnament_mock.PREVIEW_MESSAGE_MOCK.id + 1) mock_ctx.send.return_value = mock_preview_message await cog.add_emoji_role_pair(cog, mock_ctx, "🛎️", tosurnament_mock.VERIFIED_ROLE_MOCK) cog.delete_setup_messages.assert_called_once_with(reaction_for_role_message) cog.send_reply.assert_called_once_with(mocker.ANY, "success", "🛎️ | Verified\n") mock_ctx.send.assert_called_once_with("Some text") mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher( ReactionForRoleMessage( guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id, author_id=tosurnament_mock.DEFAULT_USER_MOCK.id, setup_channel_id=tosurnament_mock.SETUP_CHANNEL_MOCK.id + 1, setup_message_id=tosurnament_mock.SETUP_MESSAGE_MOCK.id + 1, preview_message_id=tosurnament_mock.PREVIEW_MESSAGE_MOCK.id + 1, channel_id=tosurnament_mock.DEFAULT_CHANNEL_MOCK.id, text="Some text", emojis="🛎️", roles=str(tosurnament_mock.VERIFIED_ROLE_MOCK.id), ) ) ) assert mock_preview_message.reactions == ["🛎️"]
async def test_set_post_result_channel(mocker): """Sets the post result channel.""" cog, mock_bot, bracket = init_mocks() channel = tosurnament_mock.ChannelMock(324769) assert bracket.post_result_channel_id != channel.id await cog.set_post_result_channel(cog, tosurnament_mock.CtxMock(mock_bot), channel=channel) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, post_result_channel_id=channel.id, name=BRACKET1_NAME)) )
async def test_reaction_on_end_tournament_message(mocker): """Sends a message to react on in order to end the tournament.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot)) mock_bot.session.add_stub( Tournament(id=1, guild_id=tosurnament_mock.GUILD_ID)) mock_bot.session.add_stub( Bracket(tournament_id=1, schedules_spreadsheet_id=1, players_spreadsheet_id=1)) mock_bot.session.add_stub(SchedulesSpreadsheet(id=1)) mock_bot.session.add_stub(PlayersSpreadsheet(id=1)) mock_bot.session.add_stub(Bracket(tournament_id=1)) mock_bot.session.add_stub( Bracket(tournament_id=1, schedules_spreadsheet_id=42, players_spreadsheet_id=42)) mock_bot.session.add_stub(SchedulesSpreadsheet(id=42)) mock_bot.session.add_stub(PlayersSpreadsheet(id=42)) mock_bot.session.add_stub( Bracket(tournament_id=1, schedules_spreadsheet_id=43)) mock_bot.session.add_stub(RescheduleMessage(tournament_id=1)) mock_bot.session.add_stub(RescheduleMessage(tournament_id=1)) mock_bot.session.add_stub(StaffRescheduleMessage(tournament_id=1)) mock_bot.session.add_stub(StaffRescheduleMessage(tournament_id=1)) mock_bot.session.add_stub(EndTournamentMessage(message_id=MESSAGE_ID)) mock_bot.session.add_stub(Tournament(id=2)) mock_bot.session.add_stub( Bracket(tournament_id=2, schedules_spreadsheet_id=2, players_spreadsheet_id=2)) mock_bot.session.add_stub(SchedulesSpreadsheet(id=2)) mock_bot.session.add_stub(PlayersSpreadsheet(id=2)) mock_bot.session.add_stub(RescheduleMessage(tournament_id=2)) await cog.reaction_on_end_tournament_message( MESSAGE_ID, tosurnament_mock.EmojiMock("✅"), tosurnament_mock.GuildMock(tosurnament_mock.GUILD_ID), tosurnament_mock.ChannelMock(), tosurnament_mock.UserMock(user_id=tosurnament_mock.GuildMock.OWNER_ID), ) cog.send_reply.assert_called_with(mocker.ANY, mocker.ANY, "success") assert len(mock_bot.session.tables[Tournament.__tablename__]) == 1 assert len(mock_bot.session.tables[Bracket.__tablename__]) == 1 assert len( mock_bot.session.tables[SchedulesSpreadsheet.__tablename__]) == 1 assert len(mock_bot.session.tables[PlayersSpreadsheet.__tablename__]) == 1 assert len(mock_bot.session.tables[RescheduleMessage.__tablename__]) == 1 assert len( mock_bot.session.tables[StaffRescheduleMessage.__tablename__]) == 0 assert len( mock_bot.session.tables[EndTournamentMessage.__tablename__]) == 0
async def test_reaction_on_end_tournament_message_invalid_emoji(): """Sends a message to react on in order to end the tournament.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot)) await cog.reaction_on_end_tournament_message( MESSAGE_ID, tosurnament_mock.EmojiMock("=)"), tosurnament_mock.GuildMock(tosurnament_mock.GUILD_ID), tosurnament_mock.ChannelMock(), tosurnament_mock.UserMock(user_id=tosurnament_mock.GuildMock.OWNER_ID), ) assert mock_bot.session.query.call_count == 0
async def test_reaction_on_end_tournament_message_no_tournament(): """Sends a message to react on in order to end the tournament.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot)) mock_bot.session.add_stub(EndTournamentMessage(message_id=MESSAGE_ID)) await cog.reaction_on_end_tournament_message( MESSAGE_ID, tosurnament_mock.EmojiMock("✅"), tosurnament_mock.GuildMock(tosurnament_mock.GUILD_ID), tosurnament_mock.ChannelMock(), tosurnament_mock.UserMock(user_id=tosurnament_mock.GuildMock.OWNER_ID), ) assert mock_bot.session.delete.call_count == 1
async def test_reaction_on_end_tournament_message_refuse(mocker): """Sends a message to react on in order to end the tournament.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot)) mock_bot.session.add_stub(Tournament(guild_id=tosurnament_mock.GUILD_ID)) mock_bot.session.add_stub(Bracket()) mock_bot.session.add_stub(EndTournamentMessage(message_id=MESSAGE_ID)) await cog.reaction_on_end_tournament_message( MESSAGE_ID, tosurnament_mock.EmojiMock("❎"), tosurnament_mock.GuildMock(tosurnament_mock.GUILD_ID), tosurnament_mock.ChannelMock(), tosurnament_mock.UserMock(user_id=tosurnament_mock.GuildMock.OWNER_ID), ) cog.send_reply.assert_called_with(mocker.ANY, mocker.ANY, "refused")
async def test_remove_match_to_ignore(mocker): """Removes a match to ignore.""" MATCH_ID_1 = "T1-1" MATCH_ID_2 = "t1-2" MATCH_ID_3 = "T1-3" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament( id=1, guild_id=tosurnament_mock.GUILD_ID, matches_to_ignore=MATCH_ID_1 + "\n" + MATCH_ID_3, current_bracket_id=1, staff_channel_id=tosurnament_mock.ChannelMock.STAFF_CHANNEL_ID, )) mock_bot.session.add_stub( Bracket(id=1, tournament_id=1, schedules_spreadsheet_id=1)) mock_bot.session.add_stub( SchedulesSpreadsheet(id=1, spreadsheet_id="single", sheet_name="Tier 1")) cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot)) await cog.remove_match_to_ignore(cog, tosurnament_mock.CtxMock(mock_bot), MATCH_ID_2, MATCH_ID_3) assert mock_bot.session.tables[ Tournament.__tablename__][0].matches_to_ignore == MATCH_ID_1 expected_replies = [ mocker.call(mocker.ANY, mocker.ANY, "success", MATCH_ID_1), mocker.call( tosurnament_mock.ChannelMock( tosurnament_mock.ChannelMock.STAFF_CHANNEL_ID), mocker.ANY, "to_not_ignore", mocker.ANY, MATCH_ID_3, ), ] assert cog.send_reply.call_args_list == expected_replies
async def test_add_emoji_role_pair_with_already_existing_emoji_role_pairs(mocker): """Adds a emoji/role pair to the role message creation, and it already contains emojis and roles.""" cog, mock_bot, reaction_for_role_message = init_mocks() reaction_for_role_message.emojis = "✅\n❎" reaction_for_role_message.roles = str(tosurnament_mock.PLAYER_ROLE_MOCK.id) + "\n12349089" cog.delete_setup_messages = mocker.AsyncMock() cog.send_reply.return_value = tosurnament_mock.MessageMock(tosurnament_mock.SETUP_MESSAGE_MOCK.id + 1) mock_channel = tosurnament_mock.ChannelMock(tosurnament_mock.SETUP_CHANNEL_MOCK.id + 1) mock_command = tosurnament_mock.CommandMock(cog.qualified_name, "add_emoji_role_pair") mock_ctx = tosurnament_mock.CtxMock(mock_bot, channel=mock_channel, command=mock_command) mock_preview_message = tosurnament_mock.MessageMock(tosurnament_mock.PREVIEW_MESSAGE_MOCK.id + 1) mock_ctx.send.return_value = mock_preview_message await cog.add_emoji_role_pair(cog, mock_ctx, "🛎️", tosurnament_mock.VERIFIED_ROLE_MOCK) cog.delete_setup_messages.assert_called_once_with(reaction_for_role_message) expected_success_output = "✅ | " + tosurnament_mock.PLAYER_ROLE_MOCK.name + "\n" expected_success_output += "❎ | Unknown role\n" expected_success_output += "🛎️ | Verified\n" cog.send_reply.assert_called_once_with(mocker.ANY, "success", expected_success_output) mock_ctx.send.assert_called_once_with("Some text") expected_roles = str(tosurnament_mock.PLAYER_ROLE_MOCK.id) + "\n" expected_roles += "12349089\n" expected_roles += str(tosurnament_mock.VERIFIED_ROLE_MOCK.id) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher( ReactionForRoleMessage( guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id, author_id=tosurnament_mock.DEFAULT_USER_MOCK.id, setup_channel_id=tosurnament_mock.SETUP_CHANNEL_MOCK.id + 1, setup_message_id=tosurnament_mock.SETUP_MESSAGE_MOCK.id + 1, preview_message_id=tosurnament_mock.PREVIEW_MESSAGE_MOCK.id + 1, channel_id=tosurnament_mock.DEFAULT_CHANNEL_MOCK.id, text="Some text", emojis="✅\n❎\n🛎️", roles=expected_roles, ) ) ) assert mock_preview_message.reactions == ["✅", "❎", "🛎️"]