async def test_set_qualifiers_results_spreadsheet(mocker): """Sets bracket spreadsheets.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1)) cog = tosurnament_mock.mock_cog(module_to_test.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot) spreadsheet_id = "abcd1234" sheet_name = "a sheet name" await cog.set_qualifiers_results_spreadsheet(cog, mock_ctx, spreadsheet_id, sheet_name=sheet_name) update_expected = [ mocker.call(tosurnament_mock.Matcher(Bracket(tournament_id=1, qualifiers_results_spreadsheet_id=1))), mocker.call( tosurnament_mock.Matcher(QualifiersResultsSpreadsheet(spreadsheet_id=spreadsheet_id, sheet_name=sheet_name)) ), ] assert mock_bot.session.update.call_args_list == update_expected await cog.set_qualifiers_results_spreadsheet(cog, mock_ctx, spreadsheet_id, sheet_name="") update_expected = [ mocker.call( tosurnament_mock.Matcher(QualifiersResultsSpreadsheet(spreadsheet_id=spreadsheet_id, sheet_name=sheet_name)) ), ] assert mock_bot.session.update.call_args_list[2:] == update_expected
async def test_create_reaction_for_role_message(mocker): """Creates a reaction for role message.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(reaction_for_role_message_module.get_class(mock_bot)) cog.send_reply = mocker.AsyncMock(return_value=tosurnament_mock.SETUP_MESSAGE_MOCK) mock_ctx = tosurnament_mock.CtxMock(mock_bot, channel=tosurnament_mock.SETUP_CHANNEL_MOCK) mock_ctx.send = mocker.AsyncMock(return_value=tosurnament_mock.PREVIEW_MESSAGE_MOCK) message_text = "Some text" await cog.create_reaction_for_role_message( cog, mock_ctx, tosurnament_mock.DEFAULT_CHANNEL_MOCK, message_text=message_text ) cog.send_reply.assert_called_once_with(mocker.ANY, "success", "None\n") mock_ctx.send.assert_called_once_with(message_text) mock_bot.session.add.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, setup_message_id=tosurnament_mock.SETUP_MESSAGE_MOCK.id, preview_message_id=tosurnament_mock.PREVIEW_MESSAGE_MOCK.id, channel_id=tosurnament_mock.DEFAULT_CHANNEL_MOCK.id, text=message_text, ) ) )
async def test_link_regenerate_code(mocker): """Links the user, and since they already tried to link, regenerate the linking code.""" mocker.patch(OS_MODULE + ".urandom", mocker.Mock(return_value=CODE_URANDOM)) mock_author = tosurnament_mock.UserMock() await mock_author.create_dm() mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( User(discord_id=tosurnament_mock.DEFAULT_USER_MOCK.id, verified=False, code="test")) cog = tosurnament_mock.mock_cog(auth.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot, mock_author) await cog.link(cog, mock_ctx) assert mock_bot.session.add.call_count == 0 user_matcher = tosurnament_mock.Matcher( User( verified=False, code=CODE_ASCII, )) mock_bot.session.update.assert_called_once_with(user_matcher) assert mock_ctx.message.delete.call_count == 1 cog.send_reply.assert_called_once_with( mocker.ANY, "success", CODE_ASCII, channel=tosurnament_mock.DM_CHANNEL_MOCK)
async def test_set_spreadsheet_values(mocker): """Sets spreadsheet values.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.GUILD_ID)) mock_bot.session.add_stub( Bracket(id=1, tournament_id=1, schedules_spreadsheet_id=1, players_spreadsheet_id=1)) mock_bot.session.add_stub(SchedulesSpreadsheet(id=1)) cog = tosurnament_mock.mock_cog(bracket.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot) await cog.set_schedules_spreadsheet_values(mock_ctx, {"sheet_name": SHEET_NAME}) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(SchedulesSpreadsheet(sheet_name=SHEET_NAME))) with pytest.raises(base.NoSpreadsheet): await cog.set_players_spreadsheet_values(mock_ctx, {"sheet_name": SHEET_NAME}) mock_bot.session.add_stub(PlayersSpreadsheet(id=1)) await cog.set_players_spreadsheet_values(mock_ctx, {"sheet_name": SHEET_NAME})
async def test_auth_not_linked(self): """Tries to authenticate the user but they didn't link their osu! account yet.""" bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.UserNotLinked): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
async def test_auth_already_verified(self): """Tries to authenticate the user but they are already verified.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=True)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(auth.UserAlreadyVerified): await cog.auth(cog, tosurnament_mock.CtxMock(bot_mock))
def init_mocks(n_of_brackets=1): mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) for i in range(n_of_brackets): bracket = Bracket(id=i + 1, tournament_id=1, name=("Bracket " + str(i + 1))) mock_bot.session.add_stub(bracket) cog = tosurnament_mock.mock_cog(bracket_module.get_class(mock_bot)) return cog, mock_bot, bracket
async def test_is_player_in_challonge(mocker): """Gets the team_info (if applicable) and the player name of the player if they are a running participant.""" # TODO cog = tosurnament_mock.mock_cog(bracket_module.get_class(tosurnament_mock.BotMock())) member = tosurnament_mock.UserMock() teams_info = [] participants = [] cog.is_player_in_challonge(member, teams_info, participants)
def test_is_player_in_challonge_no_teams_info(mocker): """ "Gets the team_info (if applicable) and the player name of the player if they are a running participant.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(bracket_module.get_class(mock_bot)) mock_bot.session.add_stub(tosurnament_mock.DEFAULT_USER_STUB) team_info, player_name = cog.is_player_in_challonge(tosurnament_mock.DEFAULT_USER_MOCK, [], PARTICIPANT_LIST) assert not team_info assert player_name == tosurnament_mock.DEFAULT_USER_STUB.osu_name
def test_is_player_in_challonge_not_a_participant(mocker): """ "Gets the team_info (if applicable) and the player name of the player if they are a running participant.""" cog = tosurnament_mock.mock_cog(bracket_module.get_class(tosurnament_mock.BotMock())) member_name = "abvdsrjgfkh" member = tosurnament_mock.UserMock(user_name=member_name) team_info, player_name = cog.is_player_in_challonge(member, [], PARTICIPANT_LIST) assert not team_info assert not player_name
def init_mocks(): mock_bot = tosurnament_mock.BotMock() tournament = Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id) mock_bot.session.add_stub(tournament) bracket = Bracket(id=1, tournament_id=1) mock_bot.session.add_stub(bracket) cog = tosurnament_mock.mock_cog(staff_module.get_class(mock_bot)) return cog, mock_bot, tournament, bracket
async def test_set_team_captain_role_remove(self): """Removes the team captain role.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(Tournament(team_captain_role_id=1)) cog = tosurnament_mock.mock_cog(tournament.get_class(bot_mock)) await cog.set_team_captain_role(cog, tosurnament_mock.CtxMock(bot_mock)) bot_mock.session.update.assert_called_once_with(tosurnament_mock.Matcher(Tournament(team_captain_role_id=0)))
async def test_link_already_verified(): """Links the user but they are already verified.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(tosurnament_mock.DEFAULT_USER_STUB) cog = tosurnament_mock.mock_cog(auth.get_class(mock_bot)) with pytest.raises(auth.UserAlreadyVerified): await cog.link(cog, tosurnament_mock.CtxMock(mock_bot))
def init_mocks(): mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1, qualifiers_results_spreadsheet_id=1)) qualifiers_results_spreadsheet = QualifiersResultsSpreadsheet(id=1) mock_bot.session.add_stub(qualifiers_results_spreadsheet) cog = tosurnament_mock.mock_cog(module_to_test.get_class(mock_bot)) return cog, mock_bot, qualifiers_results_spreadsheet
async def test_get_a_bracket_that_does_not_exist(self): """Sets a bracket as current bracket but it does not exist.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(Tournament()) bot_mock.session.add_stub(Bracket(name=BRACKET_NAME)) cog = tosurnament_mock.mock_cog(tournament.get_class(bot_mock)) with self.assertRaises(discord.ext.commands.UserInputError): await cog.get_bracket(cog, tosurnament_mock.CtxMock(bot_mock), number=0)
async def test_set_tournament_values(self): """Puts the input values into the corresponding tournament.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(Tournament()) cog = tosurnament_mock.mock_cog(tournament.get_class(bot_mock)) await cog.set_tournament_values(tosurnament_mock.CtxMock(bot_mock), {"current_bracket_id": 1}) bot_mock.session.update.assert_called_once_with(tosurnament_mock.Matcher(Tournament(current_bracket_id=1))) cog.send_reply.assert_called_once_with(mock.ANY, mock.ANY, "success", 1)
async def test_end_tournament(self): """Sends a message to react on in order to end the tournament.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(Tournament()) cog = tosurnament_mock.mock_cog(guild_owner.get_class(bot_mock)) await cog.end_tournament(cog, tosurnament_mock.CtxMock(bot_mock)) cog.send_reply.assert_called_with(mock.ANY, mock.ANY, "are_you_sure") assert bot_mock.session.add.call_count == 1
async def test_set_guild_values_and_create_guild(): """Sets guild values and create Guild object as it does not exist.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_module.get_class(mock_bot)) role = tosurnament_mock.RoleMock("role", 324987) await cog.set_guild_values(tosurnament_mock.CtxMock(mock_bot), {"admin_role_id": role.id}) mock_bot.session.add.assert_called_once_with( tosurnament_mock.Matcher(Guild(admin_role_id=role.id)))
async def test_set_bracket_name_no_name(self): """Changes current bracket's name to empty (invalid).""" bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(bracket.get_class(bot_mock)) with self.assertRaises(discord.ext.commands.UserInputError): await cog.set_bracket_name(cog, tosurnament_mock.CtxMock(bot_mock), 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_copy_bracket_not_enough_bracket(mocker): """Copies a bracket settings to another one, but there is only one bracket.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament(id=1, guild_id=tosurnament_mock.GUILD_ID)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1)) cog = tosurnament_mock.mock_cog(bracket.get_class(mock_bot)) with pytest.raises(commands.UserInputError): await cog.copy_bracket(cog, tosurnament_mock.CtxMock(mock_bot), 1, 2)
async def test_set_team_captain_role_remove(): """Removes the team captain role.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament(guild_id=tosurnament_mock.GUILD_ID, team_captain_role_id=1)) cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot)) await cog.set_team_captain_role(cog, tosurnament_mock.CtxMock(mock_bot)) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Tournament(team_captain_role_id=0)))
async def test_set_reschedule_deadline_end_invalid_date(): """Sets a reschedule deadline end but date is invalid.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament(id=1, guild_id=tosurnament_mock.GUILD_ID)) cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot)) with pytest.raises(discord.ext.commands.UserInputError): await cog.set_reschedule_deadline_end( cog, tosurnament_mock.CtxMock(mock_bot), date="some date")
async def test_end_tournament(mocker): """Sends a message to react on in order to end the tournament.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(guild_id=tosurnament_mock.GUILD_ID)) cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot)) await cog.end_tournament(cog, tosurnament_mock.CtxMock(mock_bot)) cog.send_reply.assert_called_with(mocker.ANY, mocker.ANY, "are_you_sure") mock_bot.session.add.assert_called_once_with( tosurnament_mock.Matcher(EndTournamentMessage()))
async def test_link_already_verified(self): """Links the user but they are already verified.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(User(verified=True)) cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(auth.UserAlreadyVerified): await cog.link( cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME, )
async def test_link_user_not_found(self, mock_osu): """Links the user but the osu name/id is not found.""" mock_osu.get_user.return_value = None bot_mock = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(auth.get_class(bot_mock)) with self.assertRaises(base.UserNotFound): await cog.link( cog, tosurnament_mock.CtxMock(bot_mock), osu_name=tosurnament_mock.USER_NAME, )
async def test_get_a_bracket(self): """Sets a bracket as current bracket.""" bot_mock = tosurnament_mock.BotMock() bot_mock.session.add_stub(Tournament(current_bracket_id=1)) bot_mock.session.add_stub(Bracket(id=1, name=BRACKET_NAME)) bot_mock.session.add_stub(Bracket(id=2, name=BRACKET_NAME_2)) cog = tosurnament_mock.mock_cog(tournament.get_class(bot_mock)) await cog.get_bracket(cog, tosurnament_mock.CtxMock(bot_mock), number=2) bot_mock.session.update.assert_called_once_with(tosurnament_mock.Matcher(Tournament(current_bracket_id=2))) cog.send_reply.assert_called_once_with(mock.ANY, mock.ANY, "success", BRACKET_NAME_2)
async def test_setup_verification_channel_no_verified_role(mocker): """Setups the verification channel, but the verified role does not exist.""" mock_bot = tosurnament_mock.BotMock() cog = tosurnament_mock.mock_cog(guild_module.get_class(mock_bot)) mock_guild = tosurnament_mock.DEFAULT_GUILD_MOCK mock_guild.roles = [] mock_channel = tosurnament_mock.DEFAULT_CHANNEL_MOCK with pytest.raises(exceptions.RoleDoesNotExist): await cog.setup_verification_channel( cog, tosurnament_mock.CtxMock(mock_bot, guild=mock_guild), mock_channel)
async def test_set_tournament_values(mocker): """Puts the input values into the corresponding tournament.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(guild_id=tosurnament_mock.GUILD_ID)) cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot)) await cog.set_tournament_values(tosurnament_mock.CtxMock(mock_bot), {"current_bracket_id": 1}) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Tournament(current_bracket_id=1))) cog.send_reply.assert_called_once_with(mocker.ANY, mocker.ANY, "success", 1)
async def test_get_a_bracket_that_does_not_exist(): """Sets a bracket as current bracket but it does not exist.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub( Tournament(id=1, guild_id=tosurnament_mock.GUILD_ID)) mock_bot.session.add_stub(Bracket(tournament_id=1, name=BRACKET_NAME)) cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot)) with pytest.raises(discord.ext.commands.UserInputError): await cog.get_bracket(cog, tosurnament_mock.CtxMock(mock_bot), number=0)